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) { void sort(CommentSortType sortType) {
switch (sortType) { switch (sortType) {
case CommentSortType.chat: 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: case CommentSortType.hot:
return _sort((b, a) => return _sort((b, a) =>
a.comment.computedHotRank.compareTo(b.comment.computedHotRank)); 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) { switch (sortType) {
case CommentSortType.chat: case CommentSortType.chat:
throw Exception('i dont do this kinda stuff kido'); throw Exception('i dont do this kinda stuff kido');
@ -77,27 +79,31 @@ class CommentTree {
for (var i = 0; i < comms.length; i++) { for (var i = 0; i < comms.length; i++) {
comms[i].sort(sortType); comms[i].sort(sortType);
} }
break; return comms;
case CommentSortType.new_: case CommentSortType.new_:
comms comms
.sort((b, a) => a.comment.published.compareTo(b.comment.published)); .sort((b, a) => a.comment.published.compareTo(b.comment.published));
for (var i = 0; i < comms.length; i++) { for (var i = 0; i < comms.length; i++) {
comms[i].sort(sortType); comms[i].sort(sortType);
} }
break; return comms;
case CommentSortType.old: case CommentSortType.old:
comms comms
.sort((b, a) => b.comment.published.compareTo(a.comment.published)); .sort((b, a) => b.comment.published.compareTo(a.comment.published));
for (var i = 0; i < comms.length; i++) { for (var i = 0; i < comms.length; i++) {
comms[i].sort(sortType); comms[i].sort(sortType);
} }
break; return comms;
case CommentSortType.top: case CommentSortType.top:
comms.sort((b, a) => a.comment.score.compareTo(b.comment.score)); comms.sort((b, a) => a.comment.score.compareTo(b.comment.score));
for (var i = 0; i < comms.length; i++) { for (var i = 0; i < comms.length; i++) {
comms[i].sort(sortType); 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<CommentView> rawComments;
final List<CommentTree> comments; final List<CommentTree> comments;
final int postCreatorId; final int postCreatorId;
final CommentSortType sortType;
CommentSection(this.rawComments, {@required this.postCreatorId}) CommentSection(
: comments = CommentTree.fromList(rawComments), 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); assert(postCreatorId != null);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var sorting = useState(sortType);
var rawComms = useState(rawComments); var rawComms = useState(rawComments);
var comms = useState(comments); 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: [ return Column(children: [
Padding( Padding(
@ -36,16 +49,7 @@ class CommentSection extends HookWidget {
// TODO: change it to universal BottomModal // TODO: change it to universal BottomModal
underline: Container(), underline: Container(),
isDense: true, isDense: true,
// ignore: avoid_types_on_closure_parameters onChanged: sortComments,
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;
},
value: sorting.value, value: sorting.value,
items: [ items: [
DropdownMenuItem( DropdownMenuItem(