1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-30 14:58:52 +00:00
comunicmobile/lib/forez/ui/routes/forez_route.dart

211 lines
5.9 KiB
Dart
Raw Normal View History

2021-04-24 08:14:56 +00:00
import 'package:comunic/forez/helpers/forez_group_helper.dart';
2021-04-26 07:33:54 +00:00
import 'package:comunic/forez/ui/routes/forez_member_profile_route.dart';
2021-04-25 15:06:28 +00:00
import 'package:comunic/forez/ui/screens/forez_directory_screen.dart';
2021-04-24 08:35:12 +00:00
import 'package:comunic/helpers/events_helper.dart';
2021-04-24 07:16:29 +00:00
import 'package:comunic/models/conversation.dart';
import 'package:comunic/ui/dialogs/alert_dialog.dart';
import 'package:comunic/ui/routes/main_route/main_route.dart';
import 'package:comunic/ui/routes/main_route/page_info.dart';
2021-04-24 07:46:53 +00:00
import 'package:comunic/ui/screens/conversations_list_screen.dart';
2021-04-24 08:17:18 +00:00
import 'package:comunic/ui/screens/group_sections/forez_presence_section.dart';
2021-04-24 08:21:24 +00:00
import 'package:comunic/ui/screens/group_sections/group_posts_section.dart';
2021-04-24 07:46:53 +00:00
import 'package:comunic/ui/widgets/safe_state.dart';
import 'package:comunic/ui/widgets/status_widget.dart';
import 'package:comunic/ui/widgets/tab_transition_widget.dart';
2021-04-24 07:16:29 +00:00
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart';
/// Forez route
///
/// @author Pierre Hubert
class ForezRoute extends StatefulWidget implements MainRoute {
const ForezRoute({Key key}) : super(key: key);
@override
State<StatefulWidget> createState() => _MainRouteState();
}
/// Private implementation of HomeController
class _MainRouteState extends MainController {
2021-04-24 08:47:13 +00:00
final GlobalKey<_ForezRouteBodyState> _mainKey = GlobalKey();
2021-04-24 07:16:29 +00:00
@override
PageInfo get defaultPage => PageInfo(
2021-04-24 08:47:13 +00:00
type: PageType.OTHER_PAGE,
child: ForezRouteBody(key: _mainKey),
hideNavBar: true,
);
2021-04-24 07:16:29 +00:00
@override
Widget build(BuildContext context) {
2021-04-24 08:57:10 +00:00
if (forezGroup == null) return Text(tr("Missing Forez group!"));
2021-04-24 07:46:53 +00:00
return StatusWidget(
child: (c) => SafeArea(
2021-04-24 07:16:29 +00:00
// Avoid OS areas
child: WillPopScope(
onWillPop: willPop,
child: Scaffold(
appBar: currentPage.hideNavBar ? null : AppBar(),
body: SafeArea(key: currentPage.key, child: currentPage.child),
),
),
),
);
}
2021-04-25 15:55:39 +00:00
@override
void pushPage(PageInfo page) {
if (page.hideNavBar)
Navigator.of(context).push(MaterialPageRoute(builder: (c) => page.child));
else
super.pushPage(page);
}
2021-04-24 07:16:29 +00:00
@override
void openConversation(Conversation conv, {fullScreen: false}) {
// Forcefully open conversations in a "normal" way (do not display groups)
openConversationById(conv.id, fullScreen: fullScreen);
}
@override
2021-04-25 15:55:39 +00:00
void openGroup(int groupID, {int conversationID}) => _unsupportedFeature();
@override
2021-04-26 07:33:54 +00:00
void openUserPage(int userID) => pushPage(PageInfo(
child: ForezMemberProfileRoute(userID: userID),
hideNavBar: true,
canShowAsDialog: true,
type: PageType.USER_PAGE,
id: userID,
));
2021-04-25 15:55:39 +00:00
@override
void openFriendsList() => _unsupportedFeature();
void _unsupportedFeature() => alert(context,
tr("This feature is available only in the Comunic application!"));
2021-04-24 07:16:29 +00:00
}
2021-04-24 07:46:53 +00:00
enum _PopupMenuItems { ACTION_SETTINGS, ACTION_SIGN_OUT }
2021-04-24 07:16:29 +00:00
class ForezRouteBody extends StatefulWidget {
2021-04-24 08:47:13 +00:00
ForezRouteBody({Key key}) : super(key: key);
2021-04-24 07:16:29 +00:00
@override
_ForezRouteBodyState createState() => _ForezRouteBodyState();
}
2021-04-24 07:46:53 +00:00
class _ForezRouteBodyState extends SafeState<ForezRouteBody> {
@override
void initState() {
super.initState();
2021-04-24 08:35:12 +00:00
listenChangeState<NewNumberUnreadConversations>((event) {});
2021-04-24 07:46:53 +00:00
}
2021-04-24 07:16:29 +00:00
@override
Widget build(BuildContext context) {
2021-04-24 07:46:53 +00:00
return DefaultTabController(
length: _tabs.length,
child: Scaffold(
appBar: AppBar(
2021-04-24 08:14:56 +00:00
title: Text(forezGroup.name),
2021-04-24 07:46:53 +00:00
actions: <Widget>[_buildPopupMenuButton()],
bottom: TabBar(tabs: _tabs),
),
body: TabBarView(children: _tabsPages),
),
);
2021-04-24 07:16:29 +00:00
}
2021-04-24 07:46:53 +00:00
Widget _buildPopupMenuButton() => PopupMenuButton<_PopupMenuItems>(
itemBuilder: (c) => [
PopupMenuItem(
child: Text(tr("Settings")),
value: _PopupMenuItems.ACTION_SETTINGS,
),
PopupMenuItem(
child: Text(tr("Sign out")),
value: _PopupMenuItems.ACTION_SIGN_OUT,
),
],
onSelected: _onMenuSelection,
);
void _onMenuSelection(_PopupMenuItems value) {
switch (value) {
case _PopupMenuItems.ACTION_SETTINGS:
MainController.of(context).openSettings();
break;
case _PopupMenuItems.ACTION_SIGN_OUT:
2021-04-24 08:14:56 +00:00
MainController.of(context).requestLogout();
2021-04-24 07:46:53 +00:00
break;
}
}
final _list = <_Tab>[
2021-04-24 08:21:24 +00:00
// Posts tab
_Tab(
icon: Icons.auto_stories,
title: tr("Posts"),
widget: () => GroupPostsSection(group: forezGroup),
),
2021-04-24 07:46:53 +00:00
// Presence tab
_Tab(
icon: Icons.calendar_today,
title: tr("Presence"),
2021-04-24 08:17:18 +00:00
widget: () => ForezPresenceSection(groupID: forezGroup.id),
2021-04-24 07:46:53 +00:00
),
// Conversations tab
_Tab(
icon: Icons.question_answer,
title: tr("Conversations"),
2021-04-24 08:17:18 +00:00
widget: () => ConversationsListScreen(),
2021-04-24 07:46:53 +00:00
isUnread: (c) => StatusWidgetState.of(c).unreadConversations > 0,
),
// Directory tab
_Tab(
icon: Icons.import_contacts,
title: tr("Directory"),
2021-04-25 15:06:28 +00:00
widget: () => ForezDirectoryScreen(),
2021-04-24 07:46:53 +00:00
),
];
List<Tab> get _tabs => _list
.map((e) => Tab(
icon: Stack(
children: <Widget>[
Icon(e.icon),
e.isUnread?.call(context) ?? false
? Container(color: Colors.red, width: 10, height: 10)
: Container(width: 0)
],
),
text: e.title))
.toList();
List<Widget> get _tabsPages =>
2021-04-24 08:17:18 +00:00
_list.map((e) => TabTransitionWidget(e.widget())).toList();
2021-04-24 07:46:53 +00:00
}
class _Tab {
final IconData icon;
final String title;
2021-04-24 08:17:18 +00:00
final Widget Function() widget;
2021-04-24 07:46:53 +00:00
final bool Function(BuildContext) isUnread;
const _Tab({
@required this.icon,
@required this.title,
@required this.widget,
this.isUnread,
}) : assert(icon != null),
assert(title != null),
assert(widget != null);
2021-04-24 07:16:29 +00:00
}