2020-09-14 23:58:59 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
2020-09-09 18:51:48 +02:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2020-09-16 00:26:36 +02:00
|
|
|
import 'package:fuzzy/fuzzy.dart';
|
2020-09-09 18:51:48 +02:00
|
|
|
import 'package:lemmy_api_client/lemmy_api_client.dart';
|
|
|
|
|
2020-09-16 01:27:49 +02:00
|
|
|
import '../hooks/delayed_loading.dart';
|
2020-09-17 00:24:49 +02:00
|
|
|
import '../hooks/memo_future.dart';
|
|
|
|
import '../hooks/stores.dart';
|
2020-09-15 15:14:24 +02:00
|
|
|
import '../util/extensions/iterators.dart';
|
2020-09-09 18:51:48 +02:00
|
|
|
import '../util/text_color.dart';
|
|
|
|
|
|
|
|
class CommunitiesTab extends HookWidget {
|
|
|
|
CommunitiesTab();
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-09-16 23:22:04 +02:00
|
|
|
final theme = Theme.of(context);
|
|
|
|
final filterController = useTextEditingController();
|
2020-09-10 23:19:44 +02:00
|
|
|
useValueListenable(filterController);
|
2020-09-16 23:15:42 +02:00
|
|
|
final accountsStore = useAccountsStore();
|
2020-09-14 23:54:47 +02:00
|
|
|
|
2020-09-17 00:24:49 +02:00
|
|
|
final amountOfDisplayInstances = useMemoized(() => accountsStore.users.keys
|
|
|
|
.where((e) => !accountsStore.isAnonymousFor(e))
|
|
|
|
.length);
|
2020-09-16 23:22:04 +02:00
|
|
|
final isCollapsed = useState(List.filled(amountOfDisplayInstances, false));
|
2020-09-10 23:19:44 +02:00
|
|
|
|
2020-09-16 23:29:14 +02:00
|
|
|
final instancesSnap = useMemoFuture(() {
|
2020-09-16 23:22:04 +02:00
|
|
|
final futures = accountsStore.users.keys
|
2020-09-09 18:51:48 +02:00
|
|
|
.where((e) => !accountsStore.isAnonymousFor(e))
|
|
|
|
.map(
|
|
|
|
(instanceUrl) =>
|
|
|
|
LemmyApi(instanceUrl).v1.getSite().then((e) => e.site),
|
|
|
|
)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
return Future.wait(futures);
|
|
|
|
});
|
2020-09-16 23:29:14 +02:00
|
|
|
final communitiesSnap = useMemoFuture(() {
|
2020-09-16 23:22:04 +02:00
|
|
|
final futures = accountsStore.users.keys
|
2020-09-09 18:51:48 +02:00
|
|
|
.where((e) => !accountsStore.isAnonymousFor(e))
|
|
|
|
.map(
|
|
|
|
(instanceUrl) => LemmyApi(instanceUrl)
|
|
|
|
.v1
|
|
|
|
.getUserDetails(
|
|
|
|
sort: SortType.active,
|
|
|
|
savedOnly: false,
|
|
|
|
userId: accountsStore.defaultTokenFor(instanceUrl).payload.id,
|
|
|
|
)
|
|
|
|
.then((e) => e.follows),
|
|
|
|
)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
return Future.wait(futures);
|
|
|
|
});
|
|
|
|
|
2020-09-16 00:40:19 +02:00
|
|
|
if (communitiesSnap.hasError || instancesSnap.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: [
|
|
|
|
Icon(Icons.error),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Text(
|
|
|
|
communitiesSnap.error?.toString() ??
|
|
|
|
instancesSnap.error?.toString(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else if (!communitiesSnap.hasData || !instancesSnap.hasData) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(),
|
2020-09-09 18:51:48 +02:00
|
|
|
body: Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-16 23:22:04 +02:00
|
|
|
final instances = instancesSnap.data;
|
|
|
|
final communities = communitiesSnap.data
|
2020-09-14 23:54:47 +02:00
|
|
|
..forEach(
|
|
|
|
(e) => e.sort((a, b) => a.communityName.compareTo(b.communityName)));
|
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) {
|
|
|
|
return Icon(Icons.filter_list);
|
|
|
|
}
|
|
|
|
|
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:35:14 +02:00
|
|
|
icon: Icon(Icons.clear),
|
2020-09-10 23:19:44 +02:00
|
|
|
);
|
|
|
|
}();
|
|
|
|
|
2020-09-16 00:26:36 +02:00
|
|
|
filterCommunities(List<CommunityFollowerView> comm) {
|
2020-09-16 23:22:04 +02:00
|
|
|
final matches = Fuzzy(
|
2020-09-16 00:26:36 +02:00
|
|
|
comm.map((e) => e.communityName).toList(),
|
|
|
|
options: FuzzyOptions(threshold: 0.5),
|
|
|
|
).search(filterController.text).map((e) => e.item);
|
|
|
|
|
|
|
|
return matches
|
|
|
|
.map((match) => comm.firstWhere((e) => e.communityName == 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(
|
|
|
|
icon: Icon(Icons.style),
|
|
|
|
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:09:02 +02:00
|
|
|
isDense: true,
|
2020-09-10 23:19:44 +02:00
|
|
|
border: OutlineInputBorder(),
|
2020-09-15 00:23:52 +02:00
|
|
|
hintText: 'Filter', // TODO: hint with an filter icon
|
2020-09-10 23:19:44 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2020-09-14 23:54:47 +02:00
|
|
|
body: ListView(
|
2020-09-10 23:19:44 +02:00
|
|
|
children: [
|
2020-09-16 23:22:04 +02:00
|
|
|
for (final i in Iterable.generate(amountOfDisplayInstances))
|
2020-09-10 23:19:44 +02:00
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
ListTile(
|
2020-09-14 23:54:47 +02:00
|
|
|
onLongPress: () => toggleCollapse(i),
|
2020-09-10 23:19:44 +02:00
|
|
|
leading: instances[i].icon != null
|
|
|
|
? CachedNetworkImage(
|
|
|
|
height: 50,
|
|
|
|
width: 50,
|
|
|
|
imageUrl: instances[i].icon,
|
|
|
|
imageBuilder: (context, imageProvider) => Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
image: DecorationImage(
|
|
|
|
fit: BoxFit.cover, image: imageProvider),
|
2020-09-09 19:25:12 +02:00
|
|
|
),
|
2020-09-10 23:19:44 +02:00
|
|
|
),
|
|
|
|
errorWidget: (_, __, ___) => SizedBox(width: 50),
|
|
|
|
)
|
|
|
|
: SizedBox(width: 50),
|
|
|
|
title: Text(
|
|
|
|
instances[i].name,
|
|
|
|
style: theme.textTheme.headline6,
|
2020-09-09 18:51:48 +02:00
|
|
|
),
|
2020-09-14 23:54:47 +02:00
|
|
|
trailing: IconButton(
|
|
|
|
icon: Icon(isCollapsed.value[i]
|
|
|
|
? Icons.keyboard_arrow_up
|
|
|
|
: Icons.keyboard_arrow_down),
|
|
|
|
onPressed: () => toggleCollapse(i),
|
|
|
|
),
|
2020-09-10 23:19:44 +02:00
|
|
|
),
|
2020-09-14 23:54:47 +02:00
|
|
|
if (!isCollapsed.value[i])
|
2020-09-16 23:22:04 +02:00
|
|
|
for (final comm in filterCommunities(communities[i]))
|
2020-09-14 23:54:47 +02:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(left: 17),
|
|
|
|
child: ListTile(
|
|
|
|
dense: true,
|
|
|
|
leading: VerticalDivider(
|
|
|
|
color: theme.hintColor,
|
|
|
|
),
|
|
|
|
title: Row(
|
|
|
|
children: [
|
|
|
|
if (comm.communityIcon != null)
|
|
|
|
CachedNetworkImage(
|
|
|
|
height: 30,
|
|
|
|
width: 30,
|
|
|
|
imageUrl: comm.communityIcon,
|
|
|
|
imageBuilder: (context, imageProvider) =>
|
|
|
|
Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
image: DecorationImage(
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
image: imageProvider),
|
|
|
|
),
|
2020-09-09 19:25:12 +02:00
|
|
|
),
|
2020-09-14 23:54:47 +02:00
|
|
|
errorWidget: (_, __, ___) =>
|
|
|
|
SizedBox(width: 30),
|
|
|
|
)
|
|
|
|
else
|
|
|
|
SizedBox(width: 30),
|
|
|
|
SizedBox(width: 10),
|
|
|
|
Text('!${comm.communityName}'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
trailing: _CommunitySubscribeToggle(
|
|
|
|
instanceUrl: comm.communityActorId.split('/')[2],
|
|
|
|
communityId: comm.communityId,
|
|
|
|
),
|
2020-09-10 23:19:44 +02:00
|
|
|
),
|
2020-09-14 23:54:47 +02:00
|
|
|
)
|
2020-09-10 23:19:44 +02: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 instanceUrl;
|
|
|
|
|
2020-09-09 18:53:24 +02:00
|
|
|
_CommunitySubscribeToggle(
|
|
|
|
{@required this.instanceUrl, @required this.communityId})
|
2020-09-09 18:51:48 +02:00
|
|
|
: assert(instanceUrl != null),
|
|
|
|
assert(communityId != null);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-09-16 23:22:04 +02:00
|
|
|
final theme = Theme.of(context);
|
|
|
|
final subbed = useState(true);
|
|
|
|
final delayed = useDelayedLoading(const Duration(milliseconds: 500));
|
2020-09-16 23:15:42 +02:00
|
|
|
final accountsStore = useAccountsStore();
|
2020-09-14 23:58:59 +02:00
|
|
|
|
2020-09-15 23:44:21 +02:00
|
|
|
handleTap() async {
|
2020-09-16 01:27:49 +02:00
|
|
|
delayed.start();
|
2020-09-14 23:58:59 +02:00
|
|
|
|
2020-09-15 23:44:21 +02:00
|
|
|
try {
|
|
|
|
await LemmyApi(instanceUrl).v1.followCommunity(
|
|
|
|
communityId: communityId,
|
|
|
|
follow: !subbed.value,
|
2020-09-16 23:15:42 +02:00
|
|
|
auth: accountsStore.defaultTokenFor(instanceUrl).raw,
|
2020-09-15 23:44:21 +02:00
|
|
|
);
|
|
|
|
subbed.value = !subbed.value;
|
|
|
|
} on Exception catch (err) {
|
|
|
|
Scaffold.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text('Failed to ${subbed.value ? 'un' : ''}follow: $err'),
|
|
|
|
));
|
|
|
|
}
|
2020-09-16 01:27:49 +02:00
|
|
|
|
|
|
|
delayed.cancel();
|
2020-09-15 23:44:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return InkWell(
|
2020-09-16 01:27:49 +02:00
|
|
|
onTap: delayed.pending ? () {} : 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(
|
2020-09-15 23:44:21 +02:00
|
|
|
color: subbed.value ? theme.accentColor : null,
|
2020-09-09 19:22:20 +02:00
|
|
|
border: Border.all(color: theme.accentColor),
|
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
|
2020-09-09 19:22:20 +02:00
|
|
|
? Container(
|
|
|
|
width: 20, height: 20, child: CircularProgressIndicator())
|
|
|
|
: Icon(
|
2020-09-15 23:44:21 +02:00
|
|
|
subbed.value ? Icons.done : Icons.add,
|
|
|
|
color: subbed.value
|
2020-09-09 19:22:20 +02:00
|
|
|
? textColorBasedOnBackground(theme.accentColor)
|
|
|
|
: theme.accentColor,
|
|
|
|
size: 20,
|
|
|
|
),
|
2020-09-09 18:51:48 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|