1
0
mirror of https://github.com/krawieck/lemmur/ synced 2024-12-11 08:05:43 +01:00
lemmur-app-android/lib/widgets/comment_section.dart

109 lines
3.4 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';
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';
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;
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
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
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: [
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),
title: Text(e.value[1] as String),
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-01-03 21:25:05 +01:00
Text(sortPairs[sorting.value][1] as String),
2021-01-03 19:43:39 +01:00
const Icon(Icons.arrow_drop_down),
],
),
2020-08-31 21:37:24 +02:00
),
2021-01-03 19:43:39 +01:00
const Spacer(),
],
),
),
// sorting menu goes here
if (comments.isEmpty)
2021-01-03 19:43:39 +01:00
const 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)
2021-01-24 20:01:55 +01:00
CommentWidget(
CommentTree(com),
postCreatorId: postCreatorId,
)
else
2021-01-24 20:01:55 +01:00
for (final com in comments)
CommentWidget(com, postCreatorId: postCreatorId),
2021-02-09 15:12:13 +01:00
const BottomSafe(kMinInteractiveDimension + kFloatingActionButtonMargin),
]);
}
2020-08-31 21:37:24 +02:00
}