2021-04-05 20:14:39 +02:00
|
|
|
import 'package:lemmy_api_client/v3.dart';
|
2020-08-31 01:46:47 +02:00
|
|
|
|
2020-09-02 13:49:07 +02:00
|
|
|
import 'util/hot_rank.dart';
|
|
|
|
|
|
|
|
enum CommentSortType {
|
|
|
|
hot,
|
|
|
|
top,
|
|
|
|
new_,
|
|
|
|
old,
|
2022-05-11 22:23:18 +02:00
|
|
|
chat;
|
2020-09-02 13:49:07 +02:00
|
|
|
|
2020-10-23 00:42:42 +02:00
|
|
|
/// returns a compare function for sorting a CommentTree according
|
|
|
|
/// to the comment sort type
|
|
|
|
int Function(CommentTree a, CommentTree b) get sortFunction {
|
|
|
|
switch (this) {
|
|
|
|
case CommentSortType.chat:
|
|
|
|
throw Exception('Sorting a CommentTree in chat mode is not supported'
|
|
|
|
' because it would restructure the whole tree');
|
|
|
|
|
|
|
|
case CommentSortType.hot:
|
|
|
|
return (b, a) =>
|
|
|
|
a.comment.computedHotRank.compareTo(b.comment.computedHotRank);
|
|
|
|
|
|
|
|
case CommentSortType.new_:
|
2021-01-24 20:01:55 +01:00
|
|
|
return (b, a) =>
|
|
|
|
a.comment.comment.published.compareTo(b.comment.comment.published);
|
2020-10-23 00:42:42 +02:00
|
|
|
|
|
|
|
case CommentSortType.old:
|
2021-01-24 20:01:55 +01:00
|
|
|
return (b, a) =>
|
|
|
|
b.comment.comment.published.compareTo(a.comment.comment.published);
|
2020-10-23 00:42:42 +02:00
|
|
|
|
|
|
|
case CommentSortType.top:
|
2021-01-24 20:01:55 +01:00
|
|
|
return (b, a) =>
|
|
|
|
a.comment.counts.score.compareTo(b.comment.counts.score);
|
2020-10-23 00:42:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 23:03:42 +02:00
|
|
|
extension SortCommentTreeList on List<CommentTree> {
|
|
|
|
void sortBy(CommentSortType sortType) {
|
|
|
|
sort(sortType.sortFunction);
|
|
|
|
for (final el in this) {
|
|
|
|
el._sort(sortType.sortFunction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 01:46:47 +02:00
|
|
|
class CommentTree {
|
|
|
|
CommentView comment;
|
2021-04-11 11:12:42 +02:00
|
|
|
List<CommentTree> children = [];
|
2020-08-31 01:46:47 +02:00
|
|
|
|
2021-04-11 11:12:42 +02:00
|
|
|
CommentTree(this.comment);
|
2020-08-31 01:46:47 +02:00
|
|
|
|
2020-10-23 00:42:42 +02:00
|
|
|
/// takes raw linear comments and turns them into a CommentTree
|
2020-08-31 01:46:47 +02:00
|
|
|
static List<CommentTree> fromList(List<CommentView> comments) {
|
|
|
|
CommentTree gatherChildren(CommentTree parent) {
|
2020-09-16 23:22:04 +02:00
|
|
|
for (final el in comments) {
|
2021-01-24 20:01:55 +01:00
|
|
|
if (el.comment.parentId == parent.comment.comment.id) {
|
2020-08-31 01:46:47 +02:00
|
|
|
parent.children.add(gatherChildren(CommentTree(el)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2022-01-06 16:05:46 +01:00
|
|
|
final topLevelParents =
|
|
|
|
comments.where((e) => e.comment.parentId == null).map(CommentTree.new);
|
2020-08-31 01:46:47 +02:00
|
|
|
|
2020-10-23 00:42:42 +02:00
|
|
|
final result = topLevelParents.map(gatherChildren).toList();
|
2020-08-31 01:46:47 +02:00
|
|
|
return result;
|
|
|
|
}
|
2020-09-02 13:49:07 +02:00
|
|
|
|
2020-10-23 00:42:42 +02:00
|
|
|
/// recursive sorter
|
2020-09-02 13:49:07 +02:00
|
|
|
void _sort(int compare(CommentTree a, CommentTree b)) {
|
|
|
|
children.sort(compare);
|
2020-09-16 23:22:04 +02:00
|
|
|
for (final el in children) {
|
2020-09-02 13:49:07 +02:00
|
|
|
el._sort(compare);
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 01:46:47 +02:00
|
|
|
}
|