2020-05-10 16:29:43 +00:00
|
|
|
import 'package:comunic/helpers/account_helper.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';
|
2020-05-12 16:42:33 +00:00
|
|
|
import 'package:comunic/ui/routes/settings/account_settings_route.dart';
|
2020-05-10 16:29:43 +00:00
|
|
|
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
|
2020-05-13 16:36:20 +00:00
|
|
|
void pushPage(PageInfo page) {
|
|
|
|
popUntilMainRoute();
|
|
|
|
setState(() => _pagesStack.add(page));
|
|
|
|
}
|
2020-05-10 16:29:43 +00:00
|
|
|
|
|
|
|
/// Pop current page. Do not call this method directly.
|
|
|
|
@protected
|
|
|
|
void doPopPage() {
|
|
|
|
if (_pagesStack.length > 0) setState(() => _pagesStack.removeLast());
|
|
|
|
}
|
|
|
|
|
2020-05-09 12:56:33 +00:00
|
|
|
/// Pop until main route is reached
|
|
|
|
void popUntilMainRoute() => Navigator.of(context).popUntil((settings) =>
|
|
|
|
ModalRoute.of(context).isCurrent || !ModalRoute.of(context).isActive);
|
|
|
|
|
2020-05-11 16:32:58 +00:00
|
|
|
/// Go to the previous page (use for [WillPop] widget)
|
|
|
|
@protected
|
|
|
|
Future<bool> willPop() async {
|
|
|
|
if (numberOfPages == 1) return true;
|
|
|
|
|
|
|
|
popPage();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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(
|
2020-05-11 11:38:51 +00:00
|
|
|
type: PageType.USER_PAGE,
|
|
|
|
child: UserPageScreen(userID: userID),
|
|
|
|
id: userID));
|
2020-05-10 16:29:43 +00:00
|
|
|
|
|
|
|
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(
|
2020-05-11 11:50:13 +00:00
|
|
|
type: PageType.GROUP_PAGE,
|
|
|
|
child: GroupPageScreen(groupID: groupID),
|
|
|
|
id: groupID));
|
2020-05-10 16:29:43 +00:00
|
|
|
|
|
|
|
/// 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,
|
2020-05-11 16:12:49 +00:00
|
|
|
id: convID,
|
2020-05-10 16:29:43 +00:00
|
|
|
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
|
|
|
|
2020-05-12 16:40:07 +00:00
|
|
|
/// Open settings
|
|
|
|
void openSettings() => Navigator.of(context)
|
2020-05-10 16:29:43 +00:00
|
|
|
.push(MaterialPageRoute(builder: (c) => AccountSettingsRoute()));
|
|
|
|
|
|
|
|
/// 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();
|
|
|
|
}
|
2020-05-09 05:28:17 +00:00
|
|
|
}
|