refactor(common): move setExpanded to CommentNode

This commit is contained in:
Harsh Shandilya 2024-06-13 20:08:10 +05:30
parent 7f2b052df5
commit c4aad66c03
2 changed files with 11 additions and 10 deletions

View file

@ -19,6 +19,7 @@ internal data class CommentNode(
var isExpanded: Boolean = true,
var indentLevel: Int,
) {
fun addChild(child: CommentNode) {
if (comment.shortId == child.comment.parentComment) {
children.add(child)
@ -28,6 +29,15 @@ internal data class CommentNode(
children.lastOrNull()?.addChild(child)
}
}
fun setExpanded(expanded: Boolean): CommentNode {
this.isExpanded = expanded
if (children.isNotEmpty()) {
children.forEach { it.setExpanded(expanded) }
}
return this
}
}
internal fun createListNode(
@ -62,15 +72,6 @@ internal fun createListNode(
return commentNodes
}
internal fun setExpanded(commentNode: CommentNode, expanded: Boolean): CommentNode {
commentNode.isExpanded = expanded
if (commentNode.children.isNotEmpty()) {
commentNode.children.forEach { setExpanded(it, expanded) }
}
return commentNode
}
internal tailrec fun findTopMostParent(node: CommentNode): CommentNode {
val parent = node.parent
return if (parent != null) {

View file

@ -76,7 +76,7 @@ private fun CommentsPageInternal(
nodes = commentNodes,
htmlConverter = htmlConverter,
toggleExpanded = { node ->
val newNode = setExpanded(node, !node.isExpanded)
val newNode = node.setExpanded(!node.isExpanded)
val parent = findTopMostParent(newNode)
val index =
commentNodes.indexOf(commentNodes.find { it.comment.url == parent.comment.url })