2020-08-31 01:46:47 +02:00
|
|
|
import 'package:lemmy_api_client/lemmy_api_client.dart';
|
|
|
|
|
2020-09-02 13:49:07 +02:00
|
|
|
import 'util/hot_rank.dart';
|
|
|
|
|
|
|
|
enum CommentSortType {
|
|
|
|
hot,
|
|
|
|
top,
|
|
|
|
// ignore: constant_identifier_names
|
|
|
|
new_,
|
|
|
|
old,
|
|
|
|
chat,
|
|
|
|
}
|
|
|
|
|
2020-08-31 01:46:47 +02:00
|
|
|
class CommentTree {
|
|
|
|
CommentView comment;
|
|
|
|
List<CommentTree> children;
|
|
|
|
|
|
|
|
CommentTree(this.comment, [this.children]) {
|
|
|
|
children ??= [];
|
|
|
|
}
|
|
|
|
|
|
|
|
static List<CommentTree> fromList(List<CommentView> comments) {
|
|
|
|
CommentTree gatherChildren(CommentTree parent) {
|
2020-09-16 23:22:04 +02:00
|
|
|
for (final el in comments) {
|
2020-08-31 01:46:47 +02:00
|
|
|
if (el.parentId == parent.comment.id) {
|
|
|
|
parent.children.add(gatherChildren(CommentTree(el)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2020-09-16 23:22:04 +02:00
|
|
|
final parents = <CommentTree>[];
|
2020-08-31 01:46:47 +02:00
|
|
|
|
|
|
|
// first pass to get all the parents
|
|
|
|
for (var i = 0; i < comments.length; i++) {
|
|
|
|
if (comments[i].parentId == null) {
|
|
|
|
parents.add(CommentTree(comments[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 23:22:04 +02:00
|
|
|
final result = parents.map(gatherChildren).toList();
|
2020-08-31 01:46:47 +02:00
|
|
|
return result;
|
|
|
|
}
|
2020-09-02 13:49:07 +02:00
|
|
|
|
|
|
|
void sort(CommentSortType sortType) {
|
|
|
|
switch (sortType) {
|
|
|
|
case CommentSortType.chat:
|
2020-09-02 23:09:00 +02:00
|
|
|
// throw Exception('i dont do this kinda stuff kido');
|
|
|
|
return;
|
2020-09-02 13:49:07 +02:00
|
|
|
case CommentSortType.hot:
|
|
|
|
return _sort((b, a) =>
|
|
|
|
a.comment.computedHotRank.compareTo(b.comment.computedHotRank));
|
|
|
|
case CommentSortType.new_:
|
|
|
|
return _sort(
|
|
|
|
(b, a) => a.comment.published.compareTo(b.comment.published));
|
|
|
|
case CommentSortType.old:
|
|
|
|
return _sort(
|
|
|
|
(b, a) => b.comment.published.compareTo(a.comment.published));
|
|
|
|
case CommentSortType.top:
|
|
|
|
return _sort((b, a) => a.comment.score.compareTo(b.comment.score));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-02 23:09:00 +02:00
|
|
|
static List<CommentTree> sortList(
|
|
|
|
CommentSortType sortType, List<CommentTree> comms) {
|
2020-09-02 13:49:07 +02:00
|
|
|
switch (sortType) {
|
|
|
|
case CommentSortType.chat:
|
|
|
|
throw Exception('i dont do this kinda stuff kido');
|
|
|
|
case CommentSortType.hot:
|
|
|
|
comms.sort((b, a) =>
|
|
|
|
a.comment.computedHotRank.compareTo(b.comment.computedHotRank));
|
|
|
|
for (var i = 0; i < comms.length; i++) {
|
|
|
|
comms[i].sort(sortType);
|
|
|
|
}
|
2020-09-02 23:09:00 +02:00
|
|
|
return comms;
|
|
|
|
|
2020-09-02 13:49:07 +02:00
|
|
|
case CommentSortType.new_:
|
|
|
|
comms
|
|
|
|
.sort((b, a) => a.comment.published.compareTo(b.comment.published));
|
|
|
|
for (var i = 0; i < comms.length; i++) {
|
|
|
|
comms[i].sort(sortType);
|
|
|
|
}
|
2020-09-02 23:09:00 +02:00
|
|
|
return comms;
|
|
|
|
|
2020-09-02 13:49:07 +02:00
|
|
|
case CommentSortType.old:
|
|
|
|
comms
|
|
|
|
.sort((b, a) => b.comment.published.compareTo(a.comment.published));
|
|
|
|
for (var i = 0; i < comms.length; i++) {
|
|
|
|
comms[i].sort(sortType);
|
|
|
|
}
|
2020-09-02 23:09:00 +02:00
|
|
|
return comms;
|
|
|
|
|
2020-09-02 13:49:07 +02:00
|
|
|
case CommentSortType.top:
|
|
|
|
comms.sort((b, a) => a.comment.score.compareTo(b.comment.score));
|
|
|
|
for (var i = 0; i < comms.length; i++) {
|
|
|
|
comms[i].sort(sortType);
|
|
|
|
}
|
2020-09-02 23:09:00 +02:00
|
|
|
return comms;
|
2020-09-02 13:49:07 +02:00
|
|
|
}
|
2020-09-02 23:09:00 +02:00
|
|
|
throw Exception('unreachable');
|
2020-09-02 13:49:07 +02:00
|
|
|
}
|
2020-08-31 01:46:47 +02:00
|
|
|
}
|