2020-09-09 23:56:24 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2020-08-31 21:37:24 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2021-01-24 20:01:55 +01:00
|
|
|
import 'package:lemmy_api_client/v2.dart';
|
2020-08-31 21:37:24 +02:00
|
|
|
|
|
|
|
import '../comment_tree.dart';
|
2021-03-01 14:21:45 +01:00
|
|
|
import '../l10n/l10n.dart';
|
2020-09-09 23:56:24 +02:00
|
|
|
import 'bottom_modal.dart';
|
2021-01-26 23:16:47 +01:00
|
|
|
import 'bottom_safe.dart';
|
2020-08-31 21:37:24 +02:00
|
|
|
import 'comment.dart';
|
|
|
|
|
|
|
|
/// Manages comments section, sorts them
|
|
|
|
class CommentSection extends HookWidget {
|
|
|
|
final List<CommentView> rawComments;
|
|
|
|
final List<CommentTree> comments;
|
|
|
|
final int postCreatorId;
|
2020-09-02 23:09:00 +02:00
|
|
|
final CommentSortType sortType;
|
2020-08-31 21:37:24 +02:00
|
|
|
|
2020-09-10 15:24:45 +02:00
|
|
|
static const sortPairs = {
|
2021-03-01 14:38:44 +01:00
|
|
|
CommentSortType.hot: [Icons.whatshot, L10nStrings.hot],
|
|
|
|
CommentSortType.new_: [Icons.new_releases, L10nStrings.new_],
|
|
|
|
CommentSortType.old: [Icons.calendar_today, L10nStrings.old],
|
|
|
|
CommentSortType.top: [Icons.trending_up, L10nStrings.top],
|
|
|
|
CommentSortType.chat: [Icons.chat, L10nStrings.chat],
|
2020-09-10 15:24:45 +02:00
|
|
|
};
|
|
|
|
|
2020-09-02 23:09:00 +02:00
|
|
|
CommentSection(
|
|
|
|
List<CommentView> rawComments, {
|
|
|
|
@required this.postCreatorId,
|
|
|
|
this.sortType = CommentSortType.hot,
|
|
|
|
}) : comments =
|
|
|
|
CommentTree.sortList(sortType, CommentTree.fromList(rawComments)),
|
|
|
|
rawComments = rawComments
|
2021-01-24 20:01:55 +01:00
|
|
|
..sort((b, a) => a.comment.published.compareTo(b.comment.published)),
|
2020-08-31 21:37:24 +02:00
|
|
|
assert(postCreatorId != null);
|
|
|
|
|
|
|
|
@override
|
2020-09-02 13:49:07 +02:00
|
|
|
Widget build(BuildContext context) {
|
2020-09-16 23:22:04 +02:00
|
|
|
final sorting = useState(sortType);
|
2020-09-02 23:09:00 +02:00
|
|
|
|
|
|
|
void sortComments(CommentSortType sort) {
|
2020-09-03 00:17:42 +02:00
|
|
|
if (sort != sorting.value && sort != CommentSortType.chat) {
|
2020-09-21 13:30:47 +02:00
|
|
|
CommentTree.sortList(sort, comments);
|
2020-09-03 00:17:42 +02:00
|
|
|
}
|
2020-09-02 23:09:00 +02:00
|
|
|
|
2020-09-03 00:17:42 +02:00
|
|
|
sorting.value = sort;
|
2020-09-02 23:09:00 +02:00
|
|
|
}
|
2020-09-02 13:49:07 +02:00
|
|
|
|
|
|
|
return Column(children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
2021-02-09 15:12:13 +01:00
|
|
|
OutlinedButton(
|
2020-09-09 23:56:24 +02:00
|
|
|
onPressed: () {
|
2021-02-09 15:12:13 +01:00
|
|
|
showBottomModal(
|
|
|
|
title: 'sort by',
|
|
|
|
context: context,
|
|
|
|
builder: (context) => Column(
|
|
|
|
children: [
|
|
|
|
for (final e in sortPairs.entries)
|
|
|
|
ListTile(
|
|
|
|
leading: Icon(e.value[0] as IconData),
|
2021-03-01 14:21:45 +01:00
|
|
|
title: Text((e.value[1] as String).tr(context)),
|
2021-02-09 15:12:13 +01:00
|
|
|
trailing: sorting.value == e.key
|
|
|
|
? const Icon(Icons.check)
|
|
|
|
: null,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
sortComments(e.key);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
2020-09-09 23:56:24 +02:00
|
|
|
},
|
|
|
|
child: Row(
|
|
|
|
children: [
|
2021-03-01 14:21:45 +01:00
|
|
|
Text((sortPairs[sorting.value][1] as String).tr(context)),
|
2021-01-03 19:43:39 +01:00
|
|
|
const Icon(Icons.arrow_drop_down),
|
2020-09-02 13:49:07 +02:00
|
|
|
],
|
|
|
|
),
|
2020-08-31 21:37:24 +02:00
|
|
|
),
|
2021-01-03 19:43:39 +01:00
|
|
|
const Spacer(),
|
2020-09-02 13:49:07 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
// sorting menu goes here
|
|
|
|
if (comments.isEmpty)
|
2021-01-03 19:43:39 +01:00
|
|
|
const Padding(
|
2020-09-02 13:49:07 +02:00
|
|
|
padding: EdgeInsets.symmetric(vertical: 50),
|
|
|
|
child: Text(
|
|
|
|
'no comments yet',
|
|
|
|
style: TextStyle(fontStyle: FontStyle.italic),
|
2020-08-31 21:37:24 +02:00
|
|
|
),
|
2020-09-02 13:49:07 +02:00
|
|
|
)
|
|
|
|
else if (sorting.value == CommentSortType.chat)
|
2021-02-24 20:52:18 +01:00
|
|
|
for (final com in rawComments) CommentWidget.fromCommentView(com)
|
2020-09-02 13:49:07 +02:00
|
|
|
else
|
2021-02-24 20:52:18 +01:00
|
|
|
for (final com in comments) CommentWidget(com),
|
2021-02-09 15:12:13 +01:00
|
|
|
const BottomSafe(kMinInteractiveDimension + kFloatingActionButtonMargin),
|
2020-09-02 13:49:07 +02:00
|
|
|
]);
|
|
|
|
}
|
2020-08-31 21:37:24 +02:00
|
|
|
}
|