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( internal data class CommentNode(
val comment: Comment, val comment: Comment,
val isPostAuthor: Boolean,
private var parent: CommentNode? = null, private var parent: CommentNode? = null,
val children: MutableList<CommentNode> = mutableListOf(), val children: MutableList<CommentNode> = mutableListOf(),
val isUnread: Boolean = false, val isUnread: Boolean = false,
@ -27,6 +28,7 @@ internal data class CommentNode(
?.addChild( ?.addChild(
CommentNode( CommentNode(
comment = child.comment, comment = child.comment,
isPostAuthor = child.isPostAuthor,
parent = child.parent, parent = child.parent,
isUnread = child.isUnread, isUnread = child.isUnread,
indentLevel = child.indentLevel + 1, indentLevel = child.indentLevel + 1,

View file

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

View file

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

View file

@ -36,4 +36,5 @@ class LobstersPostDetails(
@SerialName("submitter_user") val submitter: String, @SerialName("submitter_user") val submitter: String,
val tags: List<String>, val tags: List<String>,
val comments: List<Comment>, val comments: List<Comment>,
@SerialName("user_is_author") val userIsAuthor: Boolean = false,
) )

View file

@ -27,7 +27,7 @@ data class UIPost(
@SerialName("submitter_user") val submitter: String, @SerialName("submitter_user") val submitter: String,
val tags: List<String>, val tags: List<String>,
val comments: List<Comment> = emptyList(), val comments: List<Comment> = emptyList(),
val userIsAuthor: Boolean = false, @SerialName("user_is_author") val userIsAuthor: Boolean = false,
) { ) {
@KonvertFrom( @KonvertFrom(
value = SavedPost::class, value = SavedPost::class,