mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-15 00:37: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 isExpanded: Boolean = true,
|
||||||
var indentLevel: Int,
|
var indentLevel: Int,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun addChild(child: CommentNode) {
|
fun addChild(child: CommentNode) {
|
||||||
if (comment.shortId == child.comment.parentComment) {
|
if (comment.shortId == child.comment.parentComment) {
|
||||||
children.add(child)
|
children.add(child)
|
||||||
|
@ -28,6 +29,15 @@ internal data class CommentNode(
|
||||||
children.lastOrNull()?.addChild(child)
|
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(
|
internal fun createListNode(
|
||||||
|
@ -62,15 +72,6 @@ internal fun createListNode(
|
||||||
return commentNodes
|
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 {
|
internal tailrec fun findTopMostParent(node: CommentNode): CommentNode {
|
||||||
val parent = node.parent
|
val parent = node.parent
|
||||||
return if (parent != null) {
|
return if (parent != null) {
|
||||||
|
|
|
@ -76,7 +76,7 @@ private fun CommentsPageInternal(
|
||||||
nodes = commentNodes,
|
nodes = commentNodes,
|
||||||
htmlConverter = htmlConverter,
|
htmlConverter = htmlConverter,
|
||||||
toggleExpanded = { node ->
|
toggleExpanded = { node ->
|
||||||
val newNode = setExpanded(node, !node.isExpanded)
|
val newNode = node.setExpanded(!node.isExpanded)
|
||||||
val parent = findTopMostParent(newNode)
|
val parent = findTopMostParent(newNode)
|
||||||
val index =
|
val index =
|
||||||
commentNodes.indexOf(commentNodes.find { it.comment.url == parent.comment.url })
|
commentNodes.indexOf(commentNodes.find { it.comment.url == parent.comment.url })
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue