mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-15 16:57:05 +05:30
android: extract PostActions instance creation
This commit is contained in:
parent
de5d7fe42c
commit
baa3830966
2 changed files with 37 additions and 21 deletions
|
@ -32,10 +32,8 @@ import dev.msfjarvis.claw.android.viewmodel.ClawViewModel
|
||||||
import dev.msfjarvis.claw.common.comments.CommentsPage
|
import dev.msfjarvis.claw.common.comments.CommentsPage
|
||||||
import dev.msfjarvis.claw.common.comments.HTMLConverter
|
import dev.msfjarvis.claw.common.comments.HTMLConverter
|
||||||
import dev.msfjarvis.claw.common.comments.LocalHTMLConverter
|
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.theme.LobstersTheme
|
||||||
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
|
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
|
||||||
import dev.msfjarvis.claw.database.local.SavedPost
|
|
||||||
|
|
||||||
private const val ScrollDelta = 50
|
private const val ScrollDelta = 50
|
||||||
|
|
||||||
|
@ -51,6 +49,7 @@ fun LobstersApp(
|
||||||
val networkListState = rememberLazyListState()
|
val networkListState = rememberLazyListState()
|
||||||
val savedListState = rememberLazyListState()
|
val savedListState = rememberLazyListState()
|
||||||
val navController = rememberNavController()
|
val navController = rememberNavController()
|
||||||
|
val postActions = rememberPostActions(urlLauncher, navController, viewModel)
|
||||||
// The destination needs to be tracked here rather than used directly since
|
// The destination needs to be tracked here rather than used directly since
|
||||||
// `NavController#currentDestination` is not a Composable state.
|
// `NavController#currentDestination` is not a Composable state.
|
||||||
var currentDestination by remember { mutableStateOf<String?>(null) }
|
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, _ ->
|
navController.addOnDestinationChangedListener { _, destination, _ ->
|
||||||
currentDestination = destination.route ?: Destinations.Hottest.getRoute()
|
currentDestination = destination.route ?: Destinations.Hottest.getRoute()
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue