implement infinite list for communities

This commit is contained in:
krawieck 2020-09-29 00:57:25 +02:00
parent a3b152706a
commit 6eb748194f
2 changed files with 73 additions and 23 deletions

View File

@ -1,23 +1,30 @@
import 'package:cached_network_image/cached_network_image.dart'; import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/lemmy_api_client.dart'; import 'package:lemmy_api_client/lemmy_api_client.dart';
import '../hooks/infinite_scroll.dart';
import '../util/extensions/api.dart'; import '../util/extensions/api.dart';
import '../util/goto.dart'; import '../util/goto.dart';
import '../widgets/infinite_scroll.dart';
import '../widgets/markdown_text.dart'; import '../widgets/markdown_text.dart';
import '../widgets/post_list_options.dart';
class CommunitiesListPage extends StatelessWidget { class CommunitiesListPage extends StatelessWidget {
final String title; final String title;
final List<CommunityView> communities; final Future<List<CommunityView>> Function(
int page,
int batchSize,
SortType sortType,
) fetcher;
const CommunitiesListPage({Key key, @required this.communities, this.title}) const CommunitiesListPage({Key key, @required this.fetcher, this.title})
: assert(communities != null), : assert(fetcher != null),
super(key: key); super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final theme = Theme.of(context); final theme = Theme.of(context);
// TODO: abillity to load more, right now its 10 - default page size
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text(title ?? '', style: theme.textTheme.headline6), title: Text(title ?? '', style: theme.textTheme.headline6),
@ -25,25 +32,54 @@ class CommunitiesListPage extends StatelessWidget {
backgroundColor: theme.cardColor, backgroundColor: theme.cardColor,
iconTheme: theme.iconTheme, iconTheme: theme.iconTheme,
), ),
body: ListView.builder( body: InfiniteCommunitiesList(fetcher: fetcher),
itemBuilder: (context, i) => ListTile( );
title: Text(communities[i].name), }
subtitle: communities[i].description != null }
/// Infinite list of posts
class InfiniteCommunitiesList extends HookWidget {
final Future<List<CommunityView>> Function(
int page, int batchSize, SortType sortType) fetcher;
InfiniteCommunitiesList({@required this.fetcher}) : assert(fetcher != null);
@override
Widget build(BuildContext context) {
final isc = useInfiniteScrollController();
final sort = useState(SortType.active);
void changeSorting(SortType newSort) {
sort.value = newSort;
isc.clear();
}
return InfiniteScroll<CommunityView>(
prepend: PostListOptions(
onChange: changeSorting,
styleButton: false,
),
builder: (community) => Column(
children: [
Divider(),
ListTile(
title: Text(community.name),
subtitle: community.description != null
? Opacity( ? Opacity(
opacity: 0.5, opacity: 0.5,
child: MarkdownText( child: MarkdownText(
communities[i].description, community.description,
instanceUrl: communities[i].instanceUrl, instanceUrl: community.instanceUrl,
), ),
) )
: null, : null,
onTap: () => goToCommunity.byId( onTap: () => goToCommunity.byId(
context, communities[i].instanceUrl, communities[i].id), context, community.instanceUrl, community.id),
leading: communities[i].icon != null leading: community.icon != null
? CachedNetworkImage( ? CachedNetworkImage(
height: 50, height: 50,
width: 50, width: 50,
imageUrl: communities[i].icon, imageUrl: community.icon,
imageBuilder: (context, imageProvider) => Container( imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration( decoration: BoxDecoration(
shape: BoxShape.circle, shape: BoxShape.circle,
@ -56,7 +92,12 @@ class CommunitiesListPage extends StatelessWidget {
: SizedBox(width: 50), : SizedBox(width: 50),
// TODO: add trailing button for un/subscribing to communities // TODO: add trailing button for un/subscribing to communities
), ),
itemCount: communities.length, ],
)); ),
padding: EdgeInsets.zero,
fetchMore: (page, batchSize) => fetcher(page, batchSize, sort.value),
controller: isc,
batchSize: 20,
);
} }
} }

View File

@ -7,6 +7,7 @@ import 'package:intl/intl.dart';
import 'package:lemmy_api_client/lemmy_api_client.dart'; import 'package:lemmy_api_client/lemmy_api_client.dart';
import 'package:url_launcher/url_launcher.dart' as ul; import 'package:url_launcher/url_launcher.dart' as ul;
import '../hooks/stores.dart';
import '../util/extensions/api.dart'; import '../util/extensions/api.dart';
import '../util/goto.dart'; import '../util/goto.dart';
import '../util/text_color.dart'; import '../util/text_color.dart';
@ -288,13 +289,21 @@ class _AboutTab extends HookWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final theme = Theme.of(context); final theme = Theme.of(context);
final commSnap = useFuture(communitiesFuture); final commSnap = useFuture(communitiesFuture);
final accStore = useAccountsStore();
void goToCommunities() { void goToCommunities() {
goTo( goTo(
context, context,
(_) => CommunitiesListPage( (_) => CommunitiesListPage(
communities: commSnap.data, fetcher: (page, batchSize, sortType) =>
title: 'Communities of ${site.site.name}'), LemmyApi(instanceUrl).v1.listCommunities(
sort: sortType,
limit: batchSize,
page: page,
auth: accStore.defaultTokenFor(instanceUrl)?.raw,
),
title: 'Communities of ${site.site.name}',
),
); );
} }