1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-29 06:18:51 +00:00
comunicmobile/lib/ui/routes/home_route.dart

143 lines
3.7 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';
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();
}
class _HomeRouteState extends State<HomeRoute> {
2019-07-01 09:51:23 +00:00
BarCallbackActions _currTab = BarCallbackActions.OPEN_CONVERSATIONS;
List<BarCallbackActions> history = List();
2019-04-23 09:08:38 +00:00
/// Change currently visible tab
2019-07-01 09:51:23 +00:00
void _changeTab(BarCallbackActions newTab) {
2019-04-23 09:08:38 +00:00
setState(() {
2019-07-01 09:51:23 +00:00
_currTab = newTab;
2019-04-23 09:08:38 +00:00
});
}
/// Allows to go to previous tab
Future<bool> _willPop() async {
if (history.length == 1) return true;
history.removeLast();
_changeTab(history[history.length - 1]);
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;
/// Logout user
2019-07-01 09:57:34 +00:00
case BarCallbackActions.ACTION_LOGOUT:
_logoutRequested();
break;
default:
if (_currTab != action) history.add(action);
_changeTab(action);
}
2019-04-23 09:08:38 +00:00
}
@override
void initState() {
super.initState();
history.add(_currTab);
}
/// Build the body of the application
Widget _buildBody(BuildContext context) {
switch (_currTab) {
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();
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,
selectedAction: _currTab,
),
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();
}));
}
2019-04-23 09:08:38 +00:00
}