refactor comment tree

This commit is contained in:
shilangyu 2020-10-23 00:42:42 +02:00
parent b62ee7317a
commit b206d971ab
1 changed files with 37 additions and 62 deletions

View File

@ -11,6 +11,33 @@ enum CommentSortType {
chat,
}
extension on CommentSortType {
/// 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_:
return (b, a) => a.comment.published.compareTo(b.comment.published);
case CommentSortType.old:
return (b, a) => b.comment.published.compareTo(a.comment.published);
case CommentSortType.top:
return (b, a) => a.comment.score.compareTo(b.comment.score);
}
throw Exception('unreachable');
}
}
class CommentTree {
CommentView comment;
List<CommentTree> children;
@ -19,6 +46,7 @@ class CommentTree {
children ??= [];
}
/// takes raw linear comments and turns them into a CommentTree
static List<CommentTree> fromList(List<CommentView> comments) {
CommentTree gatherChildren(CommentTree parent) {
for (final el in comments) {
@ -29,38 +57,14 @@ class CommentTree {
return parent;
}
final parents = <CommentTree>[];
final topLevelParents =
comments.where((e) => e.parentId == null).map((e) => CommentTree(e));
// 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]));
}
}
final result = parents.map(gatherChildren).toList();
final result = topLevelParents.map(gatherChildren).toList();
return result;
}
void sort(CommentSortType sortType) {
switch (sortType) {
case CommentSortType.chat:
// throw Exception('i dont do this kinda stuff kido');
return;
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));
}
}
/// recursive sorter
void _sort(int compare(CommentTree a, CommentTree b)) {
children.sort(compare);
for (final el in children) {
@ -68,42 +72,13 @@ class CommentTree {
}
}
/// Sorts in-place a list of CommentTrees according to a given sortType
static List<CommentTree> sortList(
CommentSortType sortType, List<CommentTree> comms) {
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);
}
return comms;
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);
}
return comms;
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);
}
return comms;
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);
}
return comms;
comms.sort(sortType.sortFunction);
for (final el in comms) {
el._sort(sortType.sortFunction);
}
throw Exception('unreachable');
return comms;
}
}