feat(common): visually distinguish between read and unread comments

This commit is contained in:
Harsh Shandilya 2024-07-07 23:42:19 +05:30
parent 05a8b9bc67
commit a53af77705
2 changed files with 17 additions and 15 deletions

View file

@ -30,11 +30,8 @@ import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import com.github.michaelbull.result.coroutines.runSuspendCatching
import com.github.michaelbull.result.onSuccess
@ -140,7 +137,10 @@ internal fun CommentEntry(
modifier
.fillMaxWidth()
.clickable { toggleExpanded(commentNode) }
.background(MaterialTheme.colorScheme.background)
.background(
if (commentNode.isUnread) MaterialTheme.colorScheme.surfaceBright
else MaterialTheme.colorScheme.background
)
.padding(
start = CommentEntryPadding * commentNode.indentLevel,
end = CommentEntryPadding,
@ -154,7 +154,6 @@ internal fun CommentEntry(
buildCommenterString(
commenterName = comment.user,
score = comment.score,
isUnread = commentNode.isUnread,
createdAt = comment.createdAt,
updatedAt = comment.updatedAt,
),
@ -176,7 +175,6 @@ internal fun CommentEntry(
fun buildCommenterString(
commenterName: String,
score: Int,
isUnread: Boolean,
createdAt: TemporalAccessor,
updatedAt: TemporalAccessor,
): AnnotatedString {
@ -215,13 +213,5 @@ fun buildCommenterString(
append(updatedRelative.toString())
append(')')
}
if (isUnread) {
append(' ')
withStyle(
style = SpanStyle(fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.error)
) {
append("(unread)")
}
}
}
}

View file

@ -6,6 +6,7 @@
*/
package dev.msfjarvis.claw.common.comments
import android.widget.Toast
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
@ -22,6 +23,7 @@ import androidx.compose.runtime.produceState
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
@ -49,8 +51,18 @@ private fun CommentsPageInternal(
openUserProfile: (String) -> Unit,
modifier: Modifier = Modifier,
) {
val context = LocalContext.current
val commentNodes = createListNode(details.comments, commentState).toMutableStateList()
LaunchedEffect(key1 = commentNodes) { markSeenComments(details.shortId, details.comments) }
LaunchedEffect(key1 = commentNodes) {
if (details.comments.isNotEmpty() && !commentState?.commentIds.isNullOrEmpty()) {
val unreadCount = details.comments.size - (commentState?.commentIds?.size ?: 0)
if (unreadCount > 0) {
val text = "$unreadCount unread comments"
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
}
}
markSeenComments(details.shortId, details.comments)
}
Surface(color = MaterialTheme.colorScheme.surfaceVariant) {
LazyColumn(modifier = modifier, contentPadding = PaddingValues(bottom = 24.dp)) {