refactor: pull down comments stuff into common

This commit is contained in:
Harsh Shandilya 2025-06-01 13:54:26 +05:30
parent 0d47787a73
commit 7c9f833ade
6 changed files with 13 additions and 31 deletions

View file

@ -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)) },
)

View file

@ -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<Comment>) {
viewModelScope.launch { commentsRepository.markSeenComments(postId, comments) }
}
suspend fun getLinkMetadata(url: String) =
withContext(ioDispatcher) { linkMetadataRepository.getLinkMetadata(url) }

View file

@ -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<Comment>) -> Unit,
contentPadding: PaddingValues,
openUserProfile: (String) -> Unit,
modifier: Modifier = Modifier,
@ -41,7 +38,7 @@ fun CommentsPage(
LaunchedEffect(postId) { viewModel.loadPostDetails(postId) }
val commentState by
produceState<PostComments?>(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<UIPost>).data,
postActions = postActions,
commentState = commentState,
markSeenComments = markSeenComments,
markSeenComments = viewModel::markSeenComments,
openUserProfile = openUserProfile,
contentPadding = contentPadding,
modifier = modifier.fillMaxSize(),

View file

@ -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

View file

@ -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<Comment>) {
viewModelScope.launch { commentsRepository.markSeenComments(postId, comments) }
}
}

View file

@ -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("", "")
}