From c4aad66c03973bbb6e29053088ba9ff59c4addd5 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Thu, 13 Jun 2024 20:08:10 +0530 Subject: [PATCH] refactor(common): move `setExpanded` to `CommentNode` --- .../claw/common/comments/CommentNode.kt | 19 ++++++++++--------- .../claw/common/comments/Comments.kt | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) 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 2ea9b398..0a6227c8 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 @@ -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) { diff --git a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/Comments.kt b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/Comments.kt index b449e678..2f97361c 100644 --- a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/Comments.kt +++ b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/Comments.kt @@ -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 })