Skip to content

Commit

Permalink
Dont resort comments (#77)
Browse files Browse the repository at this point in the history
* Adding user and community blocking. Fixes #71 Fixes #58

* Don't resort comments in posts. Fixes #76
  • Loading branch information
dessalines authored Feb 3, 2022
1 parent 25c84ce commit d84f185
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
22 changes: 16 additions & 6 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fun commentsToFlatNodes(

fun buildCommentsTree(
comments: List<CommentView>?,
// commentSortType: CommentSortType,
sortType: SortType,
): List<CommentNodeData> {

val map = LinkedHashMap<Number, CommentNodeData>()
Expand Down Expand Up @@ -139,6 +139,8 @@ fun buildCommentsTree(
}
}

sortNodes(tree, sortType)

return tree
}

Expand Down Expand Up @@ -469,17 +471,25 @@ private fun DrawScope.drawEndBorder(
)
}

fun sortNodes(nodes: List<CommentNodeData>, sortType: SortType = SortType.New): List<CommentNodeData> {
val afterRank = when (sortType) {
SortType.Hot -> nodes.sortedByDescending {
fun sortNodes(nodes: MutableList<CommentNodeData>, sortType: SortType) {
when (sortType) {
SortType.Hot -> nodes.sortByDescending {
hotRank(
it.commentView.counts.score,
it.commentView.comment.published
)
}
else -> nodes
else -> {}
}

nodes.sortBy { it.commentView.comment.deleted || it.commentView.comment.removed }

// Go through the children recursively
nodes.forEach { node ->
node.children?.also {
sortNodes(it, sortType)
}
}
return afterRank.sortedBy { it.commentView.comment.deleted || it.commentView.comment.removed }
}

fun hotRank(score: Int, dateStr: String): Double {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ fun CommentNodesPreview() {
val comments = listOf(
sampleSecondCommentReplyView, sampleCommentReplyView, sampleCommentView
)
val tree = buildCommentsTree(comments)
val tree = buildCommentsTree(comments, SortType.Hot)
CommentNodes(
nodes = tree,
moderators = listOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.compose.runtime.Composable
import com.jerboa.CommentNodeData
import com.jerboa.datatypes.*
import com.jerboa.db.Account
import com.jerboa.sortNodes

@Composable
fun CommentNodes(
Expand All @@ -27,7 +26,7 @@ fun CommentNodes(
showRead: Boolean = false,
) {
Column {
sortNodes(nodes, SortType.Hot).forEach { node ->
nodes.forEach { node ->
CommentNode(
node = node,
onUpvoteClick = onUpvoteClick,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fun PostActivity(
val listState = rememberLazyListState()

val account = getCurrentAccount(accountViewModel = accountViewModel)
val commentNodes = sortNodes(buildCommentsTree(postViewModel.comments), SortType.Hot)
val commentNodes = buildCommentsTree(postViewModel.comments, SortType.Hot)

val swipeRefreshState = rememberSwipeRefreshState(
isRefreshing = postViewModel.loading && postViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class PostViewModel : ViewModel() {
likeCommentRoutine(
commentView = mutableStateOf(commentView),
voteType = voteType,
comments = comments,
// An edge case, but don't pass in comments, otherwise a resort will occur
account = account,
ctx = ctx,
scope = viewModelScope,
Expand Down

0 comments on commit d84f185

Please sign in to comment.