Restructure logic, comments are now sorted upon opening

This commit is contained in:
krawieck 2020-09-02 23:09:00 +02:00
parent 47116b1cb1
commit 4bc6164b64
2 changed files with 29 additions and 19 deletions

View File

@ -45,7 +45,8 @@ class CommentTree {
void sort(CommentSortType sortType) {
switch (sortType) {
case CommentSortType.chat:
throw Exception('i dont do this kinda stuff kido');
// throw Exception('i dont do this kinda stuff kido');
return;
case CommentSortType.hot:
return _sort((b, a) =>
a.comment.computedHotRank.compareTo(b.comment.computedHotRank));
@ -67,7 +68,8 @@ class CommentTree {
}
}
static void sortList(CommentSortType sortType, List<CommentTree> comms) {
static List<CommentTree> sortList(
CommentSortType sortType, List<CommentTree> comms) {
switch (sortType) {
case CommentSortType.chat:
throw Exception('i dont do this kinda stuff kido');
@ -77,27 +79,31 @@ class CommentTree {
for (var i = 0; i < comms.length; i++) {
comms[i].sort(sortType);
}
break;
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);
}
break;
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);
}
break;
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);
}
break;
return comms;
}
throw Exception('unreachable');
}
}

View File

@ -10,16 +10,29 @@ class CommentSection extends HookWidget {
final List<CommentView> rawComments;
final List<CommentTree> comments;
final int postCreatorId;
final CommentSortType sortType;
CommentSection(this.rawComments, {@required this.postCreatorId})
: comments = CommentTree.fromList(rawComments),
CommentSection(
List<CommentView> rawComments, {
@required this.postCreatorId,
this.sortType = CommentSortType.hot,
}) : comments =
CommentTree.sortList(sortType, CommentTree.fromList(rawComments)),
rawComments = rawComments
..sort((b, a) => a.published.compareTo(b.published)),
assert(postCreatorId != null);
@override
Widget build(BuildContext context) {
var sorting = useState(sortType);
var rawComms = useState(rawComments);
var comms = useState(comments);
var sorting = useState(CommentSortType.hot);
void sortComments(CommentSortType sort) {
if (sort == sorting.value || sort == CommentSortType.chat) return;
CommentTree.sortList(sort, comms.value);
}
return Column(children: [
Padding(
@ -36,16 +49,7 @@ class CommentSection extends HookWidget {
// TODO: change it to universal BottomModal
underline: Container(),
isDense: true,
// ignore: avoid_types_on_closure_parameters
onChanged: (CommentSortType val) {
if (val != sorting.value && val != CommentSortType.chat) {
CommentTree.sortList(val, comms.value);
} else {
rawComms.value
.sort((a, b) => a.published.compareTo(b.published));
}
sorting.value = val;
},
onChanged: sortComments,
value: sorting.value,
items: [
DropdownMenuItem(