From fa3ba0e18873df055c33fa65eea8b8f6f4904d34 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 25 Jul 2024 02:00:29 +0800 Subject: [PATCH] :sparkles: Shuffle mode --- lib/providers/content/posts.dart | 8 +++- lib/screens/home.dart | 76 ++++++++++++++++++++++++-------- lib/translations/en_us.dart | 2 + lib/translations/zh_cn.dart | 2 + 4 files changed, 67 insertions(+), 21 deletions(-) diff --git a/lib/providers/content/posts.dart b/lib/providers/content/posts.dart index cd8f99a..1a7b173 100644 --- a/lib/providers/content/posts.dart +++ b/lib/providers/content/posts.dart @@ -9,7 +9,7 @@ class PostProvider extends GetConnect { } Future listRecommendations(int page, - {int? realm, String? tag, category}) async { + {int? realm, String? tag, category, String? channel}) async { final queries = [ 'take=${10}', 'offset=$page', @@ -17,7 +17,11 @@ class PostProvider extends GetConnect { if (category != null) 'category=$category', if (realm != null) 'realmId=$realm', ]; - final resp = await get('/recommendations?${queries.join('&')}'); + final resp = await get( + channel == null + ? '/recommendations?${queries.join('&')}' + : '/recommendations/$channel?${queries.join('&')}', + ); if (resp.statusCode != 200) { throw Exception(resp.body); } diff --git a/lib/screens/home.dart b/lib/screens/home.dart index 68c045c..e4082b7 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -20,16 +20,24 @@ class HomeScreen extends StatefulWidget { State createState() => _HomeScreenState(); } -class _HomeScreenState extends State { +class _HomeScreenState extends State + with SingleTickerProviderStateMixin { final PagingController _pagingController = PagingController(firstPageKey: 0); + late final TabController _tabController; + + int mode = 0; + getPosts(int pageKey) async { final PostProvider provider = Get.find(); Response resp; try { - resp = await provider.listRecommendations(pageKey); + resp = await provider.listRecommendations( + pageKey, + channel: mode == 0 ? null : 'shuffle', + ); } catch (e) { _pagingController.error = e; return; @@ -48,6 +56,16 @@ class _HomeScreenState extends State { 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(); + } + }); } @override @@ -68,24 +86,44 @@ class _HomeScreenState extends State { ), body: RefreshIndicator( onRefresh: () => Future.sync(() => _pagingController.refresh()), - child: CustomScrollView( - slivers: [ - 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, + 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), + ], ), - ], - ), - FeedListWidget(controller: _pagingController), - ], + ) + ]; + }, + body: TabBarView( + controller: _tabController, + children: [ + CustomScrollView(slivers: [ + FeedListWidget(controller: _pagingController), + ]), + CustomScrollView(slivers: [ + FeedListWidget(controller: _pagingController), + ]), + ], + ), ), ), ), diff --git a/lib/translations/en_us.dart b/lib/translations/en_us.dart index 711bdf4..79094a9 100644 --- a/lib/translations/en_us.dart +++ b/lib/translations/en_us.dart @@ -83,6 +83,8 @@ const messagesEnglish = { 'notifyAllRead': 'Mark all as read', 'notifyEmpty': 'All notifications read', 'notifyEmptyCaption': 'It seems like nothing happened recently', + 'postListNews': 'News', + 'postListShuffle': 'Random', 'postEditor': 'Create new post', 'articleEditor': 'Create new article', 'articleDetail': 'Article details', diff --git a/lib/translations/zh_cn.dart b/lib/translations/zh_cn.dart index 013f3e4..a0154c0 100644 --- a/lib/translations/zh_cn.dart +++ b/lib/translations/zh_cn.dart @@ -81,6 +81,8 @@ const simplifiedChineseMessages = { 'articleEditor': '撰写文章', 'articleDetail': '文章详情', 'draftBoxOpen': '打开草稿箱', + 'postListNews': '新鲜事', + 'postListShuffle': '打乱看', 'postNew': '创建新帖子', 'postNewInRealmHint': '在领域 @realm 里发表新帖子', 'postAction': '发表',