lemmur-app-android/lib/widgets/comment_section.dart

111 lines
3.6 KiB
Dart
Raw Normal View History

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';
import 'package:lemmy_api_client/lemmy_api_client.dart';
import '../comment_tree.dart';
2020-09-09 23:56:24 +02:00
import 'bottom_modal.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;
final CommentSortType sortType;
2020-08-31 21:37:24 +02:00
static const sortPairs = {
CommentSortType.hot: [Icons.whatshot, 'Hot'],
CommentSortType.new_: [Icons.new_releases, 'New'],
CommentSortType.old: [Icons.calendar_today, 'Old'],
CommentSortType.top: [Icons.trending_up, 'Top'],
CommentSortType.chat: [Icons.chat, 'Chat'],
};
CommentSection(
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)),
2020-08-31 21:37:24 +02:00
assert(postCreatorId != null);
@override
Widget build(BuildContext context) {
2020-09-16 23:22:04 +02:00
final sorting = useState(sortType);
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-03 00:17:42 +02:00
sorting.value = sort;
}
return Column(children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
child: Row(
children: [
2020-09-09 23:56:24 +02:00
OutlineButton(
onPressed: () {
showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context,
builder: (context) => BottomModal(
title: 'sort by',
child: Column(
children: [
for (final e in sortPairs.entries)
ListTile(
leading: Icon(e.value[0]),
title: Text(e.value[1]),
trailing: sorting.value == e.key
? Icon(Icons.check)
: null,
onTap: () {
Navigator.of(context).pop();
2020-09-10 14:00:57 +02:00
sortComments(e.key);
2020-09-09 23:56:24 +02:00
},
)
],
),
));
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
2020-09-09 23:56:24 +02:00
child: Row(
children: [
Text(sortPairs[sorting.value][1]),
Icon(Icons.arrow_drop_down),
],
),
2020-08-31 21:37:24 +02:00
),
Spacer(),
],
),
),
// sorting menu goes here
if (comments.isEmpty)
Padding(
padding: EdgeInsets.symmetric(vertical: 50),
child: Text(
'no comments yet',
style: TextStyle(fontStyle: FontStyle.italic),
2020-08-31 21:37:24 +02:00
),
)
else if (sorting.value == CommentSortType.chat)
2020-09-21 13:30:47 +02:00
for (final com in rawComments)
Comment(
CommentTree(com),
postCreatorId: postCreatorId,
)
else
2020-09-21 13:30:47 +02:00
for (final com in comments) Comment(com, postCreatorId: postCreatorId),
]);
}
2020-08-31 21:37:24 +02:00
}