From d55ff24add1a9b78101e9f0a18da95e1b08fc743 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 21 Aug 2024 12:55:33 +0530 Subject: [PATCH] refactor(common): cleanup unused stuff from tree view change Follows up from changes in 6223198191d70f704a517d8241a96c242e97e40b --- .../claw/common/comments/CommentEntry.kt | 100 ------------------ .../claw/common/comments/CommentNode.kt | 1 - .../claw/common/comments/CommentsPageImpl.kt | 45 ++++++++ 3 files changed, 45 insertions(+), 101 deletions(-) diff --git a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentEntry.kt b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentEntry.kt index 26b2a441..17cc2921 100644 --- a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentEntry.kt +++ b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentEntry.kt @@ -6,7 +6,6 @@ */ package dev.msfjarvis.claw.common.comments -import android.text.format.DateUtils import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement @@ -27,10 +26,8 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue 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.buildAnnotatedString import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import com.github.michaelbull.result.coroutines.runSuspendCatching @@ -43,8 +40,6 @@ import dev.msfjarvis.claw.common.ui.NetworkImage import dev.msfjarvis.claw.common.ui.ThemedRichText import dev.msfjarvis.claw.model.LinkMetadata import dev.msfjarvis.claw.model.UIPost -import java.time.Instant -import java.time.temporal.TemporalAccessor import kotlinx.collections.immutable.toImmutableList @Composable @@ -120,98 +115,3 @@ private fun PostLink(linkMetadata: LinkMetadata, modifier: Modifier = Modifier) } } } - -private val CommentEntryPadding = 16f.dp - -@Composable -internal fun CommentEntry( - commentNode: CommentNode, - htmlConverter: HTMLConverter, - toggleExpanded: (CommentNode) -> Unit, - openUserProfile: (String) -> Unit, - modifier: Modifier = Modifier, -) { - val comment = commentNode.comment - Box( - modifier = - modifier - .fillMaxWidth() - .clickable { toggleExpanded(commentNode) } - .background( - if (commentNode.isUnread) MaterialTheme.colorScheme.surfaceContainerHigh - else MaterialTheme.colorScheme.background - ) - .padding( - start = CommentEntryPadding * commentNode.indentLevel, - end = CommentEntryPadding, - top = CommentEntryPadding, - bottom = CommentEntryPadding, - ) - ) { - Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { - Submitter( - text = - buildCommenterString( - commenterName = comment.user, - score = comment.score, - createdAt = comment.createdAt, - updatedAt = comment.updatedAt, - ), - avatarUrl = "https://lobste.rs/avatars/${comment.user}-100.png", - contentDescription = "User avatar for ${comment.user}", - modifier = Modifier.clickable { openUserProfile(comment.user) }, - ) - if (commentNode.isExpanded) { - ThemedRichText( - text = htmlConverter.convertHTMLToMarkdown(comment.comment), - modifier = Modifier.padding(top = 8.dp), - ) - } - } - } -} - -@Composable -fun buildCommenterString( - commenterName: String, - score: Int, - createdAt: TemporalAccessor, - updatedAt: TemporalAccessor, -): AnnotatedString { - val now = System.currentTimeMillis() - val createdRelative = - remember(createdAt) { - DateUtils.getRelativeTimeSpanString( - Instant.from(createdAt).toEpochMilli(), - now, - DateUtils.MINUTE_IN_MILLIS, - ) - } - val updatedRelative = - remember(updatedAt) { - DateUtils.getRelativeTimeSpanString( - Instant.from(updatedAt).toEpochMilli(), - now, - DateUtils.MINUTE_IN_MILLIS, - ) - } - return buildAnnotatedString { - append(commenterName) - append(' ') - append('•') - append(' ') - append("$score points") - append(' ') - append('•') - append(' ') - append(createdRelative.toString()) - if (updatedRelative != createdRelative) { - append(' ') - append('(') - append("Updated") - append(' ') - append(updatedRelative.toString()) - append(')') - } - } -} 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 0955ae9b..b8a3a028 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 @@ -14,7 +14,6 @@ internal data class CommentNode( var parent: CommentNode? = null, val children: MutableList = mutableListOf(), val isUnread: Boolean = false, - var isExpanded: Boolean = true, var indentLevel: Int, ) { 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 78a0045f..d451e340 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 @@ -6,6 +6,7 @@ */ package dev.msfjarvis.claw.common.comments +import android.text.format.DateUtils import android.widget.Toast import androidx.compose.foundation.background import androidx.compose.foundation.clickable @@ -29,6 +30,8 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.text.AnnotatedString +import androidx.compose.ui.text.buildAnnotatedString import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp @@ -38,6 +41,8 @@ import dev.msfjarvis.claw.common.ui.ThemedRichText import dev.msfjarvis.claw.database.local.PostComments import dev.msfjarvis.claw.model.Comment import dev.msfjarvis.claw.model.UIPost +import java.time.Instant +import java.time.temporal.TemporalAccessor @Composable internal fun CommentsPageInternal( @@ -188,3 +193,43 @@ private fun CommentEntry( } } } + +private fun buildCommenterString( + commenterName: String, + score: Int, + createdAt: TemporalAccessor, + updatedAt: TemporalAccessor, +): AnnotatedString { + val now = System.currentTimeMillis() + val createdRelative = + DateUtils.getRelativeTimeSpanString( + Instant.from(createdAt).toEpochMilli(), + now, + DateUtils.MINUTE_IN_MILLIS, + ) + val updatedRelative = + DateUtils.getRelativeTimeSpanString( + Instant.from(updatedAt).toEpochMilli(), + now, + DateUtils.MINUTE_IN_MILLIS, + ) + return buildAnnotatedString { + append(commenterName) + append(' ') + append('•') + append(' ') + append("$score points") + append(' ') + append('•') + append(' ') + append(createdRelative.toString()) + if (updatedRelative != createdRelative) { + append(' ') + append('(') + append("Updated") + append(' ') + append(updatedRelative.toString()) + append(')') + } + } +}