Skip to content

Instantly share code, notes, and snippets.

@binrebin
Last active April 8, 2021 15:26
Show Gist options
  • Save binrebin/af66ddbb486ea3c2dcd64870107cdcf4 to your computer and use it in GitHub Desktop.
Save binrebin/af66ddbb486ea3c2dcd64870107cdcf4 to your computer and use it in GitHub Desktop.
@Keep
data class CommentsResponse(
@SerializedName("comments") private val commentsResponse: List<CommentResponse>) {
fun toComments() = commentsResponse.map { it.toComment() }
fun findChildren(cid: String): List<Comment> {
val comments = toComments()
val groups = comments
.sortedWith(compareBy({ it.parentid }, { it.comid }))
.groupBy { it.parentid }
return groups[comid] ?: emptyList()
}
@Keep
data class CommentResponse(
@SerializedName("comment_id") private val comid: String,
@SerializedName("post_id") private val postid: String,
@SerializedName("parent_id") private val parentid: String?,
){
fun toComment() = Comment( comid, postid, parentid, children?? )
}
}
data class Comment(
val comid: String,
val postid: String,
val parentid: String,
val children: List<Comment>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment