1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 15:03:22 +00:00
comunicmobile/lib/ui/routes/home_route.dart

195 lines
5.0 KiB
Dart
Raw Normal View History

2019-07-01 09:57:34 +00:00
import 'package:comunic/helpers/account_helper.dart';
2019-11-01 08:59:22 +00:00
import 'package:comunic/ui/routes/app_settings_route.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';
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';
import 'package:comunic/utils/navigation_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
2019-04-23 09:08:38 +00:00
class HomeRoute extends StatefulWidget {
@override
State<StatefulWidget> createState() => _HomeRouteState();
}
2020-04-15 14:03:04 +00:00
class CurrPage {
final BarCallbackActions action;
final Map<String, dynamic> args;
const CurrPage(this.action, {this.args}) : assert(action != 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
abstract class HomeController extends State<HomeRoute> {
2020-04-16 10:06:01 +00:00
/// Get current instance of Home controller
static HomeController of(BuildContext context) =>
context.findAncestorStateOfType<HomeController>();
2020-04-15 16:06:20 +00:00
/// Open a specific group page specified by its [groupID]
void openGroup(int groupID);
2020-04-15 14:03:04 +00:00
}
2020-04-15 16:06:20 +00:00
/// Private implementation of HomeController
class _HomeRouteState extends HomeController {
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
void _popPage() {
history.removeLast();
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-15 14:03: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 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
_pushPage(CurrPage(BarCallbackActions.OPEN_CONVERSATIONS));
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_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-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"]);
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: ComunicAppBar(
onTap: _onTap,
2020-04-15 14:03:04 +00:00
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 {
openUserPage(context: context, userID: userID());
}
2019-11-01 08:59:22 +00:00
void _openAppSettings() {
Navigator.of(context)
.push(MaterialPageRoute(builder: (c) => AppSettingsRoute()));
}
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}));
}
2019-04-23 09:08:38 +00:00
}