diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/CommentsRepository.kt b/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/CommentsRepository.kt index e0ad9839..55bc4f29 100644 --- a/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/CommentsRepository.kt +++ b/android/src/main/kotlin/dev/msfjarvis/claw/android/viewmodel/CommentsRepository.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2021-2023 Harsh Shandilya. + * Copyright © 2021-2024 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. @@ -22,7 +22,10 @@ constructor( ) { suspend fun getSeenComments(postId: String) = - withContext(dbDispatcher) { postCommentsQueries.getCommentIds(postId).executeAsOneOrNull() } + withContext(dbDispatcher) { + postCommentsQueries.getCommentIds(postId).executeAsOneOrNull() + ?: PostComments(postId = postId, commentIds = emptyList()) + } suspend fun markSeenComments(postId: String, comments: List) { withContext(dbDispatcher) { 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 bedfa5a2..86b4e027 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 @@ -38,10 +38,10 @@ internal data class CommentNode( internal fun createListNode( comments: List, - commentState: PostComments?, + commentState: PostComments, ): MutableList { val commentNodes = mutableListOf() - val isUnread = { id: String -> commentState?.commentIds?.contains(id) == false } + val isUnread = { id: String -> !commentState.commentIds.contains(id) } for (i in comments.indices) { if (comments[i].parentComment == null) { 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 27d5da3e..d0ff3ae6 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 @@ -32,7 +32,7 @@ fun CommentsPage( postId: String, postActions: PostActions, htmlConverter: HTMLConverter, - getSeenComments: suspend (String) -> PostComments?, + getSeenComments: suspend (String) -> PostComments, markSeenComments: (String, List) -> Unit, modifier: Modifier = Modifier, openUserProfile: (String) -> Unit, @@ -46,7 +46,9 @@ fun CommentsPage( ) } val commentState by - produceState(initialValue = null) { value = getSeenComments(postId) } + produceState(initialValue = PostComments(postId, emptyList())) { + value = getSeenComments(postId) + } when (postDetails) { is Success<*> -> { diff --git a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsPageImpl.kt b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsPageImpl.kt index d451e340..797288fb 100644 --- a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsPageImpl.kt +++ b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsPageImpl.kt @@ -49,7 +49,7 @@ internal fun CommentsPageInternal( details: UIPost, postActions: PostActions, htmlConverter: HTMLConverter, - commentState: PostComments?, + commentState: PostComments, markSeenComments: (String, List) -> Unit, openUserProfile: (String) -> Unit, modifier: Modifier = Modifier, @@ -57,8 +57,8 @@ internal fun CommentsPageInternal( val context = LocalContext.current val commentNodes = createListNode(details.comments, commentState) LaunchedEffect(key1 = commentNodes) { - if (details.comments.isNotEmpty() && !commentState?.commentIds.isNullOrEmpty()) { - val unreadCount = details.comments.size - (commentState?.commentIds?.size ?: 0) + if (details.comments.isNotEmpty() && commentState.commentIds.isNotEmpty()) { + val unreadCount = details.comments.size - commentState.commentIds.size if (unreadCount > 0) { val text = "$unreadCount unread comments" Toast.makeText(context, text, Toast.LENGTH_SHORT).show()