diff --git a/lib/pages/community.dart b/lib/pages/community.dart index 855f208..2d795bb 100644 --- a/lib/pages/community.dart +++ b/lib/pages/community.dart @@ -8,7 +8,6 @@ import 'package:lemmy_api_client/lemmy_api_client.dart'; import 'package:url_launcher/url_launcher.dart' as ul; import '../hooks/delayed_loading.dart'; -import '../hooks/infinite_scroll.dart'; import '../hooks/logged_in_action.dart'; import '../hooks/memo_future.dart'; import '../hooks/stores.dart'; @@ -19,10 +18,8 @@ import '../util/text_color.dart'; import '../widgets/badge.dart'; import '../widgets/bottom_modal.dart'; import '../widgets/fullscreenable_image.dart'; -import '../widgets/infinite_scroll.dart'; +import '../widgets/infinite_post_list.dart'; import '../widgets/markdown_text.dart'; -import '../widgets/post.dart'; -import '../widgets/post_list_options.dart'; class CommunityPage extends HookWidget { final CommunityView _community; @@ -211,7 +208,15 @@ class CommunityPage extends HookWidget { ], body: TabBarView( children: [ - _PostsTab(community), + InfinitePostList( + fetcher: (page, batchSize, sort) => + LemmyApi(community.instanceUrl).v1.getPosts( + type: PostListingType.community, + sort: sort, + communityId: community.id, + page: page, + // limit: 10, + )), ListView( children: [ Center(child: Text('comments go here')), @@ -494,47 +499,6 @@ class _AboutTab extends StatelessWidget { } } -class _PostsTab extends HookWidget { - final CommunityView community; - - _PostsTab(this.community); - - @override - Widget build(BuildContext context) { - final isc = useInfiniteScrollController(); - final sort = useState(SortType.active); - - void changeSorting(SortType newSort) { - sort.value = newSort; - isc.clear(); - } - - return InfiniteScroll( - prepend: PostListOptions( - onChange: changeSorting, - defaultSort: SortType.active, - ), - builder: (post) => Column( - children: [ - Post(post), - SizedBox(height: 20), - ], - ), - padding: EdgeInsets.zero, - fetchMore: (page, batchSize) => - LemmyApi(community.instanceUrl).v1.getPosts( - type: PostListingType.community, - sort: sort.value, - communityId: community.id, - page: page, - // limit: 10, - ), - controller: isc, - // batchSize: 20, - ); - } -} - class _Badge extends StatelessWidget { final String text; final bool noPad; diff --git a/lib/widgets/infinite_post_list.dart b/lib/widgets/infinite_post_list.dart new file mode 100644 index 0000000..94e098d --- /dev/null +++ b/lib/widgets/infinite_post_list.dart @@ -0,0 +1,44 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_hooks/flutter_hooks.dart'; +import 'package:lemmy_api_client/lemmy_api_client.dart'; + +import '../hooks/infinite_scroll.dart'; +import 'infinite_scroll.dart'; +import 'post.dart'; +import 'post_list_options.dart'; + +/// Infinite list of posts +class InfinitePostList extends HookWidget { + final Future> Function( + int page, int batchSize, SortType sortType) fetcher; + + InfinitePostList({@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( + prepend: PostListOptions( + onChange: changeSorting, + defaultSort: SortType.active, + ), + builder: (post) => Column( + children: [ + Post(post), + SizedBox(height: 20), + ], + ), + padding: EdgeInsets.zero, + fetchMore: (page, batchSize) => fetcher(page, batchSize, sort.value), + controller: isc, + // batchSize: 20, + ); + } +}