diff --git a/lib/comment_tree.dart b/lib/comment_tree.dart index 671b7e9..fab9827 100644 --- a/lib/comment_tree.dart +++ b/lib/comment_tree.dart @@ -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 children; @@ -19,6 +46,7 @@ class CommentTree { children ??= []; } + /// takes raw linear comments and turns them into a CommentTree static List fromList(List comments) { CommentTree gatherChildren(CommentTree parent) { for (final el in comments) { @@ -29,38 +57,14 @@ class CommentTree { return parent; } - final parents = []; + 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 sortList( CommentSortType sortType, List 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; } } diff --git a/lib/pages/create_post.dart b/lib/pages/create_post.dart index 3dde101..211818d 100644 --- a/lib/pages/create_post.dart +++ b/lib/pages/create_post.dart @@ -10,9 +10,9 @@ import '../hooks/logged_in_action.dart'; import '../hooks/memo_future.dart'; import '../hooks/stores.dart'; import '../util/extensions/api.dart'; +import '../util/extensions/spaced.dart'; import '../util/goto.dart'; import '../util/pictrs.dart'; -import '../util/spaced.dart'; import '../widgets/markdown_text.dart'; import 'full_post.dart'; @@ -255,7 +255,7 @@ class CreatePost extends HookWidget { body: SafeArea( child: ListView( padding: EdgeInsets.all(5), - children: spaced(5, [ + children: [ instanceDropdown, communitiesDropdown, url, @@ -284,7 +284,7 @@ class CreatePost extends HookWidget { ) ], ), - ]), + ].spaced(6), ), ), ); diff --git a/lib/util/extensions/spaced.dart b/lib/util/extensions/spaced.dart new file mode 100644 index 0000000..1e2fa9c --- /dev/null +++ b/lib/util/extensions/spaced.dart @@ -0,0 +1,9 @@ +import 'package:flutter/cupertino.dart'; + +/// Creates gaps between given widgets +extension SpaceWidgets on List { + List spaced(double gap) => expand((item) sync* { + yield SizedBox(width: gap, height: gap); + yield item; + }).skip(1).toList(); +} diff --git a/lib/util/spaced.dart b/lib/util/spaced.dart deleted file mode 100644 index d000316..0000000 --- a/lib/util/spaced.dart +++ /dev/null @@ -1,10 +0,0 @@ -import 'package:flutter/cupertino.dart'; - -/// Creates gaps between given widgets -List spaced(double gap, Iterable children) => children - .expand((item) sync* { - yield SizedBox(width: gap, height: gap); - yield item; - }) - .skip(1) - .toList();