refactor(common): get rid of interior mutability in CommentNode

This commit is contained in:
Harsh Shandilya 2024-08-21 13:07:15 +05:30
parent d55ff24add
commit c50790e4f9

View file

@ -14,7 +14,7 @@ internal data class CommentNode(
var parent: CommentNode? = null, var parent: CommentNode? = null,
val children: MutableList<CommentNode> = mutableListOf(), val children: MutableList<CommentNode> = mutableListOf(),
val isUnread: Boolean = false, val isUnread: Boolean = false,
var indentLevel: Int, val indentLevel: Int,
) { ) {
fun addChild(child: CommentNode) { fun addChild(child: CommentNode) {
@ -22,8 +22,16 @@ internal data class CommentNode(
children.add(child) children.add(child)
child.parent = this child.parent = this
} else { } else {
child.indentLevel += 1 children
children.lastOrNull()?.addChild(child) .lastOrNull()
?.addChild(
CommentNode(
comment = child.comment,
parent = child.parent,
isUnread = child.isUnread,
indentLevel = child.indentLevel + 1,
)
)
} }
} }
} }
@ -45,12 +53,12 @@ internal fun createListNode(
) )
) )
} else { } else {
commentNodes.lastOrNull()?.let { commentNodes.lastOrNull()?.let { commentNode ->
it.addChild( commentNode.addChild(
CommentNode( CommentNode(
comment = comments[i], comment = comments[i],
isUnread = isUnread(comments[i].shortId), isUnread = isUnread(comments[i].shortId),
indentLevel = it.indentLevel + 1, indentLevel = commentNode.indentLevel + 1,
) )
) )
} }