From 4bc6164b644082ace927e3e7170d24b7ac31fdf2 Mon Sep 17 00:00:00 2001 From: krawieck Date: Wed, 2 Sep 2020 23:09:00 +0200 Subject: [PATCH] Restructure logic, comments are now sorted upon opening --- lib/comment_tree.dart | 18 ++++++++++++------ lib/widgets/comment_section.dart | 30 +++++++++++++++++------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/lib/comment_tree.dart b/lib/comment_tree.dart index 4b61749..f8d6630 100644 --- a/lib/comment_tree.dart +++ b/lib/comment_tree.dart @@ -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 comms) { + static List sortList( + CommentSortType sortType, List 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'); } } diff --git a/lib/widgets/comment_section.dart b/lib/widgets/comment_section.dart index 6b9bbad..8b7179b 100644 --- a/lib/widgets/comment_section.dart +++ b/lib/widgets/comment_section.dart @@ -10,16 +10,29 @@ class CommentSection extends HookWidget { final List rawComments; final List comments; final int postCreatorId; + final CommentSortType sortType; - CommentSection(this.rawComments, {@required this.postCreatorId}) - : comments = CommentTree.fromList(rawComments), + CommentSection( + List 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(