compose-lobsters/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentEntry.kt

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

230 lines
7.2 KiB
Kotlin
Raw Normal View History

/*
* Copyright © 2021-2024 Harsh Shandilya.
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
package dev.msfjarvis.claw.common.comments
import android.text.format.DateUtils
2022-02-28 21:14:49 +05:30
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
2022-02-28 21:14:49 +05:30
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
2022-02-28 21:14:49 +05:30
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
2022-02-28 21:14:49 +05:30
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
2022-08-19 23:40:18 +05:30
import androidx.compose.foundation.layout.size
2022-02-28 21:14:49 +05:30
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Public
2021-12-31 01:14:39 +05:30
import androidx.compose.material3.MaterialTheme
2021-12-30 10:49:21 +05:30
import androidx.compose.material3.Surface
2022-02-28 21:14:49 +05:30
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
2022-10-16 12:21:19 +05:30
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalUriHandler
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
2022-02-28 21:14:49 +05:30
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
2023-09-28 14:09:09 +05:30
import com.github.michaelbull.result.coroutines.runSuspendCatching
import com.github.michaelbull.result.onSuccess
2022-02-28 21:14:49 +05:30
import dev.msfjarvis.claw.common.posts.PostActions
import dev.msfjarvis.claw.common.posts.PostTitle
2022-02-20 15:23:26 +05:30
import dev.msfjarvis.claw.common.posts.Submitter
2022-02-28 21:14:49 +05:30
import dev.msfjarvis.claw.common.posts.TagRow
2022-08-19 23:40:18 +05:30
import dev.msfjarvis.claw.common.ui.NetworkImage
import dev.msfjarvis.claw.common.ui.ThemedRichText
2022-08-19 23:40:18 +05:30
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
internal fun CommentsHeader(
post: UIPost,
2022-02-28 21:14:49 +05:30
postActions: PostActions,
htmlConverter: HTMLConverter,
modifier: Modifier = Modifier,
) {
val uriHandler = LocalUriHandler.current
2022-10-16 12:21:19 +05:30
val linkMetadata by
produceState(initialValue = LinkMetadata(post.url, null)) {
runSuspendCatching { postActions.getLinkMetadata(post.url) }
.onSuccess { metadata -> value = metadata }
2022-10-16 12:21:19 +05:30
}
2022-02-28 21:14:49 +05:30
Surface(color = MaterialTheme.colorScheme.background, modifier = modifier) {
Column(
2022-02-28 21:14:49 +05:30
modifier = Modifier.padding(16.dp).fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
PostTitle(title = post.title, isRead = false)
TagRow(tags = post.tags.toImmutableList())
2022-02-28 21:14:49 +05:30
Spacer(Modifier.height(4.dp))
2022-10-16 12:21:19 +05:30
if (linkMetadata.url.isNotBlank()) {
2022-02-28 21:14:49 +05:30
PostLink(
2022-10-16 12:21:19 +05:30
linkMetadata = linkMetadata,
2022-02-28 21:14:49 +05:30
modifier =
Modifier.clickable {
postActions.viewPost(post.shortId, linkMetadata.url, post.commentsUrl)
},
2022-02-28 21:14:49 +05:30
)
Spacer(Modifier.height(4.dp))
}
if (post.description.isNotBlank()) {
ThemedRichText(htmlConverter.convertHTMLToMarkdown(post.description))
2022-02-28 21:14:49 +05:30
Spacer(Modifier.height(4.dp))
}
Submitter(
2024-03-16 09:48:08 +05:30
text = AnnotatedString("Submitted by ${post.submitter}"),
avatarUrl = "https://lobste.rs/avatars/${post.submitter}-100.png",
contentDescription = "User avatar for ${post.submitter}",
modifier =
2024-03-16 09:48:08 +05:30
Modifier.clickable { uriHandler.openUri("https://lobste.rs/u/${post.submitter}") },
2022-02-28 21:14:49 +05:30
)
}
}
}
@Composable
private fun PostLink(linkMetadata: LinkMetadata, modifier: Modifier = Modifier) {
2022-02-28 21:14:49 +05:30
Box(
modifier.background(
color = MaterialTheme.colorScheme.secondary,
shape = RoundedCornerShape(8.dp),
2022-02-28 21:14:49 +05:30
)
) {
Row(modifier = Modifier.padding(16.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
2022-12-09 18:18:02 +05:30
NetworkImage(
url = linkMetadata.faviconUrl,
placeholder = Icons.Filled.Public,
2022-12-09 18:18:02 +05:30
contentDescription = "",
modifier = Modifier.size(24.dp),
)
2022-02-28 21:14:49 +05:30
Text(
2022-08-19 23:40:18 +05:30
text = linkMetadata.url,
2022-02-28 21:14:49 +05:30
overflow = TextOverflow.Ellipsis,
maxLines = 1,
color = MaterialTheme.colorScheme.onSecondary,
style = MaterialTheme.typography.labelLarge,
)
}
}
}
private val CommentEntryPadding = 16f.dp
@Composable
internal fun CommentEntry(
commentNode: CommentNode,
htmlConverter: HTMLConverter,
toggleExpanded: (CommentNode) -> Unit,
modifier: Modifier = Modifier,
) {
val uriHandler = LocalUriHandler.current
val comment = commentNode.comment
2022-02-28 21:14:49 +05:30
Box(
modifier =
modifier
.fillMaxWidth()
.clickable { toggleExpanded(commentNode) }
2022-02-28 21:14:49 +05:30
.background(MaterialTheme.colorScheme.background)
.padding(
start = CommentEntryPadding * commentNode.indentLevel,
end = CommentEntryPadding,
top = CommentEntryPadding,
bottom = CommentEntryPadding,
)
2022-02-28 21:14:49 +05:30
) {
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
2022-02-20 15:23:26 +05:30
Submitter(
text =
buildCommenterString(
2024-03-16 09:48:08 +05:30
commenterName = comment.user,
score = comment.score,
isUnread = commentNode.isUnread,
createdAt = comment.createdAt,
updatedAt = comment.updatedAt,
),
2024-03-16 09:48:08 +05:30
avatarUrl = "https://lobste.rs/avatars/${comment.user}-100.png",
contentDescription = "User avatar for ${comment.user}",
modifier = Modifier.clickable { uriHandler.openUri("https://lobste.rs/u/${comment.user}") },
)
if (commentNode.isExpanded) {
ThemedRichText(
text = htmlConverter.convertHTMLToMarkdown(comment.comment),
modifier = Modifier.padding(top = 8.dp),
)
}
}
}
}
@Composable
fun buildCommenterString(
commenterName: String,
score: Int,
isUnread: Boolean,
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(')')
}
if (isUnread) {
append(' ')
withStyle(
style = SpanStyle(fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.error)
) {
append("(unread)")
}
}
}
}