fix(comments): calculate and expose indentLevel from CommentNode

This commit is contained in:
Anunay Maheshwari 2023-08-30 14:34:23 +05:30
parent 9dcf26b5a4
commit db393d4534
2 changed files with 21 additions and 7 deletions

View file

@ -149,7 +149,7 @@ internal fun CommentEntry(
.clickable { toggleExpanded(commentNode) } .clickable { toggleExpanded(commentNode) }
.background(MaterialTheme.colorScheme.background) .background(MaterialTheme.colorScheme.background)
.padding( .padding(
start = CommentEntryPadding * comment.indentLevel, start = CommentEntryPadding * commentNode.indentLevel,
end = CommentEntryPadding, end = CommentEntryPadding,
top = CommentEntryPadding, top = CommentEntryPadding,
bottom = CommentEntryPadding, bottom = CommentEntryPadding,

View file

@ -17,12 +17,14 @@ internal data class CommentNode(
val children: MutableList<CommentNode> = mutableListOf(), val children: MutableList<CommentNode> = mutableListOf(),
val isUnread: Boolean = false, val isUnread: Boolean = false,
var isExpanded: Boolean = true, var isExpanded: Boolean = true,
var indentLevel: Int
) { ) {
fun addChild(child: CommentNode) { fun addChild(child: CommentNode) {
if (comment.indentLevel + 1 == child.comment.indentLevel) { if (comment.shortId == child.comment.parentComment) {
children.add(child) children.add(child)
child.parent = this child.parent = this
} else { } else {
child.indentLevel += 1
children.last().addChild(child) children.last().addChild(child)
} }
} }
@ -36,12 +38,24 @@ internal fun createListNode(
val isUnread = { id: String -> commentState?.commentIds?.contains(id) == false } val isUnread = { id: String -> commentState?.commentIds?.contains(id) == false }
for (i in comments.indices) { for (i in comments.indices) {
if (comments[i].indentLevel == 1) { if (comments[i].parentComment == null) {
commentNodes.add(CommentNode(comment = comments[i], isUnread = isUnread(comments[i].shortId))) commentNodes.add(
CommentNode(
comment = comments[i],
isUnread = isUnread(comments[i].shortId),
indentLevel = 1
)
)
} else { } else {
commentNodes commentNodes.last().let {
.last() it.addChild(
.addChild(CommentNode(comment = comments[i], isUnread = isUnread(comments[i].shortId))) CommentNode(
comment = comments[i],
isUnread = isUnread(comments[i].shortId),
indentLevel = it.indentLevel + 1
)
)
}
} }
} }