From 80bade0e031c999c80a141f1965c08b9f56bcebc Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 19 Aug 2024 00:33:03 +0800 Subject: [PATCH] :sparkles: View posts posted by friends --- lib/controllers/post_list_controller.dart | 26 +++++++++++++----- lib/providers/content/posts.dart | 9 ++++++- lib/screens/home.dart | 33 +++++++++++++++++------ lib/translations/en_us.dart | 1 + lib/translations/zh_cn.dart | 1 + lib/widgets/posts/post_action.dart | 6 ++--- 6 files changed, 58 insertions(+), 18 deletions(-) diff --git a/lib/controllers/post_list_controller.dart b/lib/controllers/post_list_controller.dart index 64c6c13..2a952e6 100644 --- a/lib/controllers/post_list_controller.dart +++ b/lib/controllers/post_list_controller.dart @@ -9,11 +9,12 @@ class PostListController extends GetxController { /// The polling source modifier. /// - `0`: default recommendations - /// - `1`: shuffle mode + /// - `1`: friend mode + /// - `2`: shuffle mode RxInt mode = 0.obs; /// The paging controller for infinite loading. - /// Only available when mode is `0`. + /// Only available when mode is `0` or `1`. PagingController pagingController = PagingController(firstPageKey: 0); @@ -111,10 +112,23 @@ class PostListController extends GetxController { author: author, ); } else { - resp = await provider.listRecommendations( - pageKey, - channel: mode.value == 0 ? null : 'shuffle', - ); + switch (mode.value) { + case 2: + resp = await provider.listRecommendations( + pageKey, + channel: 'shuffle', + ); + break; + case 1: + resp = await provider.listRecommendations( + pageKey, + channel: 'friends', + ); + break; + default: + resp = await provider.listRecommendations(pageKey); + break; + } } } catch (e) { rethrow; diff --git a/lib/providers/content/posts.dart b/lib/providers/content/posts.dart index ab5ae11..5d6744e 100644 --- a/lib/providers/content/posts.dart +++ b/lib/providers/content/posts.dart @@ -10,12 +10,19 @@ class PostProvider extends GetConnect { Future listRecommendations(int page, {String? realm, String? channel}) async { + GetConnect client; + final AuthProvider auth = Get.find(); final queries = [ 'take=${10}', 'offset=$page', if (realm != null) 'realm=$realm', ]; - final resp = await get( + if (auth.isAuthorized.value) { + client = auth.configureClient('co'); + } else { + client = ServiceFinder.configureClient('co'); + } + final resp = await client.get( channel == null ? '/recommendations?${queries.join('&')}' : '/recommendations/$channel?${queries.join('&')}', diff --git a/lib/screens/home.dart b/lib/screens/home.dart index cb1dbc2..9af9f38 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -5,6 +5,7 @@ import 'package:solian/providers/auth.dart'; import 'package:solian/router.dart'; import 'package:solian/screens/account/notification.dart'; import 'package:solian/theme.dart'; +import 'package:solian/widgets/account/signin_required_overlay.dart'; import 'package:solian/widgets/app_bar_title.dart'; import 'package:solian/widgets/current_state_action.dart'; import 'package:solian/widgets/app_bar_leading.dart'; @@ -27,20 +28,18 @@ class _HomeScreenState extends State void initState() { super.initState(); _postController = PostListController(); - _tabController = TabController(length: 2, vsync: this); + _tabController = TabController(length: 3, vsync: this); _tabController.addListener(() { - switch (_tabController.index) { - case 0: - case 1: - if (_postController.mode.value == _tabController.index) return; - _postController.mode.value = _tabController.index; - _postController.reloadAllOver(); - } + if (_postController.mode.value == _tabController.index) return; + _postController.mode.value = _tabController.index; + _postController.reloadAllOver(); }); } @override Widget build(BuildContext context) { + final AuthProvider auth = Get.find(); + return Material( color: Theme.of(context).colorScheme.surface, child: Scaffold( @@ -82,6 +81,7 @@ class _HomeScreenState extends State controller: _tabController, tabs: [ Tab(text: 'postListNews'.tr), + Tab(text: 'postListFriends'.tr), Tab(text: 'postListShuffle'.tr), ], ), @@ -108,6 +108,23 @@ class _HomeScreenState extends State ), ]), ), + Obx(() { + if (auth.isAuthorized.value) { + return RefreshIndicator( + onRefresh: () => _postController.reloadAllOver(), + child: CustomScrollView(slivers: [ + PostWarpedListWidget( + controller: _postController.pagingController, + onUpdate: () => _postController.reloadAllOver(), + ), + ]), + ); + } else { + return SigninRequiredOverlay( + onSignedIn: () => _postController.reloadAllOver(), + ); + } + }), PostShuffleSwiper(controller: _postController), ], ); diff --git a/lib/translations/en_us.dart b/lib/translations/en_us.dart index a9b53c5..432e927 100644 --- a/lib/translations/en_us.dart +++ b/lib/translations/en_us.dart @@ -125,6 +125,7 @@ const i18nEnglish = { 'postThumbnailAttachment': 'Attachment serial number', 'postPinned': 'Pinned', 'postListNews': 'News', + 'postListFriends': 'Friends', 'postListShuffle': 'Random', 'postEditorModeStory': 'Post a post', 'postEditorModeArticle': 'Post an article', diff --git a/lib/translations/zh_cn.dart b/lib/translations/zh_cn.dart index 1e6a9dc..946997f 100644 --- a/lib/translations/zh_cn.dart +++ b/lib/translations/zh_cn.dart @@ -125,6 +125,7 @@ const i18nSimplifiedChinese = { 'articleDetail': '文章详情', 'draftBoxOpen': '打开草稿箱', 'postListNews': '新鲜事', + 'postListFriends': '好友圈', 'postListShuffle': '打乱看', 'postNew': '创建新帖子', 'postNewInRealmHint': '在领域 @realm 里发表新帖子', diff --git a/lib/widgets/posts/post_action.dart b/lib/widgets/posts/post_action.dart index 7a5c073..cbd1a97 100644 --- a/lib/widgets/posts/post_action.dart +++ b/lib/widgets/posts/post_action.dart @@ -45,7 +45,7 @@ class _PostActionState extends State { String id; final box = context.findRenderObject() as RenderBox?; if (widget.item.alias?.isNotEmpty ?? false) { - id = '${widget.item.areaAlias}:${widget.item.alias}'; + id = '${widget.item.areaAlias}/${widget.item.alias}'; } else { id = '${widget.item.id}'; } @@ -55,10 +55,10 @@ class _PostActionState extends State { sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size, ); } else { - final extraContent = [ + final extraContent = [ widget.item.body['title'], widget.item.body['description'], - ]; + ].where((x) => x != null && x.isNotEmpty).toList(); final isExtraNotEmpty = extraContent.any((x) => x != null); result = await Share.share( 'postShareContent'.trParams({