feat: highlight story author in comments

This commit is contained in:
Harsh Shandilya 2024-11-14 01:15:54 +05:30
parent 7bdaf691df
commit 5db68ba186
5 changed files with 31 additions and 9 deletions

View file

@ -10,6 +10,7 @@ import dev.msfjarvis.claw.model.Comment
internal data class CommentNode(
val comment: Comment,
val isPostAuthor: Boolean,
private var parent: CommentNode? = null,
val children: MutableList<CommentNode> = mutableListOf(),
val isUnread: Boolean = false,
@ -27,6 +28,7 @@ internal data class CommentNode(
?.addChild(
CommentNode(
comment = child.comment,
isPostAuthor = child.isPostAuthor,
parent = child.parent,
isUnread = child.isUnread,
indentLevel = child.indentLevel + 1,

View file

@ -17,16 +17,22 @@ internal class CommentsHandler {
private val _listItems: MutableStateFlow<List<CommentNode>> = MutableStateFlow(emptyList())
val listItems: StateFlow<List<CommentNode>> = _listItems.asStateFlow()
fun createListNode(comments: List<Comment>, commentState: PostComments?) {
fun createListNode(
comments: List<Comment>,
commentState: PostComments?,
isPostAuthor: (Comment) -> Boolean,
) {
val commentNodes = mutableListOf<CommentNode>()
val isUnread = { id: String -> commentState?.commentIds?.contains(id) == false }
for (i in comments.indices) {
if (comments[i].parentComment == null) {
val comment = comments[i]
if (comment.parentComment == null) {
commentNodes.add(
CommentNode(
comment = comments[i],
isUnread = isUnread(comments[i].shortId),
comment = comment,
isPostAuthor = isPostAuthor(comment),
isUnread = isUnread(comment.shortId),
indentLevel = 1,
)
)
@ -34,8 +40,9 @@ internal class CommentsHandler {
commentNodes.lastOrNull()?.let { commentNode ->
commentNode.addChild(
CommentNode(
comment = comments[i],
isUnread = isUnread(comments[i].shortId),
comment = comment,
isPostAuthor = isPostAuthor(comment),
isUnread = isUnread(comment.shortId),
indentLevel = commentNode.indentLevel + 1,
)
)

View file

@ -38,11 +38,14 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
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.TextAlign
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import dev.msfjarvis.claw.common.posts.PostActions
@ -69,7 +72,9 @@ internal fun CommentsPageInternal(
) {
val commentsHandler = CommentsHandler()
LaunchedEffect(key1 = details, key2 = commentState) {
commentsHandler.createListNode(details.comments, commentState)
commentsHandler.createListNode(details.comments, commentState) { comment ->
details.userIsAuthor && comment.user == details.submitter
}
}
val onToggleExpandedState = { shortId: String, isExpanded: Boolean ->
@ -221,6 +226,8 @@ private fun CommentEntry(
score = comment.score,
createdAt = comment.createdAt,
updatedAt = comment.updatedAt,
nameColorOverride =
if (commentNode.isPostAuthor) MaterialTheme.colorScheme.tertiary else null,
),
avatarUrl = "https://lobste.rs/avatars/${comment.user}-100.png",
contentDescription = "User avatar for ${comment.user}",
@ -241,6 +248,7 @@ private fun buildCommenterString(
score: Int,
createdAt: TemporalAccessor,
updatedAt: TemporalAccessor,
nameColorOverride: Color? = null,
): AnnotatedString {
val now = System.currentTimeMillis()
val createdRelative =
@ -256,7 +264,11 @@ private fun buildCommenterString(
DateUtils.MINUTE_IN_MILLIS,
)
return buildAnnotatedString {
append(commenterName)
if (nameColorOverride != null) {
withStyle(SpanStyle(color = nameColorOverride)) { append(commenterName) }
} else {
append(commenterName)
}
append(' ')
append('•')
append(' ')