lemmur-app-android/lib/widgets/sortable_infinite_list.dart

105 lines
2.9 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2021-01-24 20:01:55 +01:00
import 'package:lemmy_api_client/v2.dart';
2020-09-29 10:53:40 +02:00
import '../comment_tree.dart';
import '../hooks/infinite_scroll.dart';
2020-09-29 10:53:40 +02:00
import 'comment.dart';
import 'infinite_scroll.dart';
2020-09-29 10:53:40 +02:00
import 'post.dart';
import 'post_list_options.dart';
2021-02-09 20:39:31 +01:00
typedef FetcherWithSorting<T> = Future<List<T>> Function(
int page, int batchSize, SortType sortType);
/// Infinite list of posts
class SortableInfiniteList<T> extends HookWidget {
2021-02-09 20:39:31 +01:00
final FetcherWithSorting<T> fetcher;
final Widget Function(T) itemBuilder;
final InfiniteScrollController controller;
final Function onStyleChange;
2021-02-09 20:39:31 +01:00
final Widget noItems;
2021-02-24 20:52:18 +01:00
final SortType defaultSort;
2021-01-03 18:21:56 +01:00
const SortableInfiniteList({
@required this.fetcher,
2021-02-09 20:39:31 +01:00
@required this.itemBuilder,
this.controller,
this.onStyleChange,
2021-02-09 20:39:31 +01:00
this.noItems,
2021-02-24 20:52:18 +01:00
this.defaultSort = SortType.active,
}) : assert(fetcher != null),
2021-02-24 20:52:18 +01:00
assert(itemBuilder != null),
assert(defaultSort != null);
@override
Widget build(BuildContext context) {
2021-02-09 20:39:31 +01:00
final defaultController = useInfiniteScrollController();
final isc = controller ?? defaultController;
2021-02-24 20:52:18 +01:00
final sort = useState(defaultSort);
void changeSorting(SortType newSort) {
sort.value = newSort;
isc.clear();
}
return InfiniteScroll<T>(
2021-02-09 20:39:31 +01:00
leading: PostListOptions(
2021-02-09 15:12:13 +01:00
sortValue: sort.value,
onSortChanged: changeSorting,
styleButton: onStyleChange != null,
),
2021-02-09 20:39:31 +01:00
itemBuilder: itemBuilder,
padding: EdgeInsets.zero,
2021-02-09 20:39:31 +01:00
fetcher: (page, batchSize) => fetcher(page, batchSize, sort.value),
controller: isc,
batchSize: 20,
2021-02-09 20:39:31 +01:00
noItems: noItems,
);
}
}
class InfinitePostList extends StatelessWidget {
2021-02-09 20:39:31 +01:00
final FetcherWithSorting<PostView> fetcher;
final InfiniteScrollController controller;
2021-02-09 20:39:31 +01:00
const InfinitePostList({
@required this.fetcher,
this.controller,
}) : assert(fetcher != null);
Widget build(BuildContext context) => SortableInfiniteList<PostView>(
onStyleChange: () {},
2021-02-09 20:39:31 +01:00
itemBuilder: (post) => Column(
children: [
2021-01-24 20:01:55 +01:00
PostWidget(post),
2021-01-03 19:43:39 +01:00
const SizedBox(height: 20),
],
),
fetcher: fetcher,
2021-02-09 20:39:31 +01:00
controller: controller,
noItems: const Text('there are no posts'),
);
}
2020-09-29 10:53:40 +02:00
class InfiniteCommentList extends StatelessWidget {
2021-02-09 20:39:31 +01:00
final FetcherWithSorting<CommentView> fetcher;
final InfiniteScrollController controller;
2020-09-29 10:53:40 +02:00
2021-02-09 20:39:31 +01:00
const InfiniteCommentList({
@required this.fetcher,
this.controller,
}) : assert(fetcher != null);
2020-09-29 10:53:40 +02:00
Widget build(BuildContext context) => SortableInfiniteList<CommentView>(
2021-02-09 20:39:31 +01:00
itemBuilder: (comment) => CommentWidget(
2020-09-29 10:53:40 +02:00
CommentTree(comment),
detached: true,
2020-09-29 10:53:40 +02:00
),
fetcher: fetcher,
2021-02-09 20:39:31 +01:00
controller: controller,
noItems: const Text('there are no comments'),
2020-09-29 10:53:40 +02:00
);
}