diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/LobstersApp.kt b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/LobstersApp.kt index 6e44731b..6895491b 100644 --- a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/LobstersApp.kt +++ b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/LobstersApp.kt @@ -217,6 +217,8 @@ fun LobstersApp( postId = postId, postActions = postActions, htmlConverter = htmlConverter, + getSeenComments = viewModel::getSeenComments, + markSeenComments = viewModel::markSeenComments, ) } composable( diff --git a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentNode.kt b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentNode.kt index d452cc2b..439abc2b 100644 --- a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentNode.kt +++ b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentNode.kt @@ -28,14 +28,20 @@ internal data class CommentNode( } } -internal fun createListNode(comments: List): MutableList { +internal fun createListNode( + comments: List, + commentState: PostComments? +): MutableList { val commentNodes = mutableListOf() + val isUnread = { id: String -> commentState?.commentIds?.contains(id) == false } for (i in comments.indices) { if (comments[i].indentLevel == 1) { - commentNodes.add(CommentNode(comment = comments[i])) + commentNodes.add(CommentNode(comment = comments[i], isUnread = isUnread(comments[i].shortId))) } else { - commentNodes.last().addChild(CommentNode(comment = comments[i])) + commentNodes + .last() + .addChild(CommentNode(comment = comments[i], isUnread = isUnread(comments[i].shortId))) } } diff --git a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/Comments.kt b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/Comments.kt index 0cc154e4..5da029b1 100644 --- a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/Comments.kt +++ b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/Comments.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2021-2022 Harsh Shandilya. + * Copyright © 2021-2023 Harsh Shandilya. * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. @@ -16,6 +16,7 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.produceState import androidx.compose.runtime.toMutableStateList @@ -31,16 +32,22 @@ import dev.msfjarvis.claw.common.NetworkState.Success 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.LobstersPostDetails +@Suppress("LongParameterList") @Composable private fun CommentsPageInternal( details: LobstersPostDetails, postActions: PostActions, htmlConverter: HTMLConverter, + commentState: PostComments?, + markSeenComments: (String, List) -> Unit, modifier: Modifier = Modifier, ) { - val commentNodes = createListNode(details.comments).toMutableStateList() + val commentNodes = createListNode(details.comments, commentState).toMutableStateList() + LaunchedEffect(key1 = commentNodes) { markSeenComments(details.shortId, details.comments) } Surface(color = MaterialTheme.colorScheme.surfaceVariant) { LazyColumn(modifier = modifier, contentPadding = PaddingValues(bottom = 24.dp)) { @@ -90,12 +97,14 @@ private fun CommentsPageInternal( } } -@Suppress("UNCHECKED_CAST") +@Suppress("UNCHECKED_CAST", "LongParameterList") @Composable fun CommentsPage( postId: String, postActions: PostActions, htmlConverter: HTMLConverter, + getSeenComments: suspend (String) -> PostComments?, + markSeenComments: (String, List) -> Unit, modifier: Modifier = Modifier, ) { val postDetails by @@ -106,6 +115,8 @@ fun CommentsPage( onFailure = { value = Error(error = it, description = "Failed to load comments") } ) } + val commentState by + produceState(initialValue = null) { value = getSeenComments(postId) } when (postDetails) { is Success<*> -> { @@ -113,6 +124,8 @@ fun CommentsPage( details = (postDetails as Success).data, postActions = postActions, htmlConverter = htmlConverter, + commentState = commentState, + markSeenComments = markSeenComments, modifier = modifier.fillMaxSize(), ) }