feat(common): use a simplified search algorithm

This commit is contained in:
Harsh Shandilya 2022-12-12 11:23:10 +05:30
parent 2ab69de686
commit 450601f26b
No known key found for this signature in database
2 changed files with 13 additions and 3 deletions

View file

@ -49,6 +49,15 @@ fun toggleAllExpanded(commentNode: CommentNode): CommentNode {
return commentNode
}
fun findTopMostParent(node: CommentNode): CommentNode {
val parent = node.parent
return if (parent != null) {
findTopMostParent(parent)
} else {
node
}
}
fun LazyListScope.nodes(
nodes: List<CommentNode>,
htmlConverter: HTMLConverter,

View file

@ -63,12 +63,13 @@ private fun CommentsPageInternal(
nodes = commentNodes,
htmlConverter = htmlConverter,
toggleExpanded = { node ->
// TODO(anunaym14): make this search recursive
val newNode = toggleAllExpanded(node)
val parent = findTopMostParent(newNode)
val index =
commentNodes.indexOf(commentNodes.find { it.comment.url == node.comment.url })
commentNodes.indexOf(commentNodes.find { it.comment.url == parent.comment.url })
if (index != -1) {
commentNodes.removeAt(index)
commentNodes.add(index, toggleAllExpanded(node))
commentNodes.add(index, parent)
}
},
)