diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/LobstersPostsScreen.kt b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/LobstersPostsScreen.kt index a971c262..19591002 100644 --- a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/LobstersPostsScreen.kt +++ b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/LobstersPostsScreen.kt @@ -211,8 +211,6 @@ fun LobstersPostsScreen( CommentsPage( postId = dest.postId, postActions = postActions, - getSeenComments = viewModel::getSeenComments, - markSeenComments = viewModel::markSeenComments, contentPadding = contentPadding, openUserProfile = { clawBackStack.add(User(it)) }, ) diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/ClawViewModel.kt b/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/ClawViewModel.kt index c54cd8e1..e38d83f8 100644 --- a/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/ClawViewModel.kt +++ b/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/ClawViewModel.kt @@ -30,7 +30,6 @@ import dev.msfjarvis.claw.android.paging.SearchPagingSource import dev.msfjarvis.claw.api.LobstersApi import dev.msfjarvis.claw.core.injection.IODispatcher import dev.msfjarvis.claw.core.injection.MainDispatcher -import dev.msfjarvis.claw.model.Comment import dev.msfjarvis.claw.model.UIPost import dev.msfjarvis.claw.model.fromSavedPost import java.io.InputStream @@ -52,7 +51,6 @@ class ClawViewModel @Inject constructor( private val api: LobstersApi, - private val commentsRepository: CommentsRepository, private val readPostsRepository: ReadPostsRepository, private val savedPostsRepository: SavedPostsRepository, private val linkMetadataRepository: LinkMetadataRepository, @@ -136,12 +134,6 @@ constructor( return _savedPosts.contains(post.shortId) } - suspend fun getSeenComments(postId: String) = commentsRepository.getSeenComments(postId) - - fun markSeenComments(postId: String, comments: List) { - viewModelScope.launch { commentsRepository.markSeenComments(postId, comments) } - } - suspend fun getLinkMetadata(url: String) = withContext(ioDispatcher) { linkMetadataRepository.getLinkMetadata(url) } diff --git a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsPage.kt b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsPage.kt index bd6916d3..7791601f 100644 --- a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsPage.kt +++ b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsPage.kt @@ -23,7 +23,6 @@ import dev.msfjarvis.claw.common.posts.PostActions import dev.msfjarvis.claw.common.ui.NetworkError import dev.msfjarvis.claw.common.ui.ProgressBar import dev.msfjarvis.claw.database.local.PostComments -import dev.msfjarvis.claw.model.Comment import dev.msfjarvis.claw.model.UIPost @Suppress("UNCHECKED_CAST") @@ -31,8 +30,6 @@ import dev.msfjarvis.claw.model.UIPost fun CommentsPage( postId: String, postActions: PostActions, - getSeenComments: suspend (String) -> PostComments?, - markSeenComments: (String, List) -> Unit, contentPadding: PaddingValues, openUserProfile: (String) -> Unit, modifier: Modifier = Modifier, @@ -41,7 +38,7 @@ fun CommentsPage( LaunchedEffect(postId) { viewModel.loadPostDetails(postId) } val commentState by produceState(initialValue = null, key1 = postId) { - value = getSeenComments(postId) + value = viewModel.getSeenComments(postId) } when (val postDetails = viewModel.postDetails) { @@ -50,7 +47,7 @@ fun CommentsPage( details = (postDetails as Success).data, postActions = postActions, commentState = commentState, - markSeenComments = markSeenComments, + markSeenComments = viewModel::markSeenComments, openUserProfile = openUserProfile, contentPadding = contentPadding, modifier = modifier.fillMaxSize(), diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/CommentsRepository.kt b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsRepository.kt similarity index 96% rename from android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/CommentsRepository.kt rename to common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsRepository.kt index 6959beea..e801d7d4 100644 --- a/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/CommentsRepository.kt +++ b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsRepository.kt @@ -4,7 +4,7 @@ * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. */ -package dev.msfjarvis.claw.android.viewmodel +package dev.msfjarvis.claw.common.comments import dev.msfjarvis.claw.core.injection.DatabaseReadDispatcher import dev.msfjarvis.claw.core.injection.DatabaseWriteDispatcher diff --git a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsViewModel.kt b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsViewModel.kt index 917f6046..9e5e96eb 100644 --- a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsViewModel.kt +++ b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsViewModel.kt @@ -12,6 +12,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.viewModelScope import com.deliveryhero.whetstone.app.ApplicationScope import com.deliveryhero.whetstone.viewmodel.ContributesViewModel import com.github.michaelbull.result.coroutines.runSuspendCatching @@ -23,11 +24,13 @@ import dev.msfjarvis.claw.api.LobstersApi import dev.msfjarvis.claw.api.toError import dev.msfjarvis.claw.common.NetworkState import dev.msfjarvis.claw.core.injection.IODispatcher +import dev.msfjarvis.claw.model.Comment import dev.msfjarvis.claw.model.UIPost import dev.msfjarvis.claw.model.toUIPost import java.io.IOException import javax.inject.Inject import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @ContributesViewModel @@ -35,6 +38,7 @@ class CommentsViewModel @Inject constructor( private val api: LobstersApi, + private val commentsRepository: CommentsRepository, @IODispatcher private val ioDispatcher: CoroutineDispatcher, @ForScope(ApplicationScope::class) context: Context, ) : AndroidViewModel(context as Application) { @@ -58,4 +62,10 @@ constructor( failure = { NetworkState.Error(error = it, description = "Failed to load comments") }, ) } + + suspend fun getSeenComments(postId: String) = commentsRepository.getSeenComments(postId) + + fun markSeenComments(postId: String, comments: List) { + viewModelScope.launch { commentsRepository.markSeenComments(postId, comments) } + } } diff --git a/common/src/main/kotlin/dev/msfjarvis/claw/common/posts/LobstersCard.kt b/common/src/main/kotlin/dev/msfjarvis/claw/common/posts/LobstersCard.kt index 0b534ca9..0be00bea 100644 --- a/common/src/main/kotlin/dev/msfjarvis/claw/common/posts/LobstersCard.kt +++ b/common/src/main/kotlin/dev/msfjarvis/claw/common/posts/LobstersCard.kt @@ -269,21 +269,6 @@ val TEST_POST_ACTIONS = return true } - override suspend fun getComments(postId: String): UIPost { - return UIPost( - shortId = "ooga", - title = "Simple Anomaly Detection Using Plain SQL", - url = "https://hakibenita.com/sql-anomaly-detection", - createdAt = "2020-09-21T08:04:24.000-05:00", - commentCount = 1, - commentsUrl = "https://lobste.rs/s/q1hh1g/simple_anomaly_detection_using_plain_sql", - tags = listOf("databases", "apis"), - description = "", - submitter = "Haki", - comments = emptyList(), - ) - } - override suspend fun getLinkMetadata(url: String): LinkMetadata { return LinkMetadata("", "") }