Merge pull request #73 from krawieck/comment-tree-refactor
This commit is contained in:
commit
376ba3ef50
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
/// Creates gaps between given widgets
|
||||
extension SpaceWidgets on List<Widget> {
|
||||
List<Widget> spaced(double gap) => expand((item) sync* {
|
||||
yield SizedBox(width: gap, height: gap);
|
||||
yield item;
|
||||
}).skip(1).toList();
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
/// Creates gaps between given widgets
|
||||
List<Widget> spaced(double gap, Iterable<Widget> children) => children
|
||||
.expand((item) sync* {
|
||||
yield SizedBox(width: gap, height: gap);
|
||||
yield item;
|
||||
})
|
||||
.skip(1)
|
||||
.toList();
|
Loading…
Reference in New Issue