From 2e3ccac233260df88d0a751ecb277c01db9badfb Mon Sep 17 00:00:00 2001 From: krawieck Date: Thu, 9 Sep 2021 14:46:01 +0200 Subject: [PATCH] add blocking to comments --- lib/widgets/comment/comment.dart | 33 ++++++++++++++++--- .../comment/comment_more_menu_button.dart | 19 +++++++++-- lib/widgets/comment/comment_store.dart | 16 +++++++++ lib/widgets/comment/comment_store.g.dart | 7 ++++ 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/lib/widgets/comment/comment.dart b/lib/widgets/comment/comment.dart index b6b41f4..8163a93 100644 --- a/lib/widgets/comment/comment.dart +++ b/lib/widgets/comment/comment.dart @@ -8,6 +8,7 @@ import 'package:provider/provider.dart'; import '../../comment_tree.dart'; import '../../l10n/l10n.dart'; import '../../stores/config_store.dart'; +import '../../util/async_store.dart'; import '../../util/async_store_listener.dart'; import '../../util/extensions/api.dart'; import '../../util/extensions/cake_day.dart'; @@ -91,13 +92,35 @@ class CommentWidget extends StatelessWidget { detached: detached, hideOnRead: hideOnRead, ), - builder: (context, child) => AsyncStoreListener( - asyncStore: context.read().votingState, + builder: (context, child) => ObserverListener>( + store: context.read().blockingState, + listener: (context, store) { + final errorTerm = store.errorTerm; + + if (errorTerm != null) { + ScaffoldMessenger.of(context) + ..hideCurrentSnackBar() + ..showSnackBar(SnackBar(content: Text(errorTerm.tr(context)))); + } else if (store.asyncState is AsyncStateData) { + final state = store.asyncState as AsyncStateData; + ScaffoldMessenger.of(context) + ..hideCurrentSnackBar() + ..showSnackBar( + SnackBar( + content: Text( + state.data.blocked ? 'User blocked' : 'User unblocked'), + ), + ); + } + }, child: AsyncStoreListener( - asyncStore: context.read().deletingState, + asyncStore: context.read().votingState, child: AsyncStoreListener( - asyncStore: context.read().savingState, - child: const _CommentWidget(), + asyncStore: context.read().deletingState, + child: AsyncStoreListener( + asyncStore: context.read().savingState, + child: const _CommentWidget(), + ), ), ), ), diff --git a/lib/widgets/comment/comment_more_menu_button.dart b/lib/widgets/comment/comment_more_menu_button.dart index 0e533c3..f708595 100644 --- a/lib/widgets/comment/comment_more_menu_button.dart +++ b/lib/widgets/comment/comment_more_menu_button.dart @@ -130,18 +130,33 @@ class _CommentMoreMenuPopup extends HookWidget { Navigator.of(context).pop(); }, ), - if (store.isMine) + if (store.isMine) ...[ ListTile( leading: const Icon(Icons.edit), title: const Text('Edit'), onTap: handleEdit, ), - if (store.isMine) ListTile( leading: Icon(comment.deleted ? Icons.restore : Icons.delete), title: Text(comment.deleted ? 'Restore' : 'Delete'), onTap: loggedInAction(handleDelete), ), + ] else + ListTile( + leading: store.blockingState.isLoading + ? const SizedBox( + height: 20, + width: 20, + child: CircularProgressIndicator(), + ) + : const Icon(Icons.block), + title: Text( + '${store.comment.creatorBlocked ? 'Unblock' : 'Block'} ${store.comment.creator.preferredName}'), + onTap: loggedInAction((token) { + Navigator.of(context).pop(); + store.block(token); + }), + ), ListTile( leading: const Icon(Icons.info_outline), title: const Text('Nerd stuff'), diff --git a/lib/widgets/comment/comment_store.dart b/lib/widgets/comment/comment_store.dart index 86cb836..685dfc3 100644 --- a/lib/widgets/comment/comment_store.dart +++ b/lib/widgets/comment/comment_store.dart @@ -35,6 +35,7 @@ abstract class _CommentStore with Store { final savingState = AsyncStore(); final markPersonMentionAsReadState = AsyncStore(); final markAsReadState = AsyncStore(); + final blockingState = AsyncStore(); @computed bool get isMine => @@ -103,6 +104,21 @@ abstract class _CommentStore with Store { if (result != null) comment = result.commentView; } + @action + Future block(Jwt token) async { + final result = await blockingState.runLemmy( + comment.instanceHost, + BlockPerson( + personId: comment.creator.id, + block: !comment.creatorBlocked, + auth: token.raw, + ), + ); + if (result != null) { + comment = comment.copyWith(creatorBlocked: result.blocked); + } + } + @action Future markAsRead(Jwt token) async { if (userMentionId != null) { diff --git a/lib/widgets/comment/comment_store.g.dart b/lib/widgets/comment/comment_store.g.dart index 5462f05..371a9b4 100644 --- a/lib/widgets/comment/comment_store.g.dart +++ b/lib/widgets/comment/comment_store.g.dart @@ -102,6 +102,13 @@ mixin _$CommentStore on _CommentStore, Store { return _$saveAsyncAction.run(() => super.save(token)); } + final _$blockAsyncAction = AsyncAction('_CommentStore.block'); + + @override + Future block(Jwt token) { + return _$blockAsyncAction.run(() => super.block(token)); + } + final _$markAsReadAsyncAction = AsyncAction('_CommentStore.markAsRead'); @override