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

99 lines
2.8 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2021-04-05 20:14:39 +02:00
import 'package:lemmy_api_client/v3.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-04-06 17:52:10 +02:00
final Object Function(T item) uniqueProp;
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,
2021-04-06 17:52:10 +02:00
this.uniqueProp,
}) : 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,
2021-04-06 17:52:10 +02:00
uniqueProp: uniqueProp,
);
}
}
2021-04-06 17:52:10 +02:00
class InfinitePostList extends SortableInfiniteList<PostView> {
InfinitePostList({
@required FetcherWithSorting<PostView> fetcher,
InfiniteScrollController controller,
}) : super(
itemBuilder: (post) => Column(
children: [
PostWidget(post),
const SizedBox(height: 20),
],
),
fetcher: fetcher,
controller: controller,
noItems: const Text('there are no posts'),
uniqueProp: (item) => item.post.id,
);
}
2020-09-29 10:53:40 +02:00
2021-04-06 17:52:10 +02:00
class InfiniteCommentList extends SortableInfiniteList<CommentView> {
InfiniteCommentList({
@required FetcherWithSorting<CommentView> fetcher,
InfiniteScrollController controller,
}) : super(
itemBuilder: (comment) => CommentWidget(
CommentTree(comment),
detached: true,
),
fetcher: fetcher,
controller: controller,
noItems: const Text('there are no comments'),
uniqueProp: (item) => item.comment.id,
);
2020-09-29 10:53:40 +02:00
}