mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 23:27:04 +05:30
refactor: simplify argument passing
This commit is contained in:
parent
79aba9a187
commit
ccfcd48438
5 changed files with 17 additions and 12 deletions
|
@ -205,8 +205,6 @@ fun LobstersApp(
|
||||||
setWebUri("https://lobste.rs/s/$postId")
|
setWebUri("https://lobste.rs/s/$postId")
|
||||||
CommentsPage(
|
CommentsPage(
|
||||||
postId = postId,
|
postId = postId,
|
||||||
getDetails = viewModel::getPostComments,
|
|
||||||
getLinkMetadata = viewModel::getLinkMetadata,
|
|
||||||
postActions = postActions,
|
postActions = postActions,
|
||||||
htmlConverter = htmlConverter,
|
htmlConverter = htmlConverter,
|
||||||
)
|
)
|
||||||
|
|
|
@ -11,6 +11,8 @@ import dev.msfjarvis.claw.android.viewmodel.ClawViewModel
|
||||||
import dev.msfjarvis.claw.common.posts.PostActions
|
import dev.msfjarvis.claw.common.posts.PostActions
|
||||||
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
|
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
|
||||||
import dev.msfjarvis.claw.database.local.SavedPost
|
import dev.msfjarvis.claw.database.local.SavedPost
|
||||||
|
import dev.msfjarvis.claw.model.LinkMetadata
|
||||||
|
import dev.msfjarvis.claw.model.LobstersPostDetails
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.time.format.DateTimeFormatter
|
import java.time.format.DateTimeFormatter
|
||||||
|
|
||||||
|
@ -61,6 +63,14 @@ fun rememberPostActions(
|
||||||
override fun toggleSave(post: SavedPost) {
|
override fun toggleSave(post: SavedPost) {
|
||||||
viewModel.toggleSave(post)
|
viewModel.toggleSave(post)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun getComments(postId: String): LobstersPostDetails {
|
||||||
|
return viewModel.getPostComments(postId)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getLinkMetadata(url: String): LinkMetadata {
|
||||||
|
return viewModel.getLinkMetadata(url)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,6 @@ import java.time.temporal.TemporalAccessor
|
||||||
@Composable
|
@Composable
|
||||||
fun CommentsHeader(
|
fun CommentsHeader(
|
||||||
postDetails: LobstersPostDetails,
|
postDetails: LobstersPostDetails,
|
||||||
getLinkMetadata: suspend (String) -> LinkMetadata,
|
|
||||||
postActions: PostActions,
|
postActions: PostActions,
|
||||||
htmlConverter: HTMLConverter,
|
htmlConverter: HTMLConverter,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
|
@ -59,7 +58,7 @@ fun CommentsHeader(
|
||||||
produceState(
|
produceState(
|
||||||
initialValue = LinkMetadata(postDetails.url, null, null),
|
initialValue = LinkMetadata(postDetails.url, null, null),
|
||||||
) {
|
) {
|
||||||
value = getLinkMetadata(postDetails.url)
|
value = postActions.getLinkMetadata(postDetails.url)
|
||||||
}
|
}
|
||||||
|
|
||||||
Surface(color = MaterialTheme.colorScheme.background, modifier = modifier) {
|
Surface(color = MaterialTheme.colorScheme.background, modifier = modifier) {
|
||||||
|
|
|
@ -24,13 +24,11 @@ import dev.msfjarvis.claw.common.NetworkState.Success
|
||||||
import dev.msfjarvis.claw.common.posts.PostActions
|
import dev.msfjarvis.claw.common.posts.PostActions
|
||||||
import dev.msfjarvis.claw.common.ui.NetworkError
|
import dev.msfjarvis.claw.common.ui.NetworkError
|
||||||
import dev.msfjarvis.claw.common.ui.ProgressBar
|
import dev.msfjarvis.claw.common.ui.ProgressBar
|
||||||
import dev.msfjarvis.claw.model.LinkMetadata
|
|
||||||
import dev.msfjarvis.claw.model.LobstersPostDetails
|
import dev.msfjarvis.claw.model.LobstersPostDetails
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun CommentsPageInternal(
|
private fun CommentsPageInternal(
|
||||||
details: LobstersPostDetails,
|
details: LobstersPostDetails,
|
||||||
getLinkMetadata: suspend (String) -> LinkMetadata,
|
|
||||||
postActions: PostActions,
|
postActions: PostActions,
|
||||||
htmlConverter: HTMLConverter,
|
htmlConverter: HTMLConverter,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
|
@ -40,7 +38,6 @@ private fun CommentsPageInternal(
|
||||||
item {
|
item {
|
||||||
CommentsHeader(
|
CommentsHeader(
|
||||||
postDetails = details,
|
postDetails = details,
|
||||||
getLinkMetadata = getLinkMetadata,
|
|
||||||
postActions = postActions,
|
postActions = postActions,
|
||||||
htmlConverter = htmlConverter,
|
htmlConverter = htmlConverter,
|
||||||
)
|
)
|
||||||
|
@ -76,19 +73,17 @@ private fun CommentsPageInternal(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST", "LongParameterList")
|
@Suppress("UNCHECKED_CAST")
|
||||||
@Composable
|
@Composable
|
||||||
fun CommentsPage(
|
fun CommentsPage(
|
||||||
postId: String,
|
postId: String,
|
||||||
getDetails: suspend (String) -> LobstersPostDetails,
|
|
||||||
getLinkMetadata: suspend (String) -> LinkMetadata,
|
|
||||||
postActions: PostActions,
|
postActions: PostActions,
|
||||||
htmlConverter: HTMLConverter,
|
htmlConverter: HTMLConverter,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
val postDetails by
|
val postDetails by
|
||||||
produceState<NetworkState>(Loading) {
|
produceState<NetworkState>(Loading) {
|
||||||
runCatching { getDetails(postId) }
|
runCatching { postActions.getComments(postId) }
|
||||||
.fold(
|
.fold(
|
||||||
onSuccess = { details -> value = Success(details) },
|
onSuccess = { details -> value = Success(details) },
|
||||||
onFailure = { value = Error(error = it, description = "Failed to load comments") }
|
onFailure = { value = Error(error = it, description = "Failed to load comments") }
|
||||||
|
@ -99,7 +94,6 @@ fun CommentsPage(
|
||||||
is Success<*> -> {
|
is Success<*> -> {
|
||||||
CommentsPageInternal(
|
CommentsPageInternal(
|
||||||
details = (postDetails as Success<LobstersPostDetails>).data,
|
details = (postDetails as Success<LobstersPostDetails>).data,
|
||||||
getLinkMetadata = getLinkMetadata,
|
|
||||||
postActions = postActions,
|
postActions = postActions,
|
||||||
htmlConverter = htmlConverter,
|
htmlConverter = htmlConverter,
|
||||||
modifier = modifier.fillMaxSize(),
|
modifier = modifier.fillMaxSize(),
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
package dev.msfjarvis.claw.common.posts
|
package dev.msfjarvis.claw.common.posts
|
||||||
|
|
||||||
import dev.msfjarvis.claw.database.local.SavedPost
|
import dev.msfjarvis.claw.database.local.SavedPost
|
||||||
|
import dev.msfjarvis.claw.model.LinkMetadata
|
||||||
|
import dev.msfjarvis.claw.model.LobstersPostDetails
|
||||||
|
|
||||||
interface PostActions {
|
interface PostActions {
|
||||||
fun viewPost(postUrl: String, commentsUrl: String)
|
fun viewPost(postUrl: String, commentsUrl: String)
|
||||||
fun viewComments(postId: String)
|
fun viewComments(postId: String)
|
||||||
fun viewCommentsPage(commentsUrl: String)
|
fun viewCommentsPage(commentsUrl: String)
|
||||||
fun toggleSave(post: SavedPost)
|
fun toggleSave(post: SavedPost)
|
||||||
|
suspend fun getComments(postId: String): LobstersPostDetails
|
||||||
|
suspend fun getLinkMetadata(url: String): LinkMetadata
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue