lemmur-app-android/lib/pages/communities_tab.dart

302 lines
9.9 KiB
Dart
Raw Normal View History

2020-09-14 23:58:59 +02:00
import 'dart:async';
2022-05-03 09:48:04 +02:00
import 'package:collection/collection.dart';
2020-09-09 18:51:48 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:fuzzy/fuzzy.dart';
2021-04-05 20:14:39 +02:00
import 'package:lemmy_api_client/v3.dart';
2020-09-09 18:51:48 +02:00
2020-09-16 01:27:49 +02:00
import '../hooks/delayed_loading.dart';
import '../hooks/logged_in_action.dart';
2021-01-09 17:34:24 +01:00
import '../hooks/refreshable.dart';
2020-09-17 00:24:49 +02:00
import '../hooks/stores.dart';
import '../util/extensions/api.dart';
import '../util/extensions/iterators.dart';
2020-09-30 01:23:04 +02:00
import '../util/goto.dart';
2020-09-09 18:51:48 +02:00
import '../util/text_color.dart';
2021-02-18 09:19:00 +01:00
import '../widgets/avatar.dart';
import '../widgets/pull_to_refresh.dart';
import 'instance/instance.dart';
2020-09-09 18:51:48 +02:00
2020-09-30 19:05:00 +02:00
/// List of subscribed communities per instance
2020-09-09 18:51:48 +02:00
class CommunitiesTab extends HookWidget {
2021-01-03 18:21:56 +01:00
const CommunitiesTab();
2020-09-09 18:51:48 +02:00
@override
Widget build(BuildContext context) {
2020-09-16 23:22:04 +02:00
final theme = Theme.of(context);
final filterController = useListenable(useTextEditingController());
2020-09-16 23:15:42 +02:00
final accountsStore = useAccountsStore();
2020-09-14 23:54:47 +02:00
final amountOfDisplayInstances = accountsStore.loggedInInstances.length;
2020-09-16 23:22:04 +02:00
final isCollapsed = useState(List.filled(amountOfDisplayInstances, false));
2020-09-10 23:19:44 +02:00
if (amountOfDisplayInstances != isCollapsed.value.length) {
isCollapsed.value = List.filled(amountOfDisplayInstances, false);
}
2021-01-09 02:31:53 +01:00
getInstances() {
final futures = accountsStore.loggedInInstances
2020-09-09 18:51:48 +02:00
.map(
2021-04-05 20:14:39 +02:00
(instanceHost) => LemmyApiV3(instanceHost)
2021-02-24 20:52:18 +01:00
.run(const GetSite())
.then((e) => e.siteView!.site),
2020-09-09 18:51:48 +02:00
)
.toList();
return Future.wait(futures);
2021-01-09 02:31:53 +01:00
}
getCommunities() {
final futures = accountsStore.loggedInInstances
2020-09-09 18:51:48 +02:00
.map(
2021-04-05 20:14:39 +02:00
(instanceHost) => LemmyApiV3(instanceHost)
2021-08-26 00:27:24 +02:00
.run(GetSite(
2021-04-11 18:27:22 +02:00
auth: accountsStore.defaultUserDataFor(instanceHost)!.jwt.raw,
2021-01-24 20:01:55 +01:00
))
2021-08-26 00:27:24 +02:00
.then((e) => e.myUser!.follows),
2020-09-09 18:51:48 +02:00
)
.toList();
return Future.wait(futures);
2021-01-09 02:31:53 +01:00
}
2022-04-30 15:44:03 +02:00
final loggedInAccounts = accountsStore.loggedInInstances
.map((instanceHost) =>
'$instanceHost${accountsStore.defaultUsernameFor(instanceHost)}')
.toList();
2022-04-30 15:44:03 +02:00
final instancesRefreshable = useRefreshable(getInstances, loggedInAccounts);
final communitiesRefreshable =
2022-04-30 15:44:03 +02:00
useRefreshable(getCommunities, loggedInAccounts);
2021-01-09 02:31:53 +01:00
2021-01-09 17:34:24 +01:00
if (communitiesRefreshable.snapshot.hasError ||
instancesRefreshable.snapshot.hasError) {
2020-09-09 18:51:48 +02:00
return Scaffold(
2020-09-16 00:40:19 +02:00
appBar: AppBar(),
body: Center(
child: Row(
children: [
2021-01-03 19:43:39 +01:00
const Icon(Icons.error),
2020-09-16 00:40:19 +02:00
Padding(
2021-01-03 18:03:59 +01:00
padding: const EdgeInsets.all(8),
2020-09-16 00:40:19 +02:00
child: Text(
2021-01-09 17:34:24 +01:00
communitiesRefreshable.snapshot.error?.toString() ??
instancesRefreshable.snapshot.error!.toString(),
2020-09-16 00:40:19 +02:00
),
)
],
),
),
);
2021-01-09 17:34:24 +01:00
} else if (!communitiesRefreshable.snapshot.hasData ||
!instancesRefreshable.snapshot.hasData) {
2020-09-16 00:40:19 +02:00
return Scaffold(
appBar: AppBar(),
2021-01-03 19:43:39 +01:00
body: const Center(
child: CircularProgressIndicator.adaptive(),
2020-09-09 18:51:48 +02:00
),
);
}
2021-01-09 02:31:53 +01:00
refresh() async {
try {
2021-01-09 17:34:24 +01:00
await Future.wait([
instancesRefreshable.refresh(),
communitiesRefreshable.refresh(),
]);
2021-01-09 02:31:53 +01:00
} catch (e) {
2021-03-10 08:34:30 +01:00
ScaffoldMessenger.of(context)
2021-01-09 02:31:53 +01:00
.showSnackBar(SnackBar(content: Text(e.toString())));
}
}
final instances = instancesRefreshable.snapshot.data!;
final communities = communitiesRefreshable.snapshot.data!
2022-05-03 09:48:04 +02:00
.map(
(e) =>
e.sorted((a, b) => a.community.name.compareTo(b.community.name)),
)
.toList();
2020-09-09 18:51:48 +02:00
2020-09-16 23:22:04 +02:00
final filterIcon = () {
2020-09-10 23:19:44 +02:00
if (filterController.text.isEmpty) {
2021-01-03 19:43:39 +01:00
return const Icon(Icons.filter_list);
2020-09-10 23:19:44 +02:00
}
2020-09-10 23:35:14 +02:00
return IconButton(
onPressed: () {
2020-09-10 23:19:44 +02:00
filterController.clear();
primaryFocus?.unfocus();
2020-09-10 23:19:44 +02:00
},
2021-01-03 19:43:39 +01:00
icon: const Icon(Icons.clear),
2020-09-10 23:19:44 +02:00
);
}();
filterCommunities(List<CommunityFollowerView> comm) {
2020-09-16 23:22:04 +02:00
final matches = Fuzzy(
2021-01-24 20:01:55 +01:00
comm.map((e) => e.community.name).toList(),
options: FuzzyOptions(threshold: 0.5),
).search(filterController.text).map((e) => e.item);
return matches
2021-01-24 20:01:55 +01:00
.map((match) => comm.firstWhere((e) => e.community.name == match));
}
2020-09-10 23:19:44 +02:00
2020-09-14 23:54:47 +02:00
toggleCollapse(int i) => isCollapsed.value =
isCollapsed.value.mapWithIndex((e, j) => j == i ? !e : e).toList();
2020-09-09 18:51:48 +02:00
return Scaffold(
2020-09-10 23:19:44 +02:00
appBar: AppBar(
2020-09-14 23:54:47 +02:00
actions: [
IconButton(
2021-01-03 19:43:39 +01:00
icon: const Icon(Icons.style),
2020-09-14 23:54:47 +02:00
onPressed: () {}, // TODO: change styles?
),
],
2020-09-10 23:19:44 +02:00
title: TextField(
controller: filterController,
textAlign: TextAlign.center,
decoration: InputDecoration(
suffixIcon: filterIcon,
2020-09-15 00:23:52 +02:00
hintText: 'Filter', // TODO: hint with an filter icon
2020-09-10 23:19:44 +02:00
),
),
),
body: PullToRefresh(
2021-01-09 02:31:53 +01:00
onRefresh: refresh,
child: amountOfDisplayInstances == 0
? const Center(
child: Text('You are not logged in to any instances'),
)
: ListView(
2021-01-09 02:31:53 +01:00
children: [
for (var i = 0; i < amountOfDisplayInstances; i++)
Column(
children: [
ListTile(
onTap: () => Navigator.of(context).push(
InstancePage.route(
accountsStore.loggedInInstances.elementAt(i),
),
),
onLongPress: () => toggleCollapse(i),
2021-04-16 20:41:33 +02:00
leading: Avatar(
url: instances[i].icon,
alwaysShow: true,
),
title: Text(
instances[i].name,
style: theme.textTheme.headline6,
2021-01-09 02:31:53 +01:00
),
trailing: IconButton(
icon: Icon(isCollapsed.value[i]
? Icons.keyboard_arrow_up
: Icons.keyboard_arrow_down),
onPressed: () => toggleCollapse(i),
2021-01-09 02:31:53 +01:00
),
2020-09-14 23:54:47 +02:00
),
if (!isCollapsed.value[i])
for (final comm in filterCommunities(communities[i]))
Padding(
padding: const EdgeInsets.only(left: 17),
child: ListTile(
onTap: () => goToCommunity.byId(
context,
accountsStore.loggedInInstances
.elementAt(i),
comm.community.id),
dense: true,
leading: VerticalDivider(
color: theme.hintColor,
),
title: Row(
children: [
2021-02-18 09:19:00 +01:00
Avatar(
radius: 15,
url: comm.community.icon,
2021-04-16 20:41:33 +02:00
alwaysShow: true,
2021-02-18 09:19:00 +01:00
),
const SizedBox(width: 10),
2021-04-29 11:38:28 +02:00
Text(comm.community.originPreferredName),
],
),
trailing: _CommunitySubscribeToggle(
key: ValueKey(comm.community.id),
instanceHost: comm.instanceHost,
communityId: comm.community.id,
),
),
)
],
),
2021-01-09 02:31:53 +01:00
],
),
2020-09-09 18:51:48 +02:00
),
);
}
}
2020-09-09 18:53:24 +02:00
class _CommunitySubscribeToggle extends HookWidget {
2020-09-09 18:51:48 +02:00
final int communityId;
final String instanceHost;
2020-09-09 18:51:48 +02:00
2022-05-11 22:23:18 +02:00
const _CommunitySubscribeToggle({
required this.instanceHost,
required this.communityId,
super.key,
});
2020-09-09 18:51:48 +02:00
@override
Widget build(BuildContext context) {
2020-09-16 23:22:04 +02:00
final theme = Theme.of(context);
final subbed = useState(true);
2021-01-03 18:03:59 +01:00
final delayed = useDelayedLoading();
final loggedInAction = useLoggedInAction(instanceHost);
2020-09-14 23:58:59 +02:00
handleTap(Jwt token) async {
2020-09-16 01:27:49 +02:00
delayed.start();
2020-09-14 23:58:59 +02:00
try {
2021-04-05 20:14:39 +02:00
await LemmyApiV3(instanceHost).run(FollowCommunity(
2021-01-24 20:01:55 +01:00
communityId: communityId,
follow: !subbed.value,
auth: token.raw,
2021-01-24 20:01:55 +01:00
));
subbed.value = !subbed.value;
} on Exception catch (err) {
2021-03-10 08:34:30 +01:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Failed to ${subbed.value ? 'un' : ''}follow: $err'),
));
}
2020-09-16 01:27:49 +02:00
delayed.cancel();
}
return InkWell(
onTap: delayed.pending ? () {} : loggedInAction(handleTap),
2020-09-09 18:51:48 +02:00
child: Container(
2020-09-16 01:27:49 +02:00
decoration: delayed.loading
2020-09-09 19:22:20 +02:00
? null
: BoxDecoration(
color: subbed.value ? theme.colorScheme.secondary : null,
border: Border.all(color: theme.colorScheme.secondary),
2020-09-16 01:28:40 +02:00
borderRadius: BorderRadius.circular(7),
2020-09-09 19:22:20 +02:00
),
2020-09-16 01:27:49 +02:00
child: delayed.loading
2021-01-03 19:43:39 +01:00
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator.adaptive())
2020-09-09 19:22:20 +02:00
: Icon(
subbed.value ? Icons.done : Icons.add,
color: subbed.value
? textColorBasedOnBackground(theme.colorScheme.secondary)
: theme.colorScheme.secondary,
2020-09-09 19:22:20 +02:00
size: 20,
),
2020-09-09 18:51:48 +02:00
),
);
}
}