mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 22:17:03 +05:30
refactor(common): optimize comment entry display
This commit is contained in:
parent
18d82a5eb3
commit
0083aa69b5
2 changed files with 45 additions and 24 deletions
|
@ -6,8 +6,8 @@
|
|||
*/
|
||||
package dev.msfjarvis.claw.common.comments
|
||||
|
||||
import androidx.compose.foundation.lazy.LazyListScope
|
||||
import androidx.compose.material3.Divider
|
||||
import androidx.compose.runtime.Composable
|
||||
import dev.msfjarvis.claw.model.Comment
|
||||
|
||||
data class CommentNode(
|
||||
|
@ -40,22 +40,6 @@ fun createListNode(comments: List<Comment>): MutableList<CommentNode> {
|
|||
return commentNodes
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun DisplayListNode(
|
||||
comments: List<CommentNode>,
|
||||
htmlConverter: HTMLConverter,
|
||||
updateComments: (CommentNode) -> Unit,
|
||||
) {
|
||||
comments.forEach {
|
||||
CommentEntry(commentNode = it, htmlConverter = htmlConverter, updateComments)
|
||||
Divider()
|
||||
|
||||
if (it.children.isNotEmpty()) {
|
||||
DisplayListNode(comments = it.children, htmlConverter = htmlConverter, updateComments)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun toggleAllExpanded(commentNode: CommentNode): CommentNode {
|
||||
commentNode.isExpanded = !commentNode.isExpanded
|
||||
|
||||
|
@ -64,3 +48,39 @@ fun toggleAllExpanded(commentNode: CommentNode): CommentNode {
|
|||
}
|
||||
return commentNode
|
||||
}
|
||||
|
||||
fun LazyListScope.nodes(
|
||||
nodes: List<CommentNode>,
|
||||
htmlConverter: HTMLConverter,
|
||||
toggleExpanded: (CommentNode) -> Unit,
|
||||
) {
|
||||
nodes.forEach { node ->
|
||||
node(
|
||||
node = node,
|
||||
htmlConverter = htmlConverter,
|
||||
toggleExpanded = toggleExpanded,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun LazyListScope.node(
|
||||
node: CommentNode,
|
||||
htmlConverter: HTMLConverter,
|
||||
toggleExpanded: (CommentNode) -> Unit,
|
||||
) {
|
||||
item {
|
||||
CommentEntry(
|
||||
commentNode = node,
|
||||
htmlConverter = htmlConverter,
|
||||
toggleExpanded = toggleExpanded,
|
||||
)
|
||||
Divider()
|
||||
}
|
||||
if (node.children.isNotEmpty()) {
|
||||
nodes(
|
||||
node.children,
|
||||
htmlConverter = htmlConverter,
|
||||
toggleExpanded = toggleExpanded,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,19 +59,20 @@ private fun CommentsPageInternal(
|
|||
)
|
||||
}
|
||||
|
||||
item {
|
||||
DisplayListNode(comments = commentNodes, htmlConverter = htmlConverter) { node ->
|
||||
nodes(
|
||||
nodes = commentNodes,
|
||||
htmlConverter = htmlConverter,
|
||||
toggleExpanded = { node ->
|
||||
val newNode = toggleAllExpanded(node)
|
||||
// TODO(anunaym14): make this search recursive
|
||||
val index =
|
||||
commentNodes.indexOf(commentNodes.find { it.comment.url == node.comment.url })
|
||||
if (index == -1) {
|
||||
error("Failed to find node for comment: ${node.comment}")
|
||||
} else {
|
||||
if (index != -1) {
|
||||
commentNodes.removeAt(index)
|
||||
commentNodes.add(index, newNode)
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
} else {
|
||||
item {
|
||||
Text(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue