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
This commit is contained in:
krawieck 2021-09-14 23:48:04 +02:00
parent 5ca4748572
commit 88466dfd73
5 changed files with 95 additions and 31 deletions

View File

@ -39,7 +39,15 @@ class FullPostPage extends StatelessWidget {
create: (context) => fullPostStore,
builder: (context, store) => AsyncStoreListener(
asyncStore: context.read<FullPostStore>().fullPostState,
child: const _FullPostPage(),
child: AsyncStoreListener<BlockedCommunity>(
asyncStore: context.read<FullPostStore>().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<PostStore>(
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'),
)
],
);
}
}

View File

@ -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<void> 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);

View File

@ -76,6 +76,28 @@ mixin _$FullPostStore on _FullPostStore, Store {
return _$refreshAsyncAction.run(() => super.refresh());
}
final _$blockCommunityAsyncAction =
AsyncAction('_FullPostStore.blockCommunity');
@override
Future<void> 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 '''

View File

@ -11,7 +11,8 @@ class PostLinkPreview extends StatelessWidget {
Widget build(BuildContext context) {
return ObserverBuilder<PostStore>(
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();
}

View File

@ -112,16 +112,21 @@ class PostMoreMenu extends HookWidget {
),
if (fullPostStore != null && fullPostStore!.fullPostView != null)
ObserverBuilder<FullPostStore>(
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'),