android: extract PostActions instance creation

This commit is contained in:
Harsh Shandilya 2022-02-06 15:32:19 +05:30
parent de5d7fe42c
commit baa3830966
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
2 changed files with 37 additions and 21 deletions

View file

@ -32,10 +32,8 @@ import dev.msfjarvis.claw.android.viewmodel.ClawViewModel
import dev.msfjarvis.claw.common.comments.CommentsPage
import dev.msfjarvis.claw.common.comments.HTMLConverter
import dev.msfjarvis.claw.common.comments.LocalHTMLConverter
import dev.msfjarvis.claw.common.posts.PostActions
import dev.msfjarvis.claw.common.theme.LobstersTheme
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
import dev.msfjarvis.claw.database.local.SavedPost
private const val ScrollDelta = 50
@ -51,6 +49,7 @@ fun LobstersApp(
val networkListState = rememberLazyListState()
val savedListState = rememberLazyListState()
val navController = rememberNavController()
val postActions = rememberPostActions(urlLauncher, navController, viewModel)
// The destination needs to be tracked here rather than used directly since
// `NavController#currentDestination` is not a Composable state.
var currentDestination by remember { mutableStateOf<String?>(null) }
@ -71,25 +70,6 @@ fun LobstersApp(
}
}
}
val postActions = remember {
object : PostActions {
override fun viewPost(postUrl: String, commentsUrl: String) {
urlLauncher.openUri(postUrl.ifEmpty { commentsUrl })
}
override fun viewComments(postId: String) {
navController.navigate(Destinations.Comments.getRoute(postId))
}
override fun viewCommentsPage(commentsUrl: String) {
urlLauncher.openUri(commentsUrl)
}
override fun toggleSave(post: SavedPost) {
viewModel.toggleSave(post)
}
}
}
navController.addOnDestinationChangedListener { _, destination, _ ->
currentDestination = destination.route ?: Destinations.Hottest.getRoute()
}

View file

@ -0,0 +1,36 @@
package dev.msfjarvis.claw.android.ui
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.navigation.NavController
import dev.msfjarvis.claw.android.viewmodel.ClawViewModel
import dev.msfjarvis.claw.common.posts.PostActions
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
import dev.msfjarvis.claw.database.local.SavedPost
@Composable
fun rememberPostActions(
urlLauncher: UrlLauncher,
navController: NavController,
viewModel: ClawViewModel,
): PostActions {
return remember {
object : PostActions {
override fun viewPost(postUrl: String, commentsUrl: String) {
urlLauncher.openUri(postUrl.ifEmpty { commentsUrl })
}
override fun viewComments(postId: String) {
navController.navigate(Destinations.Comments.getRoute(postId))
}
override fun viewCommentsPage(commentsUrl: String) {
urlLauncher.openUri(commentsUrl)
}
override fun toggleSave(post: SavedPost) {
viewModel.toggleSave(post)
}
}
}
}