1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-29 16:26:27 +00:00
comunicmobile/lib/ui/routes/main_route/main_route.dart

167 lines
5.8 KiB
Dart
Raw Normal View History

2020-05-10 16:29:43 +00:00
import 'package:comunic/helpers/account_helper.dart';
import 'package:comunic/ui/routes/account_settings/account_settings_route.dart';
import 'package:comunic/ui/routes/app_settings_route.dart';
import 'package:comunic/ui/routes/conversation_route.dart';
import 'package:comunic/ui/routes/login_route.dart';
import 'package:comunic/ui/routes/main_route/page_info.dart';
import 'package:comunic/ui/screens/call_screen.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';
import 'package:comunic/ui/screens/groups_list_screen.dart';
import 'package:comunic/ui/screens/newest_posts.dart';
import 'package:comunic/ui/screens/notifications_screen.dart';
import 'package:comunic/ui/screens/other_friends_lists_screen.dart';
import 'package:comunic/ui/screens/search_screen.dart';
import 'package:comunic/ui/screens/user_access_denied_screen.dart';
import 'package:comunic/ui/screens/user_page_screen.dart';
import 'package:comunic/utils/account_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
2020-05-05 11:21:37 +00:00
import 'package:flutter/material.dart';
/// Abstract main application route
///
/// This mixin contains methods available in all display modes
///
/// @author Pierre Hubert
2020-05-09 12:07:14 +00:00
/// Main controller key statically shared across application
///
/// Do not use it directly to avoid context leak, instead use the access method
/// [MainController.of]
final mainControllerKey = GlobalKey<MainController>();
2020-05-05 11:21:37 +00:00
mixin MainRoute implements StatefulWidget {}
/// Public interface of home controller
2020-05-09 12:18:09 +00:00
abstract class MainController extends State<MainRoute> {
2020-05-10 16:29:43 +00:00
final _pagesStack = List<PageInfo>();
/// Default page of the application
PageInfo get defaultPage;
/// Get the current page being active in the application
PageInfo get currentPage => _pagesStack.last;
/// Get the current number of page in application stack
int get numberOfPages => _pagesStack.length;
2020-05-05 11:21:37 +00:00
/// Get current instance of Home controller
2020-05-09 12:07:14 +00:00
static MainController of(BuildContext context) {
assert(context != null); // A future implementation might need context again
return mainControllerKey.currentState;
}
2020-05-05 11:21:37 +00:00
2020-05-10 16:29:43 +00:00
@override
void initState() {
super.initState();
_pagesStack.add(defaultPage);
}
/// Push a new page
void pushPage(PageInfo page) => setState(() => _pagesStack.add(page));
/// Pop current page. Do not call this method directly.
@protected
void doPopPage() {
if (_pagesStack.length > 0) setState(() => _pagesStack.removeLast());
}
/// Pop until main route is reached
void popUntilMainRoute() => Navigator.of(context).popUntil((settings) =>
ModalRoute.of(context).isCurrent || !ModalRoute.of(context).isActive);
2020-05-10 16:29:43 +00:00
/// Open notifications page
void openNotificationsPage() => pushPage(PageInfo(
type: PageType.NOTIFICATIONS_PAGE, child: NotificationsScreen()));
/// Open conversations page
void openConversationsPage() => pushPage(PageInfo(
type: PageType.CONVERSATIONS_LIST_PAGE,
child: ConversationsListScreen()));
/// Open latest posts page
void openLatestPostsPage() => pushPage(
PageInfo(type: PageType.LATEST_POSTS_PAGE, child: NewestPostsScreen()));
2020-05-05 11:21:37 +00:00
/// Open user page
2020-05-10 16:29:43 +00:00
void openUserPage(int userID) => pushPage(PageInfo(
type: PageType.USER_PAGE, child: UserPageScreen(userID: userID)));
void openUserAccessDeniedPage(int userID) =>
pushPage(PageInfo(child: UserAccessDeniedScreen(userID: userID)));
2020-05-05 11:21:37 +00:00
2020-05-10 16:29:43 +00:00
/// Open current user page
void openCurrentUserPage() => this.openUserPage(userID());
2020-05-05 11:21:37 +00:00
/// Open a specific group page specified by its [groupID]
2020-05-10 16:29:43 +00:00
void openGroup(int groupID) => pushPage(PageInfo(
type: PageType.GROUP_PAGE, child: GroupPageScreen(groupID: groupID)));
/// Display the list of friends of current user
void openFriendsList() => pushPage(
PageInfo(type: PageType.FRIENDS_LIST_PAGE, child: FriendsListScreen()));
/// Open search page
void openSearchPage() => pushPage(PageInfo(child: SearchScreen()));
/// Open groups list page
void openGroupsListPage() => pushPage(PageInfo(child: GroupsListScreen()));
2020-05-05 11:21:37 +00:00
/// Display the list of friends of a user
2020-05-10 16:29:43 +00:00
void openUserFriendsList(int userID) =>
pushPage(PageInfo(child: OtherUserFriendsListScreen(userID: userID)));
/// Push a new widget
void push(Widget w,
{bool hideNavBar = false, bool canShowAsDialog = false}) =>
pushPage(PageInfo(
child: w, hideNavBar: hideNavBar, canShowAsDialog: canShowAsDialog));
/// Open a conversation
void openConversation(int convID, {fullScreen: false}) => pushPage(PageInfo(
type: PageType.CONVERSATION_PAGE,
child: ConversationRoute(conversationID: convID),
hideNavBar: true,
));
/// Start a call for a given conversation
void startCall(int convID) =>
2020-05-10 16:34:54 +00:00
pushPage(PageInfo(child: CallScreen(convID: convID), hideNavBar: true));
2020-05-10 16:29:43 +00:00
/// Open account settings
void openAccountSettings() => Navigator.of(context)
.push(MaterialPageRoute(builder: (c) => AccountSettingsRoute()));
/// Open application settings
void openAppSettings() => Navigator.of(context)
.push(MaterialPageRoute(builder: (c) => AppSettingsRoute()));
/// Handle logout requests
void requestLogout() async {
if (!await showConfirmDialog(
context: context,
message: tr("Do you really want to sign out from the application ?"),
title: tr("Sign out"))) return;
popUntilMainRoute();
await AccountHelper().signOut();
Navigator.pushReplacement(context, MaterialPageRoute(builder: (c) {
return LoginRoute();
}));
}
2020-05-05 11:21:37 +00:00
/// Pop current page. Last page can not be popped
2020-05-09 12:18:09 +00:00
///
/// If the current route is not the main route, we pop one page
void popPage() {
2020-05-09 12:38:58 +00:00
if (!ModalRoute.of(context).isCurrent)
2020-05-09 12:18:09 +00:00
Navigator.of(context).pop();
else
doPopPage();
}
}