sort communities and toggle instances

This commit is contained in:
shilangyu 2020-09-14 23:54:47 +02:00
parent 2238e3303b
commit 6dac4ac72c
2 changed files with 75 additions and 40 deletions

View File

@ -5,6 +5,7 @@ import 'package:lemmy_api_client/lemmy_api_client.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import '../stores/accounts_store.dart'; import '../stores/accounts_store.dart';
import '../util/iterators.dart';
import '../util/text_color.dart'; import '../util/text_color.dart';
class CommunitiesTab extends HookWidget { class CommunitiesTab extends HookWidget {
@ -15,6 +16,14 @@ class CommunitiesTab extends HookWidget {
var theme = Theme.of(context); var theme = Theme.of(context);
var filterController = useTextEditingController(); var filterController = useTextEditingController();
useValueListenable(filterController); useValueListenable(filterController);
var amountOfDisplayInstances = useMemoized(() {
var accountsStore = context.watch<AccountsStore>();
return accountsStore.users.keys
.where((e) => !accountsStore.isAnonymousFor(e))
.length;
});
var isCollapsed = useState(List.filled(amountOfDisplayInstances, false));
var instancesFut = useMemoized(() { var instancesFut = useMemoized(() {
var accountsStore = context.watch<AccountsStore>(); var accountsStore = context.watch<AccountsStore>();
@ -61,7 +70,9 @@ class CommunitiesTab extends HookWidget {
} }
var instances = instancesSnap.data; var instances = instancesSnap.data;
var communities = communitiesSnap.data; var communities = communitiesSnap.data
..forEach(
(e) => e.sort((a, b) => a.communityName.compareTo(b.communityName)));
var filterIcon = () { var filterIcon = () {
if (filterController.text.isEmpty) { if (filterController.text.isEmpty) {
@ -82,9 +93,17 @@ class CommunitiesTab extends HookWidget {
.toLowerCase() .toLowerCase()
.contains(filterController.text.toLowerCase())); .contains(filterController.text.toLowerCase()));
toggleCollapse(int i) => isCollapsed.value =
isCollapsed.value.mapWithIndex((e, j) => j == i ? !e : e).toList();
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
actions: [IconButton(icon: Icon(Icons.style))], actions: [
IconButton(
icon: Icon(Icons.style),
onPressed: () {}, // TODO: change styles?
),
],
// TODO: should be smaller // TODO: should be smaller
title: TextField( title: TextField(
controller: filterController, controller: filterController,
@ -96,12 +115,13 @@ class CommunitiesTab extends HookWidget {
), ),
), ),
), ),
body: Column( body: ListView(
children: [ children: [
for (var i in Iterable.generate(instances.length)) for (var i in Iterable.generate(amountOfDisplayInstances))
Column( Column(
children: [ children: [
ListTile( ListTile(
onLongPress: () => toggleCollapse(i),
leading: instances[i].icon != null leading: instances[i].icon != null
? CachedNetworkImage( ? CachedNetworkImage(
height: 50, height: 50,
@ -121,7 +141,14 @@ class CommunitiesTab extends HookWidget {
instances[i].name, instances[i].name,
style: theme.textTheme.headline6, style: theme.textTheme.headline6,
), ),
trailing: IconButton(
icon: Icon(isCollapsed.value[i]
? Icons.keyboard_arrow_up
: Icons.keyboard_arrow_down),
onPressed: () => toggleCollapse(i),
), ),
),
if (!isCollapsed.value[i])
for (var comm in filterCommunities(communities[i])) for (var comm in filterCommunities(communities[i]))
Padding( Padding(
padding: const EdgeInsets.only(left: 17), padding: const EdgeInsets.only(left: 17),
@ -142,10 +169,12 @@ class CommunitiesTab extends HookWidget {
decoration: BoxDecoration( decoration: BoxDecoration(
shape: BoxShape.circle, shape: BoxShape.circle,
image: DecorationImage( image: DecorationImage(
fit: BoxFit.cover, image: imageProvider), fit: BoxFit.cover,
image: imageProvider),
), ),
), ),
errorWidget: (_, __, ___) => SizedBox(width: 30), errorWidget: (_, __, ___) =>
SizedBox(width: 30),
) )
else else
SizedBox(width: 30), SizedBox(width: 30),
@ -181,7 +210,7 @@ class _CommunitySubscribeToggle extends HookWidget {
var theme = Theme.of(context); var theme = Theme.of(context);
var subed = useState(true); var subed = useState(true);
var loading = useState(false); var loading = useState(false);
// TODO: load animation after 500ms
return InkWell( return InkWell(
onTap: () async { onTap: () async {
loading.value = true; loading.value = true;

6
lib/util/iterators.dart Normal file
View File

@ -0,0 +1,6 @@
extension ExtraIterators<E> on Iterable<E> {
Iterable<T> mapWithIndex<T>(T f(E e, int i)) {
var i = 0;
return map((e) => f(e, i++));
}
}