lemmur-app-android/lib/pages/settings/blocks/blocks.dart

201 lines
6.7 KiB
Dart
Raw Normal View History

2021-09-14 23:45:26 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2021-11-05 16:00:03 +01:00
import 'package:nested/nested.dart';
2021-09-14 23:45:26 +02:00
2021-11-05 16:00:03 +01:00
import '../../../hooks/logged_in_action.dart';
2021-10-02 17:42:28 +02:00
import '../../../hooks/stores.dart';
import '../../../l10n/l10n_from_string.dart';
2021-10-02 17:42:28 +02:00
import '../../../stores/accounts_store.dart';
2021-10-02 23:15:15 +02:00
import '../../../util/async_store_listener.dart';
import '../../../util/mobx_provider.dart';
2021-10-02 17:42:28 +02:00
import '../../../util/observer_consumers.dart';
import '../../../widgets/pull_to_refresh.dart';
2021-11-05 16:00:03 +01:00
import 'block_dialog.dart';
2021-10-02 20:05:42 +02:00
import 'block_tile.dart';
2021-09-14 23:45:26 +02:00
import 'blocks_store.dart';
class BlocksPage extends HookWidget {
const BlocksPage._();
2021-09-14 23:45:26 +02:00
@override
Widget build(BuildContext context) {
final accStore = useAccountsStore();
return DefaultTabController(
length: accStore.loggedInInstances.length,
child: Scaffold(
appBar: AppBar(
title: const Text('Blocks'),
bottom: TabBar(
isScrollable: true,
tabs: [
for (final instance in accStore.loggedInInstances)
Tab(
child: Text(
'${accStore.defaultUsernameFor(instance)!}@$instance'),
),
],
),
),
body: TabBarView(
children: [
for (final instance in accStore.loggedInInstances)
_UserBlocksWrapper(
2021-09-14 23:45:26 +02:00
instanceHost: instance,
username: accStore.defaultUsernameFor(instance)!,
)
],
),
),
);
}
static Route route() =>
MaterialPageRoute(builder: (context) => const BlocksPage._());
2021-09-14 23:45:26 +02:00
}
class _UserBlocksWrapper extends StatelessWidget {
2021-09-14 23:45:26 +02:00
final String instanceHost;
final String username;
const _UserBlocksWrapper(
{required this.instanceHost, required this.username});
2021-09-14 23:45:26 +02:00
@override
Widget build(BuildContext context) {
return MobxProvider(
2021-09-14 23:45:26 +02:00
create: (context) => BlocksStore(
instanceHost: instanceHost,
token: context
.read<AccountsStore>()
.userDataFor(instanceHost, username)!
.jwt,
)..refresh(),
2021-09-14 23:45:26 +02:00
child: const _UserBlocks(),
);
}
}
2021-11-05 16:00:03 +01:00
class _UserBlocks extends HookWidget {
2021-09-14 23:45:26 +02:00
const _UserBlocks();
@override
Widget build(BuildContext context) {
2021-11-05 16:00:03 +01:00
final loggedInAction =
useLoggedInAction(context.read<BlocksStore>().instanceHost);
return Nested(
children: [
AsyncStoreListener(
asyncStore: context.read<BlocksStore>().blocksState,
),
AsyncStoreListener(
asyncStore: context.read<BlocksStore>().communityBlockingState,
),
AsyncStoreListener(
asyncStore: context.read<BlocksStore>().userBlockingState,
),
],
2021-10-02 23:15:15 +02:00
child: ObserverBuilder<BlocksStore>(
builder: (context, store) {
return PullToRefresh(
2021-10-02 23:15:15 +02:00
onRefresh: store.refresh,
child: ListView(
children: [
if (!store.isUsable) ...[
if (store.blocksState.isLoading)
const Padding(
padding: EdgeInsets.only(top: 64),
child: Center(
child: CircularProgressIndicator.adaptive(),
),
)
else if (store.blocksState.errorTerm != null)
Padding(
padding: const EdgeInsets.only(top: 64),
child: Center(
child: Text(
store.blocksState.errorTerm!.tr(context),
),
),
)
] else ...[
for (final user in store.blockedUsers!)
MobxProvider.value(
value: user,
2021-10-02 23:15:15 +02:00
key: ValueKey(user),
child: const BlockPersonTile(),
),
2021-10-02 23:15:15 +02:00
if (store.blockedUsers!.isEmpty)
const ListTile(
title: Center(
child: Text('No users blocked'),
),
),
2021-11-05 16:00:03 +01:00
ListTile(
leading: Padding(
padding: const EdgeInsets.only(left: 16, right: 10),
child: store.userBlockingState.isLoading
? const CircularProgressIndicator.adaptive()
: const Icon(Icons.add),
),
onTap: store.userBlockingState.isLoading
? null
: loggedInAction(
(token) async {
final person =
await BlockPersonDialog.show(context);
if (person != null) {
await store.blockUser(token, person.person.id);
}
},
),
title: const Text('Block User'),
),
2021-10-02 23:15:15 +02:00
const Divider(),
for (final community in store.blockedCommunities!)
MobxProvider.value(
value: community,
2021-10-02 23:15:15 +02:00
key: ValueKey(community),
child: const BlockCommunityTile(),
),
if (store.blockedCommunities!.isEmpty)
const ListTile(
title: Center(
child: Text('No communities blocked'),
),
),
2021-11-05 16:00:03 +01:00
ListTile(
leading: Padding(
padding: const EdgeInsets.only(left: 16, right: 10),
child: store.communityBlockingState.isLoading
? const CircularProgressIndicator.adaptive()
: const Icon(Icons.add),
),
onTap: store.communityBlockingState.isLoading
? null
: loggedInAction(
(token) async {
final community =
await BlockCommunityDialog.show(context);
if (community != null) {
await store.blockCommunity(
token,
community.community.id,
);
}
},
),
title: const Text('Block Community'),
),
2021-10-02 23:15:15 +02:00
],
],
),
);
},
),
2021-09-14 23:45:26 +02:00
);
}
}