From 32c491ae843bf586abfea1df2b2a24ccc165ca82 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 16 Apr 2020 14:58:02 +0200 Subject: [PATCH] Remove Navigator references from conversations pages --- lib/ui/routes/conversation_route.dart | 16 +++--- lib/ui/routes/home_route.dart | 51 +++++++++++++++++-- lib/ui/routes/update_conversation_route.dart | 2 + lib/ui/screens/conversations_list_screen.dart | 29 ++++------- .../create_conversation_screen.dart} | 5 +- .../screens/update_conversation_screen.dart | 31 +++++------ .../widgets/comunic_back_button_widget.dart | 11 ++++ lib/ui/widgets/navbar_widget.dart | 2 + lib/utils/conversations_utils.dart | 10 +--- lib/utils/navigation_utils.dart | 8 ++- 10 files changed, 108 insertions(+), 57 deletions(-) rename lib/ui/{routes/create_conversation_route.dart => screens/create_conversation_screen.dart} (72%) create mode 100644 lib/ui/widgets/comunic_back_button_widget.dart diff --git a/lib/ui/routes/conversation_route.dart b/lib/ui/routes/conversation_route.dart index 9914330..a95e7f9 100644 --- a/lib/ui/routes/conversation_route.dart +++ b/lib/ui/routes/conversation_route.dart @@ -1,7 +1,9 @@ import 'package:comunic/helpers/conversations_helper.dart'; import 'package:comunic/models/conversation.dart'; +import 'package:comunic/ui/routes/home_route.dart'; import 'package:comunic/ui/routes/update_conversation_route.dart'; import 'package:comunic/ui/screens/conversation_screen.dart'; +import 'package:comunic/ui/widgets/comunic_back_button_widget.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; @@ -44,11 +46,10 @@ class _ConversationRouteState extends State { if (_conversation == null) return setError(true); - final conversationName = await ConversationsHelper.getConversationNameAsync(_conversation); - if(!this.mounted) return null; + if (!this.mounted) return null; if (conversationName == null) return setError(true); @@ -58,11 +59,11 @@ class _ConversationRouteState extends State { } void _openSettings() { - Navigator.of(context).push(MaterialPageRoute(builder: (b) { - return UpdateConversationRoute( - conversationID: widget.conversationID, - ); - })); + HomeController.of(context).push( + UpdateConversationRoute( + conversationID: widget.conversationID, + ), + hideNavBar: true); } Widget _buildRouteBody() { @@ -95,6 +96,7 @@ class _ConversationRouteState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( + leading: ComunicBackButton(), title: Text( _conversationName == null ? tr("Loading") : _conversationName, ), diff --git a/lib/ui/routes/home_route.dart b/lib/ui/routes/home_route.dart index 159bb77..3d91e11 100644 --- a/lib/ui/routes/home_route.dart +++ b/lib/ui/routes/home_route.dart @@ -1,5 +1,6 @@ import 'package:comunic/helpers/account_helper.dart'; import 'package:comunic/ui/routes/app_settings_route.dart'; +import 'package:comunic/ui/routes/conversation_route.dart'; import 'package:comunic/ui/screens/conversations_list_screen.dart'; import 'package:comunic/ui/screens/friends_list_screen.dart'; import 'package:comunic/ui/screens/group_screen.dart'; @@ -29,8 +30,14 @@ class HomeRoute extends StatefulWidget { class CurrPage { final BarCallbackActions action; final Map args; + final bool hideNavBar; - const CurrPage(this.action, {this.args}) : assert(action != null); + const CurrPage( + this.action, { + this.args, + this.hideNavBar = false, + }) : assert(action != null), + assert(hideNavBar != null); @override String toString() => @@ -60,6 +67,12 @@ abstract class HomeController extends State { /// Pop current page. Last page can not be popped void popPage(); + + /// Push a new widget + void push(Widget w, {bool hideNavBar}); + + /// Open a conversation + void openConversation(int convID); } /// Private implementation of HomeController @@ -129,6 +142,9 @@ class _HomeRouteState extends HomeController { /// Build the body of the application Widget _buildBody(BuildContext context) { switch (_currTab.action) { + case BarCallbackActions.OPEN_CUSTOM_WIDGET: + return _currTab.args["widget"]; + case BarCallbackActions.OPEN_NOTIFICATIONS: return NotificationsScreen(); @@ -158,6 +174,11 @@ class _HomeRouteState extends HomeController { userID: _currTab.args["userID"], ); + case BarCallbackActions.OPEN_CONVERSATION: + return ConversationRoute( + conversationID: _currTab.args["convID"], + ); + default: throw "Invalid tab : " + _currTab.toString(); } @@ -172,10 +193,12 @@ class _HomeRouteState extends HomeController { child: WillPopScope( onWillPop: _willPop, child: Scaffold( - appBar: ComunicAppBar( - onTap: _onTap, - selectedAction: _currTab.action, - ), + appBar: _currTab.hideNavBar + ? null + : ComunicAppBar( + onTap: _onTap, + selectedAction: _currTab.action, + ), body: SafeArea( child: _buildBody(context), ), @@ -232,4 +255,22 @@ class _HomeRouteState extends HomeController { _pushPage(CurrPage(BarCallbackActions.OPEN_USER_FRIENDS_LIST, args: {"userID": userID})); } + + @override + void push(Widget w, {bool hideNavBar = false}) { + _pushPage(CurrPage( + BarCallbackActions.OPEN_CUSTOM_WIDGET, + args: {"widget": w}, + hideNavBar: hideNavBar, + )); + } + + @override + void openConversation(int convID) { + _pushPage(CurrPage( + BarCallbackActions.OPEN_CONVERSATION, + args: {"convID": convID}, + hideNavBar: true, + )); + } } diff --git a/lib/ui/routes/update_conversation_route.dart b/lib/ui/routes/update_conversation_route.dart index d8f5075..6bd9dcd 100644 --- a/lib/ui/routes/update_conversation_route.dart +++ b/lib/ui/routes/update_conversation_route.dart @@ -3,6 +3,7 @@ import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/lists/users_list.dart'; import 'package:comunic/models/conversation.dart'; import 'package:comunic/ui/screens/update_conversation_screen.dart'; +import 'package:comunic/ui/widgets/comunic_back_button_widget.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; @@ -83,6 +84,7 @@ class _UpdateConversationRoute extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( + leading: ComunicBackButton(), title: Text(tr("Update a conversation")), ), body: _buildBody(), diff --git a/lib/ui/screens/conversations_list_screen.dart b/lib/ui/screens/conversations_list_screen.dart index 542bc24..72dd3ff 100644 --- a/lib/ui/screens/conversations_list_screen.dart +++ b/lib/ui/screens/conversations_list_screen.dart @@ -3,9 +3,9 @@ import 'package:comunic/helpers/conversations_helper.dart'; import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/lists/conversations_list.dart'; import 'package:comunic/models/conversation.dart'; -import 'package:comunic/ui/routes/conversation_route.dart'; -import 'package:comunic/ui/routes/create_conversation_route.dart'; +import 'package:comunic/ui/routes/home_route.dart'; import 'package:comunic/ui/routes/update_conversation_route.dart'; +import 'package:comunic/ui/screens/create_conversation_screen.dart'; import 'package:comunic/ui/tiles/conversation_tile.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; @@ -94,27 +94,20 @@ class _ConversationScreenState extends State { } /// Open a conversation - void _openConversation(BuildContext context, int conversationId) { - Navigator.of(context).push(MaterialPageRoute(builder: (c) { - return ConversationRoute( - conversationID: conversationId, - ); - })); + void _openConversation(int conversationId) { + HomeController.of(context).openConversation(conversationId); } /// Create a new conversation - void _createConversation(BuildContext context) { - Navigator.of(context) - .push(MaterialPageRoute(builder: (c) => CreateConversationRoute())); + void _createConversation() { + HomeController.of(context).push(CreateConversationScreen()); } /// Handle conversations updated requests void _updateConversation(Conversation conversation) { - Navigator.of(context).push( - MaterialPageRoute( - builder: (c) => UpdateConversationRoute( - conversationID: conversation.id, - ), + HomeController.of(context).push( + UpdateConversationRoute( + conversationID: conversation.id, ), ); } @@ -181,7 +174,7 @@ class _ConversationScreenState extends State { conversation: _list.elementAt(index), usersList: _list.users, onOpen: (c) { - _openConversation(context, c.id); + _openConversation(c.id); }, onRequestUpdate: _updateConversation, onRequestDelete: _requestDeleteConversation, @@ -199,7 +192,7 @@ class _ConversationScreenState extends State { right: 20.0, bottom: 20.0, child: FloatingActionButton( - onPressed: () => _createConversation(context), + onPressed: () => _createConversation(), child: Icon(Icons.add), ), ), diff --git a/lib/ui/routes/create_conversation_route.dart b/lib/ui/screens/create_conversation_screen.dart similarity index 72% rename from lib/ui/routes/create_conversation_route.dart rename to lib/ui/screens/create_conversation_screen.dart index c0a2f65..62c266f 100644 --- a/lib/ui/routes/create_conversation_route.dart +++ b/lib/ui/screens/create_conversation_screen.dart @@ -1,4 +1,5 @@ import 'package:comunic/ui/screens/update_conversation_screen.dart'; +import 'package:comunic/ui/widgets/comunic_back_button_widget.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:flutter/material.dart'; @@ -6,14 +7,14 @@ import 'package:flutter/material.dart'; /// /// @author Pierre HUBERT -class CreateConversationRoute extends StatelessWidget { +class CreateConversationScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( + leading: ComunicBackButton(), title: Text(tr("Create a conversation")), ), - body: UpdateConversationScreen(), ); } diff --git a/lib/ui/screens/update_conversation_screen.dart b/lib/ui/screens/update_conversation_screen.dart index 6972688..a2a0487 100644 --- a/lib/ui/screens/update_conversation_screen.dart +++ b/lib/ui/screens/update_conversation_screen.dart @@ -1,7 +1,7 @@ import 'package:comunic/helpers/conversations_helper.dart'; import 'package:comunic/lists/users_list.dart'; import 'package:comunic/models/conversation.dart'; -import 'package:comunic/ui/routes/conversation_route.dart'; +import 'package:comunic/ui/routes/home_route.dart'; import 'package:comunic/ui/tiles/simple_user_tile.dart'; import 'package:comunic/ui/widgets/pick_user_widget.dart'; import 'package:comunic/utils/intl_utils.dart'; @@ -71,8 +71,8 @@ class _UpdateConversationScreen extends State { label: tr("Add member"), enabled: isOwner, onSelectUser: (user) => setState(() { - if (!_members.contains(user)) _members.add(user); - }), + if (!_members.contains(user)) _members.add(user); + }), ), //Conversation members @@ -91,11 +91,11 @@ class _UpdateConversationScreen extends State { _membersMenuItemSelected(i, choice), itemBuilder: (c) => >[ - PopupMenuItem( - child: Text(tr("Remove")), - value: _MembersMenuChoices.REMOVE, - ) - ], + PopupMenuItem( + child: Text(tr("Remove")), + value: _MembersMenuChoices.REMOVE, + ) + ], ) : null, ); @@ -110,8 +110,8 @@ class _UpdateConversationScreen extends State { Switch( value: _followConversation, onChanged: (b) => setState(() { - _followConversation = b; - }), + _followConversation = b; + }), ), Text(tr("Follow conversation")) ], @@ -158,7 +158,8 @@ class _UpdateConversationScreen extends State { if (isUpdating) error = !(await ConversationsHelper().updateConversation(settings)); else { - conversationID = await ConversationsHelper().createConversation(settings); + conversationID = + await ConversationsHelper().createConversation(settings); if (conversationID < 1) error = true; } @@ -172,9 +173,9 @@ class _UpdateConversationScreen extends State { )); // Open the conversation - Navigator.of(context).pushReplacement(MaterialPageRoute( - builder: (c) => ConversationRoute( - conversationID: conversationID, - ))); + + HomeController.of(context).popPage(); + if (!isUpdating) + HomeController.of(context).openConversation(conversationID); } } diff --git a/lib/ui/widgets/comunic_back_button_widget.dart b/lib/ui/widgets/comunic_back_button_widget.dart new file mode 100644 index 0000000..c52f7d6 --- /dev/null +++ b/lib/ui/widgets/comunic_back_button_widget.dart @@ -0,0 +1,11 @@ +import 'package:comunic/utils/navigation_utils.dart'; +import 'package:flutter/material.dart'; + +class ComunicBackButton extends StatelessWidget { + @override + Widget build(BuildContext context) { + return BackButton( + onPressed: () => popPage(context), + ); + } +} diff --git a/lib/ui/widgets/navbar_widget.dart b/lib/ui/widgets/navbar_widget.dart index 8f3a00d..bab0b30 100644 --- a/lib/ui/widgets/navbar_widget.dart +++ b/lib/ui/widgets/navbar_widget.dart @@ -10,6 +10,7 @@ typedef OnSelectMenuAction = void Function(BarCallbackActions); /// Callback actions enum BarCallbackActions { + OPEN_CUSTOM_WIDGET, OPEN_NOTIFICATIONS, OPEN_CONVERSATIONS, OPEN_NEWEST_POSTS, @@ -22,6 +23,7 @@ enum BarCallbackActions { OPEN_APP_SETTINGS, OPEN_USER_FRIENDS_LIST, OPEN_ABOUT_DIALOG, + OPEN_CONVERSATION, NONE, ACTION_LOGOUT } diff --git a/lib/utils/conversations_utils.dart b/lib/utils/conversations_utils.dart index f577a09..9679491 100644 --- a/lib/utils/conversations_utils.dart +++ b/lib/utils/conversations_utils.dart @@ -1,5 +1,5 @@ import 'package:comunic/helpers/conversations_helper.dart'; -import 'package:comunic/ui/routes/conversation_route.dart'; +import 'package:comunic/ui/routes/home_route.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; @@ -18,13 +18,7 @@ Future openPrivateConversation(BuildContext context, int userID) async { } // Open the conversation - Navigator.of(context).push( - MaterialPageRoute( - builder: (c) => ConversationRoute( - conversationID: convID, - ), - ), - ); + HomeController.of(context).openConversation(convID); // Success return true; diff --git a/lib/utils/navigation_utils.dart b/lib/utils/navigation_utils.dart index dfffdd7..cd09aa4 100644 --- a/lib/utils/navigation_utils.dart +++ b/lib/utils/navigation_utils.dart @@ -10,6 +10,11 @@ import 'package:meta/meta.dart'; /// /// @author Pierre HUBERT +/// Pop a page +void popPage(BuildContext context) { + HomeController.of(context).popPage(); +} + /// Open the page of a user void openUserPage({@required int userID, @required BuildContext context}) { assert(userID != null); @@ -20,8 +25,7 @@ void openUserPage({@required int userID, @required BuildContext context}) { /// Open a post in full screen void openPostFullScreen(int postID, BuildContext context) { - Navigator.of(context) - .push(MaterialPageRoute(builder: (c) => SinglePostRoute(postID: postID))); + HomeController.of(context).push(SinglePostRoute(postID: postID)); } /// Open a virtual directory