From a157596a2eccfe30b33450a5ade64d004c9cda4c Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 4 Aug 2024 18:13:59 +0800 Subject: [PATCH] :wheelchair: Optimize and fixes --- lib/router.dart | 1 - lib/screens/account/profile_page.dart | 1 + lib/screens/feed/draft_box.dart | 8 ++- lib/screens/feed/search.dart | 5 +- lib/screens/home.dart | 55 ++++++++++-------- lib/screens/posts/post_editor.dart | 8 --- .../account/account_profile_popup.dart | 56 +++++++++++++++---- lib/widgets/posts/post_action.dart | 36 ++++++------ lib/widgets/posts/post_list.dart | 8 ++- lib/widgets/posts/post_warped_list.dart | 6 +- 10 files changed, 117 insertions(+), 67 deletions(-) diff --git a/lib/router.dart b/lib/router.dart index d7ba60f..c30d710 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -104,7 +104,6 @@ abstract class AppRouter { reply: arguments?.reply, repost: arguments?.repost, realm: arguments?.realm, - postListController: arguments?.postListController, mode: int.tryParse(state.uri.queryParameters['mode'] ?? '0') ?? 0, ), transitionsBuilder: diff --git a/lib/screens/account/profile_page.dart b/lib/screens/account/profile_page.dart index 2f27d5a..b972d09 100644 --- a/lib/screens/account/profile_page.dart +++ b/lib/screens/account/profile_page.dart @@ -300,6 +300,7 @@ class _AccountProfilePageState extends State { PostWarpedListWidget( isPinned: false, controller: _postController.pagingController, + onUpdate: () => _postController.reloadAllOver(), ), ]), ), diff --git a/lib/screens/feed/draft_box.dart b/lib/screens/feed/draft_box.dart index e487551..8aef123 100644 --- a/lib/screens/feed/draft_box.dart +++ b/lib/screens/feed/draft_box.dart @@ -87,7 +87,13 @@ class _DraftBoxScreenState extends State { noReact: true, ), ).then((value) { - if (value != null) _pagingController.refresh(); + if (value is Future) { + value.then((_) { + _pagingController.refresh(); + }); + } else if (value != null) { + _pagingController.refresh(); + } }); }, ).paddingOnly(left: 12, right: 12, bottom: 4); diff --git a/lib/screens/feed/search.dart b/lib/screens/feed/search.dart index 729f94a..0ff0b2b 100644 --- a/lib/screens/feed/search.dart +++ b/lib/screens/feed/search.dart @@ -77,7 +77,10 @@ class _FeedSearchScreenState extends State { onRefresh: () => Future.sync(() => _pagingController.refresh()), child: CustomScrollView( slivers: [ - PostWarpedListWidget(controller: _pagingController), + PostWarpedListWidget( + controller: _pagingController, + onUpdate: () => _pagingController.refresh(), + ), ], ), ), diff --git a/lib/screens/home.dart b/lib/screens/home.dart index 046b71a..53c8412 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -4,7 +4,6 @@ import 'package:solian/controllers/post_list_controller.dart'; import 'package:solian/providers/auth.dart'; import 'package:solian/router.dart'; import 'package:solian/screens/account/notification.dart'; -import 'package:solian/screens/posts/post_editor.dart'; import 'package:solian/theme.dart'; import 'package:solian/widgets/app_bar_title.dart'; import 'package:solian/widgets/current_state_action.dart'; @@ -47,15 +46,20 @@ class _HomeScreenState extends State child: Scaffold( floatingActionButton: FloatingActionButton( child: const Icon(Icons.add), - onPressed: () { - showModalBottomSheet( + onPressed: () async { + final value = await showModalBottomSheet( useRootNavigator: true, isScrollControlled: true, context: context, - builder: (context) => PostCreatePopup( - controller: _postController, - ), + builder: (context) => const PostCreatePopup(), ); + if (value is Future) { + value.then((_) { + _postController.reloadAllOver(); + }); + } else if (value != null) { + _postController.reloadAllOver(); + } }, ), body: NestedScrollView( @@ -100,6 +104,7 @@ class _HomeScreenState extends State child: CustomScrollView(slivers: [ PostWarpedListWidget( controller: _postController.pagingController, + onUpdate: () => _postController.reloadAllOver(), ), ]), ), @@ -121,12 +126,10 @@ class _HomeScreenState extends State class PostCreatePopup extends StatelessWidget { final bool hideDraftBox; - final PostListController controller; const PostCreatePopup({ super.key, this.hideDraftBox = false, - required this.controller, }); @override @@ -142,13 +145,14 @@ class PostCreatePopup extends StatelessWidget { icon: const Icon(Icons.post_add), label: 'postEditorModeStory'.tr, onTap: () { - Navigator.pop(context); - AppRouter.instance.pushNamed( - 'postEditor', - extra: PostPublishArguments(postListController: controller), - queryParameters: { - 'mode': 0.toString(), - }, + Navigator.pop( + context, + AppRouter.instance.pushNamed( + 'postEditor', + queryParameters: { + 'mode': 0.toString(), + }, + ), ); }, ), @@ -156,13 +160,14 @@ class PostCreatePopup extends StatelessWidget { icon: const Icon(Icons.description), label: 'postEditorModeArticle'.tr, onTap: () { - Navigator.pop(context); - AppRouter.instance.pushNamed( - 'postEditor', - extra: PostPublishArguments(postListController: controller), - queryParameters: { - 'mode': 1.toString(), - }, + Navigator.pop( + context, + AppRouter.instance.pushNamed( + 'postEditor', + queryParameters: { + 'mode': 1.toString(), + }, + ), ); }, ), @@ -170,8 +175,10 @@ class PostCreatePopup extends StatelessWidget { icon: const Icon(Icons.drafts), label: 'draftBoxOpen'.tr, onTap: () { - Navigator.pop(context); - AppRouter.instance.pushNamed('draftBox'); + Navigator.pop( + context, + AppRouter.instance.pushNamed('draftBox'), + ); }, ), ]; diff --git a/lib/screens/posts/post_editor.dart b/lib/screens/posts/post_editor.dart index 184de01..73e0235 100644 --- a/lib/screens/posts/post_editor.dart +++ b/lib/screens/posts/post_editor.dart @@ -5,7 +5,6 @@ import 'package:get/get.dart'; import 'package:intl/intl.dart'; import 'package:markdown_toolbar/markdown_toolbar.dart'; import 'package:solian/controllers/post_editor_controller.dart'; -import 'package:solian/controllers/post_list_controller.dart'; import 'package:solian/exts.dart'; import 'package:solian/models/post.dart'; import 'package:solian/models/realm.dart'; @@ -23,14 +22,12 @@ class PostPublishArguments { final Post? reply; final Post? repost; final Realm? realm; - final PostListController? postListController; PostPublishArguments({ this.edit, this.reply, this.repost, this.realm, - this.postListController, }); } @@ -39,7 +36,6 @@ class PostPublishScreen extends StatefulWidget { final Post? reply; final Post? repost; final Realm? realm; - final PostListController? postListController; final int mode; const PostPublishScreen({ @@ -48,7 +44,6 @@ class PostPublishScreen extends StatefulWidget { this.reply, this.repost, this.realm, - this.postListController, required this.mode, }); @@ -95,9 +90,6 @@ class _PostPublishScreenState extends State { context.showErrorDialog(resp.bodyString); } else { _editorController.localClear(); - if (widget.postListController != null) { - widget.postListController!.reloadAllOver(); - } AppRouter.instance.pop(resp.body); } diff --git a/lib/widgets/account/account_profile_popup.dart b/lib/widgets/account/account_profile_popup.dart index f1cf350..233b3c1 100644 --- a/lib/widgets/account/account_profile_popup.dart +++ b/lib/widgets/account/account_profile_popup.dart @@ -1,6 +1,5 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; -import 'package:solian/exts.dart'; import 'package:solian/models/account.dart'; import 'package:solian/providers/account_status.dart'; import 'package:solian/router.dart'; @@ -18,23 +17,38 @@ class AccountProfilePopup extends StatefulWidget { class _AccountProfilePopupState extends State { bool _isBusy = true; + dynamic _hasError; Account? _userinfo; void _getUserinfo() async { setState(() => _isBusy = true); - final client = ServiceFinder.configureClient('auth'); - final resp = await client.get('/users/${widget.name}'); - if (resp.statusCode == 200) { - _userinfo = Account.fromJson(resp.body); - setState(() => _isBusy = false); - } else { - context.showErrorDialog(resp.bodyString); - Navigator.pop(context); + try { + final client = ServiceFinder.configureClient('auth'); + final resp = await client.get('/users/${widget.name}'); + if (resp.statusCode == 200) { + setState(() { + _userinfo = Account.fromJson(resp.body); + _isBusy = false; + }); + } else { + setState(() { + _hasError = resp.bodyString; + _isBusy = false; + }); + } + } catch (e) { + setState(() { + _hasError = e; + _isBusy = false; + }); } } + Color get _unFocusColor => + Theme.of(context).colorScheme.onSurface.withOpacity(0.75); + @override void initState() { super.initState(); @@ -43,13 +57,35 @@ class _AccountProfilePopupState extends State { @override Widget build(BuildContext context) { - if (_isBusy || _userinfo == null) { + if (_isBusy) { return SizedBox( height: MediaQuery.of(context).size.height * 0.75, child: const Center(child: CircularProgressIndicator()), ); } + if (_hasError != null) { + return SizedBox( + height: MediaQuery.of(context).size.height * 0.75, + width: double.infinity, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Icon(Icons.cancel, size: 24), + const SizedBox(height: 12), + Text( + _hasError.toString(), + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 13, + color: _unFocusColor, + ), + ), + ], + ), + ); + } + return SizedBox( height: MediaQuery.of(context).size.height * 0.75, child: Column( diff --git a/lib/widgets/posts/post_action.dart b/lib/widgets/posts/post_action.dart index e465158..6ee3c3f 100644 --- a/lib/widgets/posts/post_action.dart +++ b/lib/widgets/posts/post_action.dart @@ -97,13 +97,13 @@ class _PostActionState extends State { leading: const FaIcon(FontAwesomeIcons.reply, size: 20), title: Text('reply'.tr), onTap: () async { - final value = await AppRouter.instance.pushNamed( - 'postEditor', - extra: PostPublishArguments(reply: widget.item), + Navigator.pop( + context, + AppRouter.instance.pushNamed( + 'postEditor', + extra: PostPublishArguments(reply: widget.item), + ), ); - if (value != null) { - Navigator.pop(context, true); - } }, ), if (!widget.noReact) @@ -112,13 +112,13 @@ class _PostActionState extends State { leading: const FaIcon(FontAwesomeIcons.retweet, size: 20), title: Text('repost'.tr), onTap: () async { - final value = await AppRouter.instance.pushNamed( - 'postEditor', - extra: PostPublishArguments(repost: widget.item), + Navigator.pop( + context, + AppRouter.instance.pushNamed( + 'postEditor', + extra: PostPublishArguments(repost: widget.item), + ), ); - if (value != null) { - Navigator.pop(context, true); - } }, ), if (_canModifyContent && !widget.noReact) @@ -146,13 +146,13 @@ class _PostActionState extends State { leading: const Icon(Icons.edit), title: Text('edit'.tr), onTap: () async { - final value = await AppRouter.instance.pushNamed( - 'postEditor', - extra: PostPublishArguments(edit: widget.item), + Navigator.pop( + context, + AppRouter.instance.pushNamed( + 'postEditor', + extra: PostPublishArguments(edit: widget.item), + ), ); - if (value != null) { - Navigator.pop(context, true); - } }, ), if (_canModifyContent) diff --git a/lib/widgets/posts/post_list.dart b/lib/widgets/posts/post_list.dart index 5d6616e..05a8eaa 100644 --- a/lib/widgets/posts/post_list.dart +++ b/lib/widgets/posts/post_list.dart @@ -85,7 +85,13 @@ class PostListEntryWidget extends StatelessWidget { context: context, builder: (context) => PostAction(item: item), ).then((value) { - if (value != null) onUpdate(); + if (value is Future) { + value.then((_) { + onUpdate(); + }); + } else if (value != null) { + onUpdate(); + } }); }, ); diff --git a/lib/widgets/posts/post_warped_list.dart b/lib/widgets/posts/post_warped_list.dart index a4609cd..5293dc9 100644 --- a/lib/widgets/posts/post_warped_list.dart +++ b/lib/widgets/posts/post_warped_list.dart @@ -9,6 +9,7 @@ class PostWarpedListWidget extends StatelessWidget { final bool isNestedClickable; final bool isPinned; final PagingController controller; + final Function? onUpdate; const PostWarpedListWidget({ super.key, @@ -17,6 +18,7 @@ class PostWarpedListWidget extends StatelessWidget { this.isClickable = true, this.isNestedClickable = true, this.isPinned = true, + this.onUpdate, }); @override @@ -35,9 +37,7 @@ class PostWarpedListWidget extends StatelessWidget { isNestedClickable: isNestedClickable, isClickable: isClickable, item: item, - onUpdate: () { - controller.refresh(); - }, + onUpdate: onUpdate ?? () {}, ); }, ),