mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 22:17:03 +05:30
refactor(common): move setExpanded
to CommentNode
This commit is contained in:
parent
7f2b052df5
commit
c4aad66c03
2 changed files with 11 additions and 10 deletions
|
@ -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) {
|
||||
|
|
|
@ -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 })
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue