Skip to content

Commit d84f185

Browse files
authored
Dont resort comments (#77)
* Adding user and community blocking. Fixes #71 Fixes #58 * Don't resort comments in posts. Fixes #76
1 parent 25c84ce commit d84f185

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

app/src/main/java/com/jerboa/Utils.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fun commentsToFlatNodes(
107107

108108
fun buildCommentsTree(
109109
comments: List<CommentView>?,
110-
// commentSortType: CommentSortType,
110+
sortType: SortType,
111111
): List<CommentNodeData> {
112112

113113
val map = LinkedHashMap<Number, CommentNodeData>()
@@ -139,6 +139,8 @@ fun buildCommentsTree(
139139
}
140140
}
141141

142+
sortNodes(tree, sortType)
143+
142144
return tree
143145
}
144146

@@ -469,17 +471,25 @@ private fun DrawScope.drawEndBorder(
469471
)
470472
}
471473

472-
fun sortNodes(nodes: List<CommentNodeData>, sortType: SortType = SortType.New): List<CommentNodeData> {
473-
val afterRank = when (sortType) {
474-
SortType.Hot -> nodes.sortedByDescending {
474+
fun sortNodes(nodes: MutableList<CommentNodeData>, sortType: SortType) {
475+
when (sortType) {
476+
SortType.Hot -> nodes.sortByDescending {
475477
hotRank(
476478
it.commentView.counts.score,
477479
it.commentView.comment.published
478480
)
479481
}
480-
else -> nodes
482+
else -> {}
483+
}
484+
485+
nodes.sortBy { it.commentView.comment.deleted || it.commentView.comment.removed }
486+
487+
// Go through the children recursively
488+
nodes.forEach { node ->
489+
node.children?.also {
490+
sortNodes(it, sortType)
491+
}
481492
}
482-
return afterRank.sortedBy { it.commentView.comment.deleted || it.commentView.comment.removed }
483493
}
484494

485495
fun hotRank(score: Int, dateStr: String): Double {

app/src/main/java/com/jerboa/ui/components/comment/CommentNode.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ fun CommentNodesPreview() {
389389
val comments = listOf(
390390
sampleSecondCommentReplyView, sampleCommentReplyView, sampleCommentView
391391
)
392-
val tree = buildCommentsTree(comments)
392+
val tree = buildCommentsTree(comments, SortType.Hot)
393393
CommentNodes(
394394
nodes = tree,
395395
moderators = listOf(),

app/src/main/java/com/jerboa/ui/components/comment/CommentNodes.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import androidx.compose.runtime.Composable
55
import com.jerboa.CommentNodeData
66
import com.jerboa.datatypes.*
77
import com.jerboa.db.Account
8-
import com.jerboa.sortNodes
98

109
@Composable
1110
fun CommentNodes(
@@ -27,7 +26,7 @@ fun CommentNodes(
2726
showRead: Boolean = false,
2827
) {
2928
Column {
30-
sortNodes(nodes, SortType.Hot).forEach { node ->
29+
nodes.forEach { node ->
3130
CommentNode(
3231
node = node,
3332
onUpvoteClick = onUpvoteClick,

app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fun PostActivity(
5555
val listState = rememberLazyListState()
5656

5757
val account = getCurrentAccount(accountViewModel = accountViewModel)
58-
val commentNodes = sortNodes(buildCommentsTree(postViewModel.comments), SortType.Hot)
58+
val commentNodes = buildCommentsTree(postViewModel.comments, SortType.Hot)
5959

6060
val swipeRefreshState = rememberSwipeRefreshState(
6161
isRefreshing = postViewModel.loading && postViewModel

app/src/main/java/com/jerboa/ui/components/post/PostViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class PostViewModel : ViewModel() {
8484
likeCommentRoutine(
8585
commentView = mutableStateOf(commentView),
8686
voteType = voteType,
87-
comments = comments,
87+
// An edge case, but don't pass in comments, otherwise a resort will occur
8888
account = account,
8989
ctx = ctx,
9090
scope = viewModelScope,

0 commit comments

Comments
 (0)