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

300 lines
7.9 KiB
Dart
Raw Normal View History

2019-07-01 09:57:34 +00:00
import 'package:comunic/helpers/account_helper.dart';
2020-04-16 14:33:44 +00:00
import 'package:comunic/ui/routes/account_settings/account_settings_route.dart';
2019-11-01 08:59:22 +00:00
import 'package:comunic/ui/routes/app_settings_route.dart';
import 'package:comunic/ui/routes/conversation_route.dart';
2020-04-20 08:53:25 +00:00
import 'package:comunic/ui/screens/call_screen.dart';
2019-04-24 15:46:25 +00:00
import 'package:comunic/ui/screens/conversations_list_screen.dart';
import 'package:comunic/ui/screens/friends_list_screen.dart';
2020-04-15 16:06:20 +00:00
import 'package:comunic/ui/screens/group_screen.dart';
2020-04-15 10:04:19 +00:00
import 'package:comunic/ui/screens/groups_list_screen.dart';
2019-05-04 18:01:17 +00:00
import 'package:comunic/ui/screens/newest_posts.dart';
import 'package:comunic/ui/screens/notifications_screen.dart';
2020-04-16 11:13:31 +00:00
import 'package:comunic/ui/screens/other_friends_lists_screen.dart';
2020-04-17 08:30:29 +00:00
import 'package:comunic/ui/screens/search_screen.dart';
2020-04-16 11:26:04 +00:00
import 'package:comunic/ui/screens/user_access_denied_screen.dart';
import 'package:comunic/ui/screens/user_page_screen.dart';
2019-07-01 09:51:23 +00:00
import 'package:comunic/ui/widgets/navbar_widget.dart';
import 'package:comunic/utils/account_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
2019-07-01 09:57:34 +00:00
import 'package:comunic/utils/ui_utils.dart';
2019-04-22 17:16:26 +00:00
import 'package:flutter/material.dart';
2019-07-01 09:57:34 +00:00
import 'login_route.dart';
2019-04-22 17:16:26 +00:00
/// Main route of the application
///
/// @author Pierre HUBERT
2020-04-17 13:26:37 +00:00
class MainRoute extends StatefulWidget {
2019-04-23 09:08:38 +00:00
@override
2020-04-17 13:26:37 +00:00
State<StatefulWidget> createState() => _MainRouteState();
2019-04-23 09:08:38 +00:00
}
2020-04-15 14:03:04 +00:00
class CurrPage {
final BarCallbackActions action;
final Map<String, dynamic> args;
final bool hideNavBar;
2020-04-15 14:03:04 +00:00
const CurrPage(
this.action, {
this.args,
this.hideNavBar = false,
}) : assert(action != null),
assert(hideNavBar != null);
2020-04-15 16:06:20 +00:00
@override
String toString() =>
"CurrPage {\n\taction: " +
this.action.toString() +
"\n\targs: " +
this.args.toString() +
"\n}";
}
/// Public interface of home controller
2020-04-17 13:26:37 +00:00
abstract class MainController extends State<MainRoute> {
2020-04-16 10:06:01 +00:00
/// Get current instance of Home controller
2020-04-17 13:26:37 +00:00
static MainController of(BuildContext context) =>
context.findAncestorStateOfType<MainController>();
2020-04-16 10:06:01 +00:00
2020-04-16 11:13:31 +00:00
/// Open user page
void openUserPage(int userID);
2020-04-16 11:26:04 +00:00
void openUserAccessDeniedPage(int userID);
2020-04-15 16:06:20 +00:00
/// Open a specific group page specified by its [groupID]
void openGroup(int groupID);
2020-04-16 11:13:31 +00:00
/// Display the list of friends of a user
void openUserFriendsList(int userID);
2020-04-16 11:26:04 +00:00
/// 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);
2020-04-20 08:53:25 +00:00
/// Start a call for a given conversation
void startCall(int convID);
2020-04-15 14:03:04 +00:00
}
2020-04-15 16:06:20 +00:00
/// Private implementation of HomeController
2020-04-17 13:26:37 +00:00
class _MainRouteState extends MainController {
2020-04-15 14:03:04 +00:00
CurrPage get _currTab => history.last;
List<CurrPage> history = List();
2019-04-23 09:08:38 +00:00
/// Change currently visible tab
2020-04-15 14:03:04 +00:00
void _pushPage(CurrPage newPage) {
2019-04-23 09:08:38 +00:00
setState(() {
2020-04-15 14:03:04 +00:00
history.add(newPage);
2019-04-23 09:08:38 +00:00
});
}
2020-04-15 14:03:04 +00:00
/// Pop the page
2020-04-16 11:26:04 +00:00
void popPage() {
if (history.length > 1) history.removeLast();
2020-04-15 14:03:04 +00:00
setState(() {});
}
2019-04-23 09:08:38 +00:00
/// Allows to go to previous tab
Future<bool> _willPop() async {
if (history.length == 1) return true;
2020-04-16 11:26:04 +00:00
popPage();
2019-04-23 09:08:38 +00:00
return false;
}
/// Handles a new tab being tapped
2019-07-01 09:51:23 +00:00
void _onTap(BarCallbackActions action) {
2019-07-01 09:57:34 +00:00
/// Check more quick actions
switch (action) {
/// Open current user page
case BarCallbackActions.OPEN_MY_PAGE:
_openCurrentUserPage();
break;
2019-11-01 08:59:22 +00:00
/// Open app settings page
case BarCallbackActions.OPEN_APP_SETTINGS:
_openAppSettings();
break;
2020-04-16 14:33:44 +00:00
case BarCallbackActions.OPEN_ACCOUNT_SETTINGS:
_openAccountsSettings();
break;
2020-04-16 07:29:37 +00:00
/// Show about dialog
case BarCallbackActions.OPEN_ABOUT_DIALOG:
showAboutAppDialog(context);
break;
/// Logout user
2019-07-01 09:57:34 +00:00
case BarCallbackActions.ACTION_LOGOUT:
_logoutRequested();
break;
default:
2020-04-15 14:03:04 +00:00
_pushPage(CurrPage(action));
2019-07-01 09:57:34 +00:00
}
2019-04-23 09:08:38 +00:00
}
@override
void initState() {
super.initState();
2020-04-15 14:03:04 +00:00
// Default page: conversations list
2020-04-17 10:15:13 +00:00
_pushPage(CurrPage(BarCallbackActions.OPEN_NOTIFICATIONS));
2019-04-23 09:08:38 +00:00
}
/// Build the body of the application
Widget _buildBody(BuildContext context) {
2020-04-15 14:03:04 +00:00
switch (_currTab.action) {
case BarCallbackActions.OPEN_CUSTOM_WIDGET:
return _currTab.args["widget"];
case BarCallbackActions.OPEN_NOTIFICATIONS:
return NotificationsScreen();
2019-07-01 09:51:23 +00:00
case BarCallbackActions.OPEN_CONVERSATIONS:
2019-04-24 15:46:25 +00:00
return ConversationsListScreen();
2019-04-23 09:08:38 +00:00
2019-07-01 09:51:23 +00:00
case BarCallbackActions.OPEN_NEWEST_POSTS:
2019-05-04 18:01:17 +00:00
return NewestPostsScreen();
2019-07-01 09:51:23 +00:00
case BarCallbackActions.OPEN_FRIENDS:
2019-05-04 18:01:17 +00:00
return FriendsListScreen();
2020-04-16 11:13:31 +00:00
case BarCallbackActions.OPEN_USER_PAGE:
2020-04-16 11:26:04 +00:00
return UserPageScreen(userID: _currTab.args["userID"]);
case BarCallbackActions.OPEN_USER_ACCESS_DENIED_PAGE:
return UserAccessDeniedScreen(userID: _currTab.args["userID"]);
2020-04-16 11:13:31 +00:00
2020-04-17 08:30:29 +00:00
case BarCallbackActions.OPEN_SEARCH_PAGE:
return SearchScreen();
2020-04-15 10:04:19 +00:00
case BarCallbackActions.OPEN_GROUPS:
return GroupsListScreen();
2020-04-15 16:06:20 +00:00
case BarCallbackActions.OPEN_GROUP_PAGE:
return GroupPageScreen(groupID: _currTab.args["groupID"]);
2020-04-16 11:13:31 +00:00
case BarCallbackActions.OPEN_USER_FRIENDS_LIST:
return OtherUserFriendsListScreen(
userID: _currTab.args["userID"],
);
case BarCallbackActions.OPEN_CONVERSATION:
return ConversationRoute(
conversationID: _currTab.args["convID"],
);
2019-04-23 09:08:38 +00:00
default:
2019-07-01 09:51:23 +00:00
throw "Invalid tab : " + _currTab.toString();
2019-04-23 09:08:38 +00:00
}
}
2019-04-22 17:16:26 +00:00
@override
Widget build(BuildContext context) {
2019-07-01 09:51:23 +00:00
return Container(
color: Colors.blueAccent,
child: SafeArea(
// Avoid OS areas
child: WillPopScope(
onWillPop: _willPop,
child: Scaffold(
appBar: _currTab.hideNavBar
? null
: ComunicAppBar(
onTap: _onTap,
selectedAction: _currTab.action,
),
2019-07-01 09:51:23 +00:00
body: SafeArea(
child: _buildBody(context),
),
),
2019-04-23 09:08:38 +00:00
),
),
);
2019-04-22 17:16:26 +00:00
}
2019-07-01 09:57:34 +00:00
/// Open current user page
Future<void> _openCurrentUserPage() async {
2020-04-16 11:13:31 +00:00
this.openUserPage(userID());
}
2019-11-01 08:59:22 +00:00
void _openAppSettings() {
Navigator.of(context)
.push(MaterialPageRoute(builder: (c) => AppSettingsRoute()));
}
2020-04-16 14:33:44 +00:00
void _openAccountsSettings() {
Navigator.of(context)
.push(MaterialPageRoute(builder: (c) => AccountSettingsRoute()));
}
2019-07-01 09:57:34 +00:00
/// Handle logout requests from user
Future<void> _logoutRequested() async {
if (!await showConfirmDialog(
context: context,
message: tr("Do you really want to sign out from the application ?"),
title: tr("Sign out"))) return;
await AccountHelper().signOut();
Navigator.pushReplacement(context, MaterialPageRoute(builder: (c) {
2019-07-01 09:57:34 +00:00
return LoginRoute();
}));
}
2020-04-15 16:06:20 +00:00
@override
void openGroup(int groupID) {
_pushPage(CurrPage(BarCallbackActions.OPEN_GROUP_PAGE,
args: {"groupID": groupID}));
}
2020-04-16 11:13:31 +00:00
@override
void openUserPage(int userID) {
_pushPage(
CurrPage(BarCallbackActions.OPEN_USER_PAGE, args: {"userID": userID}));
}
2020-04-16 11:26:04 +00:00
@override
void openUserAccessDeniedPage(int userID) {
_pushPage(CurrPage(BarCallbackActions.OPEN_USER_ACCESS_DENIED_PAGE,
args: {"userID": userID}));
}
2020-04-16 11:13:31 +00:00
@override
void openUserFriendsList(int userID) {
_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,
));
}
2020-04-20 08:53:25 +00:00
@override
void startCall(int convID) {
push(CallScreen(convID: convID), hideNavBar: true);
}
2019-04-23 09:08:38 +00:00
}