val commentsByParentId = comments.groupBy { it.parentId }
val parentComments = commentsByParentId[null] ?: emptyList()
return parentComments
.sortedBy { it.createdAt }
.flatMap { comment ->
val childComments = commentsByParentId[comment.id] ?: emptyList()
listOf(
CommentUiModel.Base(
id = comment.id,
content = comment.content,
date = comment.createdAt.localizedTime(),
userName = comment.profileName,
userImage = comment.profileImage,
likedCount = "꾸잇 ${comment.likedCount}",
isWriter = comment.isMine,
onReplyClick = {
onReplyClick.onNext(listOf(comment) + childComments)
},
onMenuClick = { onMenuClick.onNext(comment) },
isLiked = comment.liked,
onLikeClick = { onCommentLikeClick.onNext(comment) },
onUserClick = { onUserClick.onNext(comment) },
isVisibleMenu = userId == comment.userId
)
) + childComments
.sortedBy { it.createdAt }
.map { childComment ->
CommentUiModel.Sub(
id = childComment.id,
content = childComment.content,
date = childComment.createdAt.localizedTime(),
userName = childComment.profileName,
userImage = childComment.profileImage,
likedCount = "꾸잇 ${childComment.likedCount}",
isWriter = childComment.isMine,
onMenuClick = { onMenuClick.onNext(childComment) }
)
}
}
}