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

196 lines
6.6 KiB
Dart
Raw Normal View History

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';
import 'package:lemmy_api_client/lemmy_api_client.dart';
import 'package:provider/provider.dart';
import '../stores/accounts_store.dart';
import '../util/text_color.dart';
class CommunitiesTab extends HookWidget {
CommunitiesTab();
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var instancesFut = useMemoized(() {
var accountsStore = context.watch<AccountsStore>();
var futures = accountsStore.users.keys
.where((e) => !accountsStore.isAnonymousFor(e))
.map(
(instanceUrl) =>
LemmyApi(instanceUrl).v1.getSite().then((e) => e.site),
)
.toList();
return Future.wait(futures);
});
var communitiesFut = useMemoized(() {
var accountsStore = context.watch<AccountsStore>();
var futures = accountsStore.users.keys
.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);
});
var communitiesSnap = useFuture(communitiesFut);
var instancesSnap = useFuture(instancesFut);
if (!communitiesSnap.hasData || !instancesSnap.hasData) {
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
}
var instances = instancesSnap.data;
var communities = communitiesSnap.data;
return Scaffold(
appBar: AppBar(),
body: Column(
children: Iterable.generate(instances.length)
.map(
(i) => Column(
children: [
ListTile(
2020-09-09 19:25:12 +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),
),
),
errorWidget: (_, __, ___) => SizedBox(width: 50),
)
: SizedBox(width: 50),
2020-09-09 18:51:48 +02:00
title: Text(
instances[i].name,
style: theme.textTheme.headline6,
),
),
for (var comm in communities[i])
Padding(
2020-09-09 20:47:34 +02:00
padding: const EdgeInsets.only(left: 17),
2020-09-09 18:51:48 +02:00
child: ListTile(
2020-09-09 19:22:20 +02:00
dense: true,
2020-09-09 20:47:34 +02:00
leading: VerticalDivider(
color: theme.hintColor,
),
title: Row(
children: [
if (comm.communityIcon != null)
CachedNetworkImage(
2020-09-09 19:25:12 +02:00
height: 30,
width: 30,
imageUrl: comm.communityIcon,
imageBuilder: (context, imageProvider) =>
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover,
image: imageProvider),
),
),
errorWidget: (_, __, ___) =>
SizedBox(width: 30),
)
2020-09-09 20:47:34 +02:00
else
SizedBox(width: 30),
SizedBox(width: 10),
Text('!${comm.communityName}'),
],
),
2020-09-09 18:53:24 +02:00
trailing: _CommunitySubscribeToggle(
2020-09-09 19:22:20 +02:00
instanceUrl: comm.communityActorId.split('/')[2],
communityId: comm.communityId,
2020-09-09 18:51:48 +02:00
),
),
)
],
),
)
.toList(),
),
);
}
}
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) {
var theme = Theme.of(context);
var subed = useState(true);
2020-09-09 19:22:20 +02:00
var loading = useState(false);
2020-09-09 18:51:48 +02:00
return InkWell(
2020-09-09 19:22:20 +02:00
onTap: () async {
loading.value = true;
try {
await LemmyApi(instanceUrl).v1.followCommunity(
communityId: communityId,
follow: !subed.value,
auth: context
.read<AccountsStore>()
.defaultTokenFor(instanceUrl)
.raw,
);
subed.value = !subed.value;
} on Exception catch (err) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('Failed to ${subed.value ? 'un' : ''}follow: $err'),
));
}
loading.value = false;
2020-09-09 18:51:48 +02:00
},
child: Container(
2020-09-09 19:22:20 +02:00
decoration: loading.value
? null
: BoxDecoration(
color: subed.value ? theme.accentColor : null,
border: Border.all(color: theme.accentColor),
borderRadius: BorderRadius.circular(5),
),
child: loading.value
? Container(
width: 20, height: 20, child: CircularProgressIndicator())
: Icon(
subed.value ? Icons.done : Icons.add,
color: subed.value
? textColorBasedOnBackground(theme.accentColor)
: theme.accentColor,
size: 20,
),
2020-09-09 18:51:48 +02:00
),
);
}
}