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

100 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';
2021-04-23 19:20:32 +02:00
import '../hooks/stores.dart';
import '../stores/config_store.dart';
import 'comment/comment.dart';
import 'infinite_scroll.dart';
2021-09-11 01:04:33 +02:00
import 'post/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-04-23 19:20:32 +02:00
/// if no defaultSort is provided, the defaultSortType
/// from the configStore will be used
final SortType? defaultSort;
final Object Function(T item)? uniqueProp;
2021-01-03 18:21:56 +01:00
const SortableInfiniteList({
required this.fetcher,
required this.itemBuilder,
2021-02-09 20:39:31 +01:00
this.controller,
this.onStyleChange,
this.noItems = const SizedBox.shrink(),
2021-04-23 19:20:32 +02:00
this.defaultSort,
2021-04-06 17:52:10 +02:00
this.uniqueProp,
});
@override
Widget build(BuildContext context) {
2021-04-23 19:20:32 +02:00
final defaultSortType =
useStore((ConfigStore store) => store.defaultSortType);
2021-02-09 20:39:31 +01:00
final defaultController = useInfiniteScrollController();
final isc = controller ?? defaultController;
2021-04-23 19:20:32 +02:00
final sort = useState(defaultSort ?? defaultSortType);
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({
2022-05-11 22:23:18 +02:00
required super.fetcher,
super.controller,
2021-04-06 17:52:10 +02:00
}) : super(
itemBuilder: (post) => Column(
children: [
2021-09-11 01:04:33 +02:00
PostTile.fromPostView(post),
2021-04-06 17:52:10 +02:00
const SizedBox(height: 20),
],
),
noItems: const Text('there are no posts'),
2021-04-22 21:08:30 +02:00
uniqueProp: (item) => item.post.apId,
2021-04-06 17:52:10 +02:00
);
}
2020-09-29 10:53:40 +02:00
2021-04-06 17:52:10 +02:00
class InfiniteCommentList extends SortableInfiniteList<CommentView> {
InfiniteCommentList({
2022-05-11 22:23:18 +02:00
required super.fetcher,
super.controller,
2021-04-06 17:52:10 +02:00
}) : super(
itemBuilder: (comment) => CommentWidget(
CommentTree(comment),
detached: true,
),
noItems: const Text('there are no comments'),
2021-04-22 21:08:30 +02:00
uniqueProp: (item) => item.comment.apId,
2021-04-06 17:52:10 +02:00
);
2020-09-29 10:53:40 +02:00
}