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.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString 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.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.github.michaelbull.result.coroutines.runSuspendCatching import com.github.michaelbull.result.coroutines.runSuspendCatching
import com.github.michaelbull.result.onSuccess import com.github.michaelbull.result.onSuccess
@ -140,7 +137,10 @@ internal fun CommentEntry(
modifier modifier
.fillMaxWidth() .fillMaxWidth()
.clickable { toggleExpanded(commentNode) } .clickable { toggleExpanded(commentNode) }
.background(MaterialTheme.colorScheme.background) .background(
if (commentNode.isUnread) MaterialTheme.colorScheme.surfaceBright
else MaterialTheme.colorScheme.background
)
.padding( .padding(
start = CommentEntryPadding * commentNode.indentLevel, start = CommentEntryPadding * commentNode.indentLevel,
end = CommentEntryPadding, end = CommentEntryPadding,
@ -154,7 +154,6 @@ internal fun CommentEntry(
buildCommenterString( buildCommenterString(
commenterName = comment.user, commenterName = comment.user,
score = comment.score, score = comment.score,
isUnread = commentNode.isUnread,
createdAt = comment.createdAt, createdAt = comment.createdAt,
updatedAt = comment.updatedAt, updatedAt = comment.updatedAt,
), ),
@ -176,7 +175,6 @@ internal fun CommentEntry(
fun buildCommenterString( fun buildCommenterString(
commenterName: String, commenterName: String,
score: Int, score: Int,
isUnread: Boolean,
createdAt: TemporalAccessor, createdAt: TemporalAccessor,
updatedAt: TemporalAccessor, updatedAt: TemporalAccessor,
): AnnotatedString { ): AnnotatedString {
@ -215,13 +213,5 @@ fun buildCommenterString(
append(updatedRelative.toString()) append(updatedRelative.toString())
append(')') 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 package dev.msfjarvis.claw.common.comments
import android.widget.Toast
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
@ -22,6 +23,7 @@ import androidx.compose.runtime.produceState
import androidx.compose.runtime.toMutableStateList import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -49,8 +51,18 @@ private fun CommentsPageInternal(
openUserProfile: (String) -> Unit, openUserProfile: (String) -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
) { ) {
val context = LocalContext.current
val commentNodes = createListNode(details.comments, commentState).toMutableStateList() 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) { Surface(color = MaterialTheme.colorScheme.surfaceVariant) {
LazyColumn(modifier = modifier, contentPadding = PaddingValues(bottom = 24.dp)) { LazyColumn(modifier = modifier, contentPadding = PaddingValues(bottom = 24.dp)) {