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

143 lines
3.7 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/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();
}
class _HomeRouteState extends State<HomeRoute> {
BarCallbackActions _currTab = BarCallbackActions.OPEN_CONVERSATIONS;
List<BarCallbackActions> history = List();
/// Change currently visible tab
void _changeTab(BarCallbackActions newTab) {
setState(() {
_currTab = newTab;
});
}
/// 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
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:
if (_currTab != action) history.add(action);
_changeTab(action);
}
}
@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();
case BarCallbackActions.OPEN_CONVERSATIONS:
return ConversationsListScreen();
case BarCallbackActions.OPEN_NEWEST_POSTS:
return NewestPostsScreen();
case BarCallbackActions.OPEN_FRIENDS:
return FriendsListScreen();
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,
),
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();
}));
}
}