From 7dc198f0a75ec8d7cfdec116bf970c48486c080e Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 25 Jul 2024 14:42:50 +0800 Subject: [PATCH] :recycle: Post list controller layer --- lib/controllers/post_list_controller.dart | 109 ++++++++++++++++++++ lib/screens/home.dart | 117 ++++++++-------------- lib/screens/posts/post_editor.dart | 2 +- lib/translations/en_us.dart | 2 +- lib/widgets/posts/post_item.dart | 2 +- 5 files changed, 155 insertions(+), 77 deletions(-) create mode 100644 lib/controllers/post_list_controller.dart diff --git a/lib/controllers/post_list_controller.dart b/lib/controllers/post_list_controller.dart new file mode 100644 index 0000000..66bed99 --- /dev/null +++ b/lib/controllers/post_list_controller.dart @@ -0,0 +1,109 @@ +import 'package:get/get.dart'; +import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart'; +import 'package:solian/models/pagination.dart'; +import 'package:solian/models/post.dart'; + +import '../providers/content/posts.dart'; + +class PostListController { + /// The polling source modifier. + /// - `0`: default recommendations + /// - `1`: shuffle mode + RxInt mode = 0.obs; + + /// The paging controller for infinite loading. + /// Only available when mode is `0`. + PagingController pagingController = + PagingController(firstPageKey: 0); + + PostListController() { + _initPagingController(); + } + + /// Initialize a compatibility layer to paging controller + void _initPagingController() { + pagingController.addPageRequestListener(_onPagingControllerRequest); + } + + Future _onPagingControllerRequest(int pageKey) async { + try { + final result = await loadMore(); + + if (result != null && hasMore.value) { + pagingController.appendPage(result, nextPageKey.value); + } else if (result != null) { + pagingController.appendLastPage(result); + } + } catch (e) { + pagingController.error = e; + } + } + + void _resetPagingController() { + pagingController.removePageRequestListener(_onPagingControllerRequest); + pagingController.nextPageKey = nextPageKey.value; + pagingController.itemList?.clear(); + } + + RxBool isBusy = false.obs; + + RxList postList = RxList.empty(growable: true); + RxInt nextPageKey = 0.obs; + RxBool hasMore = true.obs; + + Future reloadAllOver() async { + nextPageKey.value = 0; + hasMore.value = true; + _resetPagingController(); + final result = await loadMore(); + if (result != null && hasMore.value) { + pagingController.appendPage(result, nextPageKey.value); + } else if (result != null) { + pagingController.appendLastPage(result); + } + _initPagingController(); + } + + Future?> loadMore() async { + final result = await _loadPosts(nextPageKey.value); + + if (result != null && result.length >= 10) { + nextPageKey.value = nextPageKey.value + result.length; + hasMore.value = true; + } else if (result != null) { + nextPageKey.value = nextPageKey.value + result.length; + hasMore.value = false; + } + + return result; + } + + Future?> _loadPosts(int pageKey) async { + isBusy.value = true; + + final PostProvider provider = Get.find(); + + Response resp; + try { + resp = await provider.listRecommendations( + pageKey, + channel: mode.value == 0 ? null : 'shuffle', + ); + } catch (e) { + rethrow; + } finally { + isBusy.value = false; + } + + final PaginationResult result = PaginationResult.fromJson(resp.body); + final out = result.data?.map((e) => Post.fromJson(e)).toList(); + + if (out != null) postList.addAll(out.cast()); + + return out; + } + + void dispose() { + pagingController.dispose(); + } +} diff --git a/lib/screens/home.dart b/lib/screens/home.dart index e4082b7..88d192e 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -1,10 +1,7 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; -import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart'; -import 'package:solian/models/pagination.dart'; -import 'package:solian/models/post.dart'; +import 'package:solian/controllers/post_list_controller.dart'; import 'package:solian/providers/auth.dart'; -import 'package:solian/providers/content/posts.dart'; import 'package:solian/router.dart'; import 'package:solian/screens/account/notification.dart'; import 'package:solian/theme.dart'; @@ -22,48 +19,21 @@ class HomeScreen extends StatefulWidget { class _HomeScreenState extends State with SingleTickerProviderStateMixin { - final PagingController _pagingController = - PagingController(firstPageKey: 0); + final PostListController _postController = PostListController(); late final TabController _tabController; - int mode = 0; - - getPosts(int pageKey) async { - final PostProvider provider = Get.find(); - - Response resp; - try { - resp = await provider.listRecommendations( - pageKey, - channel: mode == 0 ? null : 'shuffle', - ); - } catch (e) { - _pagingController.error = e; - return; - } - - final PaginationResult result = PaginationResult.fromJson(resp.body); - final parsed = result.data?.map((e) => Post.fromJson(e)).toList(); - if (parsed != null && parsed.length >= 10) { - _pagingController.appendPage(parsed, pageKey + parsed.length); - } else if (parsed != null) { - _pagingController.appendLastPage(parsed); - } - } - @override void initState() { super.initState(); - _pagingController.addPageRequestListener(getPosts); _tabController = TabController(length: 2, vsync: this); _tabController.addListener(() { switch (_tabController.index) { case 0: case 1: - if (mode == _tabController.index) break; - mode = _tabController.index; - _pagingController.refresh(); + if (_postController.mode.value == _tabController.index) break; + _postController.mode.value = _tabController.index; + _postController.reloadAllOver(); } }); } @@ -84,46 +54,45 @@ class _HomeScreenState extends State ); }, ), - body: RefreshIndicator( - onRefresh: () => Future.sync(() => _pagingController.refresh()), - child: NestedScrollView( - headerSliverBuilder: - (BuildContext context, bool innerBoxIsScrolled) { - return [ - SliverAppBar( - title: AppBarTitle('home'.tr), - centerTitle: false, - floating: true, - toolbarHeight: SolianTheme.toolbarHeight(context), - leading: AppBarLeadingButton.adaptive(context), - actions: [ - const BackgroundStateWidget(), - const NotificationButton(), - SizedBox( - width: SolianTheme.isLargeScreen(context) ? 8 : 16, - ), - ], - bottom: TabBar( - controller: _tabController, - tabs: [ - Tab(text: 'postListNews'.tr), - Tab(text: 'postListShuffle'.tr), - ], + body: NestedScrollView( + headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { + return [ + SliverAppBar( + title: AppBarTitle('home'.tr), + centerTitle: false, + floating: true, + toolbarHeight: SolianTheme.toolbarHeight(context), + leading: AppBarLeadingButton.adaptive(context), + actions: [ + const BackgroundStateWidget(), + const NotificationButton(), + SizedBox( + width: SolianTheme.isLargeScreen(context) ? 8 : 16, ), - ) - ]; - }, - body: TabBarView( - controller: _tabController, - children: [ - CustomScrollView(slivers: [ - FeedListWidget(controller: _pagingController), + ], + bottom: TabBar( + controller: _tabController, + tabs: [ + Tab(text: 'postListNews'.tr), + Tab(text: 'postListShuffle'.tr), + ], + ), + ) + ]; + }, + body: TabBarView( + controller: _tabController, + children: [ + RefreshIndicator( + onRefresh: () => _postController.reloadAllOver(), + child: CustomScrollView(slivers: [ + FeedListWidget(controller: _postController.pagingController), ]), - CustomScrollView(slivers: [ - FeedListWidget(controller: _pagingController), - ]), - ], - ), + ), + CustomScrollView(slivers: [ + FeedListWidget(controller: _postController.pagingController), + ]), + ], ), ), ), @@ -132,7 +101,7 @@ class _HomeScreenState extends State @override void dispose() { - _pagingController.dispose(); + _postController.dispose(); super.dispose(); } } diff --git a/lib/screens/posts/post_editor.dart b/lib/screens/posts/post_editor.dart index 2905368..42536df 100644 --- a/lib/screens/posts/post_editor.dart +++ b/lib/screens/posts/post_editor.dart @@ -105,7 +105,7 @@ class _PostPublishScreenState extends State { void syncWidget() { if (widget.edit != null) { _contentController.text = widget.edit!.body['content']; - _attachments = widget.edit!.body['attachments'] ?? List.empty(); + _attachments = widget.edit!.body['attachments']?.cast() ?? List.empty(); _isDraft = widget.edit!.isDraft ?? false; } } diff --git a/lib/translations/en_us.dart b/lib/translations/en_us.dart index 79094a9..40a58e1 100644 --- a/lib/translations/en_us.dart +++ b/lib/translations/en_us.dart @@ -95,7 +95,7 @@ const messagesEnglish = { 'postEdited': 'Edited at @date', 'postNewCreated': 'Created at @date', 'postAttachmentTip': '@count attachment(s)', - 'postInRealm': 'In realm @realm', + 'postInRealm': 'In @realm', 'postDetail': 'Post', 'postReplies': 'Replies', 'postPublish': 'Post a post', diff --git a/lib/widgets/posts/post_item.dart b/lib/widgets/posts/post_item.dart index 85e4563..c1d64b7 100644 --- a/lib/widgets/posts/post_item.dart +++ b/lib/widgets/posts/post_item.dart @@ -86,7 +86,7 @@ class _PostItemState extends State { } if (widget.item.realm != null) { labels.add('postInRealm'.trParams({ - 'realm': '#${widget.item.realm!.id}', + 'realm': widget.item.realm!.alias, })); }