From 88466dfd73fb49f7053fae81f0d3d926ab8613fd Mon Sep 17 00:00:00 2001 From: krawieck Date: Tue, 14 Sep 2021 23:48:04 +0200 Subject: [PATCH] add community blocking + minor fixes minor fixes: * make link preview show up in proper times * ListTile for (un)blocking communities had a thing switched around --- lib/pages/full_post/full_post.dart | 59 +++++++++++++++++-------- lib/widgets/post/full_post_store.dart | 17 ++++++- lib/widgets/post/full_post_store.g.dart | 22 +++++++++ lib/widgets/post/post_link_preview.dart | 3 +- lib/widgets/post/post_more_menu.dart | 25 ++++++----- 5 files changed, 95 insertions(+), 31 deletions(-) diff --git a/lib/pages/full_post/full_post.dart b/lib/pages/full_post/full_post.dart index ac27d00..5f2589f 100644 --- a/lib/pages/full_post/full_post.dart +++ b/lib/pages/full_post/full_post.dart @@ -39,7 +39,15 @@ class FullPostPage extends StatelessWidget { create: (context) => fullPostStore, builder: (context, store) => AsyncStoreListener( asyncStore: context.read().fullPostState, - child: const _FullPostPage(), + child: AsyncStoreListener( + asyncStore: context.read().communityBlockingState, + successMessageBuilder: (context, asyncStore) { + final name = + asyncStore.data.communityView.community.originPreferredName; + return '${asyncStore.data.blocked ? 'Blocked' : 'Unblocked'} $name'; + }, + child: const _FullPostPage(), + ), ), ); } @@ -65,17 +73,11 @@ class _FullPostPage extends HookWidget { return Scaffold( appBar: AppBar(), body: Center( - child: (store.fullPostState.isLoading) - ? const CircularProgressIndicator.adaptive() - : Column( - children: [ - const Text('Post failed to load'), - ElevatedButton.icon( - onPressed: store.refresh, - icon: const Icon(Icons.refresh), - label: const Text('try again')) - ], - )), + child: (store.fullPostState.isLoading) + ? const CircularProgressIndicator.adaptive() + : FailedToLoad( + message: 'Post failed to load', refresh: store.refresh), + ), ); } @@ -114,7 +116,7 @@ class _FullPostPage extends HookWidget { ), ), actions: [ - IconButton(icon: const Icon(Icons.share), onPressed: sharePost), + IconButton(icon: Icon(shareIcon), onPressed: sharePost), Provider( create: (context) => store.postStore!, child: const SavePostButton(), @@ -168,12 +170,8 @@ class _Comments extends StatelessWidget { } else if (store.fullPostState.errorTerm != null) { return Padding( padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 30), - child: Column( - children: [ - const Icon(Icons.error), - Text('Error: ${store.fullPostState.errorTerm}') - ], - ), + child: FailedToLoad( + message: 'Comments failed to load', refresh: store.refresh), ); } else { return const Padding( @@ -185,3 +183,26 @@ class _Comments extends StatelessWidget { ); } } + +class FailedToLoad extends StatelessWidget { + final String message; + final VoidCallback refresh; + + const FailedToLoad({required this.refresh, required this.message}); + + @override + Widget build(BuildContext context) { + return Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text(message), + const SizedBox(height: 5), + ElevatedButton.icon( + onPressed: refresh, + icon: const Icon(Icons.refresh), + label: const Text('try again'), + ) + ], + ); + } +} diff --git a/lib/widgets/post/full_post_store.dart b/lib/widgets/post/full_post_store.dart index cb8a269..999ca79 100644 --- a/lib/widgets/post/full_post_store.dart +++ b/lib/widgets/post/full_post_store.dart @@ -40,10 +40,25 @@ abstract class _FullPostStore with Store { postStore ??= PostStore(result.postView); fullPostView = result; - postStore?.postView = result.postView; + postStore!.postView = result.postView; } } + @action + Future blockCommunity(Jwt token) async { + final result = await communityBlockingState.runLemmy( + instanceHost, + BlockCommunity( + communityId: fullPostView!.communityView.community.id, + block: !fullPostView!.communityView.blocked, + auth: token.raw)); + if (result != null) { + fullPostView = + fullPostView!.copyWith(communityView: result.communityView); + } + } + + @action void addComment(CommentView commentView) => newComments.insert(0, commentView); diff --git a/lib/widgets/post/full_post_store.g.dart b/lib/widgets/post/full_post_store.g.dart index 3757f52..093360e 100644 --- a/lib/widgets/post/full_post_store.g.dart +++ b/lib/widgets/post/full_post_store.g.dart @@ -76,6 +76,28 @@ mixin _$FullPostStore on _FullPostStore, Store { return _$refreshAsyncAction.run(() => super.refresh()); } + final _$blockCommunityAsyncAction = + AsyncAction('_FullPostStore.blockCommunity'); + + @override + Future blockCommunity(Jwt token) { + return _$blockCommunityAsyncAction.run(() => super.blockCommunity(token)); + } + + final _$_FullPostStoreActionController = + ActionController(name: '_FullPostStore'); + + @override + void addComment(CommentView commentView) { + final _$actionInfo = _$_FullPostStoreActionController.startAction( + name: '_FullPostStore.addComment'); + try { + return super.addComment(commentView); + } finally { + _$_FullPostStoreActionController.endAction(_$actionInfo); + } + } + @override String toString() { return ''' diff --git a/lib/widgets/post/post_link_preview.dart b/lib/widgets/post/post_link_preview.dart index 0719b59..0134bab 100644 --- a/lib/widgets/post/post_link_preview.dart +++ b/lib/widgets/post/post_link_preview.dart @@ -11,7 +11,8 @@ class PostLinkPreview extends StatelessWidget { Widget build(BuildContext context) { return ObserverBuilder( builder: (context, store) { - if (store.postView.post.url == null || + if (store.hasMedia || + store.postView.post.url == null || store.postView.post.url!.isEmpty) { return const SizedBox(); } diff --git a/lib/widgets/post/post_more_menu.dart b/lib/widgets/post/post_more_menu.dart index e86d08f..37839ec 100644 --- a/lib/widgets/post/post_more_menu.dart +++ b/lib/widgets/post/post_more_menu.dart @@ -112,16 +112,21 @@ class PostMoreMenu extends HookWidget { ), if (fullPostStore != null && fullPostStore!.fullPostView != null) ObserverBuilder( - store: fullPostStore, - builder: (context, store) { - return ListTile( - leading: store.communityBlockingState.isLoading - ? const CircularProgressIndicator.adaptive() - : const Icon(Icons.block), - title: Text( - '${store.fullPostView!.communityView.blocked ? 'Block' : 'Unblock'} community'), - ); - }), + store: fullPostStore, + builder: (context, store) { + return ListTile( + leading: store.communityBlockingState.isLoading + ? const CircularProgressIndicator.adaptive() + : const Icon(Icons.block), + title: Text( + '${store.fullPostView!.communityView.blocked ? 'Unblock' : 'Block'} community'), + onTap: () { + Navigator.of(context).pop(); + loggedInAction(store.blockCommunity)(); + }, + ); + }, + ), ListTile( leading: const Icon(Icons.info_outline), title: const Text('Nerd stuff'),