mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-29 16:26:27 +00:00
189 lines
4.9 KiB
Dart
189 lines
4.9 KiB
Dart
import 'package:comunic/helpers/account_helper.dart';
|
|
import 'package:comunic/ui/routes/app_settings_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';
|
|
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/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';
|
|
import 'package:comunic/utils/ui_utils.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'login_route.dart';
|
|
|
|
/// Main route of the application
|
|
///
|
|
/// @author Pierre HUBERT
|
|
|
|
class HomeRoute extends StatefulWidget {
|
|
@override
|
|
State<StatefulWidget> createState() => _HomeRouteState();
|
|
|
|
/// Get current instance of Home controller
|
|
static HomeController of(BuildContext context) =>
|
|
context.findAncestorStateOfType<HomeController>();
|
|
}
|
|
|
|
class CurrPage {
|
|
final BarCallbackActions action;
|
|
final Map<String, dynamic> args;
|
|
|
|
const CurrPage(this.action, {this.args}) : assert(action != null);
|
|
|
|
@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> {
|
|
/// Open a specific group page specified by its [groupID]
|
|
void openGroup(int groupID);
|
|
}
|
|
|
|
/// Private implementation of HomeController
|
|
class _HomeRouteState extends HomeController {
|
|
CurrPage get _currTab => history.last;
|
|
List<CurrPage> history = List();
|
|
|
|
/// Change currently visible tab
|
|
void _pushPage(CurrPage newPage) {
|
|
setState(() {
|
|
history.add(newPage);
|
|
});
|
|
}
|
|
|
|
/// Pop the page
|
|
void _popPage() {
|
|
history.removeLast();
|
|
setState(() {});
|
|
}
|
|
|
|
/// Allows to go to previous tab
|
|
Future<bool> _willPop() async {
|
|
if (history.length == 1) return true;
|
|
|
|
_popPage();
|
|
return false;
|
|
}
|
|
|
|
/// Handles a new tab being tapped
|
|
void _onTap(BarCallbackActions action) {
|
|
/// Check more quick actions
|
|
switch (action) {
|
|
|
|
/// Open current user page
|
|
case BarCallbackActions.OPEN_MY_PAGE:
|
|
_openCurrentUserPage();
|
|
break;
|
|
|
|
/// Open app settings page
|
|
case BarCallbackActions.OPEN_APP_SETTINGS:
|
|
_openAppSettings();
|
|
break;
|
|
|
|
/// Logout user
|
|
case BarCallbackActions.ACTION_LOGOUT:
|
|
_logoutRequested();
|
|
break;
|
|
|
|
default:
|
|
_pushPage(CurrPage(action));
|
|
}
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
// Default page: conversations list
|
|
_pushPage(CurrPage(BarCallbackActions.OPEN_CONVERSATIONS));
|
|
}
|
|
|
|
/// Build the body of the application
|
|
Widget _buildBody(BuildContext context) {
|
|
switch (_currTab.action) {
|
|
case BarCallbackActions.OPEN_NOTIFICATIONS:
|
|
return NotificationsScreen();
|
|
|
|
case BarCallbackActions.OPEN_CONVERSATIONS:
|
|
return ConversationsListScreen();
|
|
|
|
case BarCallbackActions.OPEN_NEWEST_POSTS:
|
|
return NewestPostsScreen();
|
|
|
|
case BarCallbackActions.OPEN_FRIENDS:
|
|
return FriendsListScreen();
|
|
|
|
case BarCallbackActions.OPEN_GROUPS:
|
|
return GroupsListScreen();
|
|
|
|
case BarCallbackActions.OPEN_GROUP_PAGE:
|
|
return GroupPageScreen(groupID: _currTab.args["groupID"]);
|
|
|
|
default:
|
|
throw "Invalid tab : " + _currTab.toString();
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Container(
|
|
color: Colors.blueAccent,
|
|
child: SafeArea(
|
|
// Avoid OS areas
|
|
child: WillPopScope(
|
|
onWillPop: _willPop,
|
|
child: Scaffold(
|
|
appBar: ComunicAppBar(
|
|
onTap: _onTap,
|
|
selectedAction: _currTab.action,
|
|
),
|
|
body: SafeArea(
|
|
child: _buildBody(context),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Open current user page
|
|
Future<void> _openCurrentUserPage() async {
|
|
openUserPage(context: context, userID: userID());
|
|
}
|
|
|
|
void _openAppSettings() {
|
|
Navigator.of(context)
|
|
.push(MaterialPageRoute(builder: (c) => AppSettingsRoute()));
|
|
}
|
|
|
|
/// 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) {
|
|
return LoginRoute();
|
|
}));
|
|
}
|
|
|
|
@override
|
|
void openGroup(int groupID) {
|
|
_pushPage(CurrPage(BarCallbackActions.OPEN_GROUP_PAGE,
|
|
args: {"groupID": groupID}));
|
|
}
|
|
}
|