feat: limit comment depth to 4

This commit is contained in:
rebelonion 2024-02-24 19:05:17 -06:00
parent 526098f2bf
commit a251dd4ffb
2 changed files with 40 additions and 2 deletions

View file

@ -40,6 +40,7 @@ class CommentItem(val comment: Comment,
) : BindableItem<ItemCommentsBinding>() { ) : BindableItem<ItemCommentsBinding>() {
var binding: ItemCommentsBinding? = null var binding: ItemCommentsBinding? = null
val adapter = GroupieAdapter() val adapter = GroupieAdapter()
private var subCommentIds: MutableList<Int> = mutableListOf()
val repliesSection = Section() val repliesSection = Section()
var isEditing = false var isEditing = false
private var isReplying = false private var isReplying = false
@ -77,6 +78,7 @@ class CommentItem(val comment: Comment,
viewBinding.commentTotalReplies.setOnClickListener { viewBinding.commentTotalReplies.setOnClickListener {
if (repliesVisible) { if (repliesVisible) {
repliesSection.clear() repliesSection.clear()
removeSubCommentIds()
viewBinding.commentTotalReplies.text = "View ${comment.replyCount} repl${if (comment.replyCount == 1) "y" else "ies"}" viewBinding.commentTotalReplies.text = "View ${comment.replyCount} repl${if (comment.replyCount == 1) "y" else "ies"}"
repliesVisible = false repliesVisible = false
} else { } else {
@ -168,7 +170,7 @@ class CommentItem(val comment: Comment,
viewBinding.commentUserAvatar viewBinding.commentUserAvatar
comment.profilePictureUrl?.let { viewBinding.commentUserAvatar.loadImage(it) } comment.profilePictureUrl?.let { viewBinding.commentUserAvatar.loadImage(it) }
viewBinding.commentUserName.text = comment.username viewBinding.commentUserName.text = comment.username
val levelColor = getAvatarColor(comment.upvotes - comment.downvotes, backgroundColor) val levelColor = getAvatarColor(comment.totalVotes, backgroundColor)
viewBinding.commentUserName.setTextColor(levelColor.first) viewBinding.commentUserName.setTextColor(levelColor.first)
viewBinding.commentUserLevel.text = "Lv. ${levelColor.second}" viewBinding.commentUserLevel.text = "Lv. ${levelColor.second}"
viewBinding.commentUserLevel.setTextColor(levelColor.first) viewBinding.commentUserLevel.setTextColor(levelColor.first)
@ -193,9 +195,27 @@ class CommentItem(val comment: Comment,
binding?.commentEdit?.text = if (isEditing) currActivity()!!.getString(R.string.cancel) else currActivity()!!.getString(R.string.edit) binding?.commentEdit?.text = if (isEditing) currActivity()!!.getString(R.string.cancel) else currActivity()!!.getString(R.string.edit)
this.isEditing = isEditing this.isEditing = isEditing
} }
fun registerSubComment(id: Int) {
subCommentIds.add(id)
}
private fun removeSubCommentIds(){
subCommentIds.forEach { id ->
val parentComments = parentSection.groups as? List<CommentItem> ?: emptyList()
val commentToRemove = parentComments.find { it.comment.commentId == id }
commentToRemove?.let {
it.removeSubCommentIds()
parentSection.remove(it)
}
}
subCommentIds.clear()
}
fun test(isEditing: Boolean){ fun test(isEditing: Boolean){
this.isEditing = isEditing this.isEditing = isEditing
} }
private fun setVoteButtons(viewBinding: ItemCommentsBinding) { private fun setVoteButtons(viewBinding: ItemCommentsBinding) {
when (comment.userVoteType) { when (comment.userVoteType) {
1 -> { 1 -> {

View file

@ -171,6 +171,7 @@ class CommentsActivity : AppCompatActivity() {
} }
} }
//adds additional comments to the section
private suspend fun updateUIWithComment(comment: Comment) { private suspend fun updateUIWithComment(comment: Comment) {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
section.add( section.add(
@ -220,6 +221,12 @@ class CommentsActivity : AppCompatActivity() {
NONE, EDIT, REPLY NONE, EDIT, REPLY
} }
/**
* Loads and displays the comments
* Called when the activity is created
* Or when the user refreshes the comments
*/
private suspend fun loadAndDisplayComments() { private suspend fun loadAndDisplayComments() {
binding.commentsProgressBar.visibility = View.VISIBLE binding.commentsProgressBar.visibility = View.VISIBLE
binding.commentsList.visibility = View.GONE binding.commentsList.visibility = View.GONE
@ -336,6 +343,11 @@ class CommentsActivity : AppCompatActivity() {
binding.commentReplyToContainer.visibility = View.GONE binding.commentReplyToContainer.visibility = View.GONE
} }
} }
/**
* Callback from the comment item to view the replies to the comment
* @param comment the comment to view the replies of
*/
fun viewReplyCallback(comment: CommentItem) { fun viewReplyCallback(comment: CommentItem) {
lifecycleScope.launch { lifecycleScope.launch {
val replies = withContext(Dispatchers.IO) { val replies = withContext(Dispatchers.IO) {
@ -345,7 +357,7 @@ class CommentsActivity : AppCompatActivity() {
replies?.comments?.forEach { replies?.comments?.forEach {
val depth = if (comment.commentDepth + 1 > comment.MAX_DEPTH) comment.commentDepth else comment.commentDepth + 1 val depth = if (comment.commentDepth + 1 > comment.MAX_DEPTH) comment.commentDepth else comment.commentDepth + 1
val section = if (comment.commentDepth + 1 > comment.MAX_DEPTH) comment.parentSection else comment.repliesSection val section = if (comment.commentDepth + 1 > comment.MAX_DEPTH) comment.parentSection else comment.repliesSection
if (depth >= comment.MAX_DEPTH) comment.registerSubComment(it.commentId)
val newCommentItem = CommentItem( val newCommentItem = CommentItem(
it, it,
buildMarkwon(), buildMarkwon(),
@ -439,6 +451,11 @@ class CommentsActivity : AppCompatActivity() {
item.notifyChanged() item.notifyChanged()
} }
/**
* Handles the new user-added comment
* @param commentText the text of the comment
*/
private suspend fun handleNewComment(commentText: String) { private suspend fun handleNewComment(commentText: String) {
val success = withContext(Dispatchers.IO) { val success = withContext(Dispatchers.IO) {
CommentsAPI.comment( CommentsAPI.comment(
@ -452,6 +469,7 @@ class CommentsActivity : AppCompatActivity() {
if (commentWithInteraction == null) return@let if (commentWithInteraction == null) return@let
val section = if (commentWithInteraction!!.commentDepth + 1 > commentWithInteraction!!.MAX_DEPTH) commentWithInteraction?.parentSection else commentWithInteraction?.repliesSection val section = if (commentWithInteraction!!.commentDepth + 1 > commentWithInteraction!!.MAX_DEPTH) commentWithInteraction?.parentSection else commentWithInteraction?.repliesSection
val depth = if (commentWithInteraction!!.commentDepth + 1 > commentWithInteraction!!.MAX_DEPTH) commentWithInteraction!!.commentDepth else commentWithInteraction!!.commentDepth + 1 val depth = if (commentWithInteraction!!.commentDepth + 1 > commentWithInteraction!!.MAX_DEPTH) commentWithInteraction!!.commentDepth else commentWithInteraction!!.commentDepth + 1
if (depth >= commentWithInteraction!!.MAX_DEPTH) commentWithInteraction!!.registerSubComment(it.commentId)
section?.add( section?.add(
if (commentWithInteraction!!.commentDepth + 1 > commentWithInteraction!!.MAX_DEPTH) 0 else section.itemCount, if (commentWithInteraction!!.commentDepth + 1 > commentWithInteraction!!.MAX_DEPTH) 0 else section.itemCount,
CommentItem( CommentItem(