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

266 lines
7.1 KiB
Dart
Raw Normal View History

import 'package:comunic/helpers/notifications_helper.dart';
import 'package:comunic/models/count_unread_notifications.dart';
import 'package:comunic/ui/widgets/safe_state.dart';
2019-07-01 09:51:23 +00:00
import 'package:comunic/utils/intl_utils.dart';
2019-11-01 13:17:46 +00:00
import 'package:comunic/utils/ui_utils.dart';
2019-07-01 09:51:23 +00:00
import 'package:flutter/material.dart';
/// Navigation bar widget
///
/// @author Pierre HUBERT
typedef OnSelectMenuAction = void Function(BarCallbackActions);
/// Callback actions
enum BarCallbackActions {
OPEN_CUSTOM_WIDGET,
OPEN_NOTIFICATIONS,
2019-07-01 09:51:23 +00:00
OPEN_CONVERSATIONS,
OPEN_NEWEST_POSTS,
OPEN_FRIENDS,
OPEN_MY_PAGE,
2020-04-17 08:30:29 +00:00
OPEN_SEARCH_PAGE,
2020-04-15 10:04:19 +00:00
OPEN_GROUPS,
2020-04-15 16:06:20 +00:00
OPEN_GROUP_PAGE,
2020-04-16 11:13:31 +00:00
OPEN_USER_PAGE,
2020-04-16 11:26:04 +00:00
OPEN_USER_ACCESS_DENIED_PAGE,
2020-04-16 14:33:44 +00:00
OPEN_ACCOUNT_SETTINGS,
2019-11-01 08:59:22 +00:00
OPEN_APP_SETTINGS,
2020-04-16 11:13:31 +00:00
OPEN_USER_FRIENDS_LIST,
2020-04-16 07:29:37 +00:00
OPEN_ABOUT_DIALOG,
OPEN_CONVERSATION,
2019-07-01 09:51:23 +00:00
NONE,
ACTION_LOGOUT
}
2019-11-01 13:17:46 +00:00
Color _primaryColor() => darkTheme() ? Colors.black : Colors.blue;
2019-11-01 13:17:46 +00:00
Color _secondaryColor() => darkTheme() ? darkAccentColor : Colors.white;
2019-07-01 09:51:23 +00:00
/// Menu item information
class _MenuItem {
final String label;
final Widget icon;
final BarCallbackActions action;
final bool isMenu;
2019-11-01 08:59:22 +00:00
const _MenuItem({
@required this.label,
@required this.icon,
@required this.action,
this.isMenu = false,
}) : assert(label != null),
2019-07-01 09:51:23 +00:00
assert(icon != null || isMenu),
assert(action != null),
assert(isMenu != null);
}
/// Item of action menu
class _ActionMenuItem {
final String label;
final BarCallbackActions action;
const _ActionMenuItem({@required this.label, @required this.action})
: assert(label != null),
assert(action != null);
}
2019-07-01 09:51:23 +00:00
/// List of menu items to show
final _menuItems = <_MenuItem>[
_MenuItem(
label: tr("Notifications"),
icon: Icon(Icons.notifications),
action: BarCallbackActions.OPEN_NOTIFICATIONS),
2019-07-01 09:51:23 +00:00
_MenuItem(
label: tr("Conversations"),
icon: Icon(Icons.comment),
action: BarCallbackActions.OPEN_CONVERSATIONS),
_MenuItem(
label: tr("Newest"),
icon: Icon(Icons.refresh),
action: BarCallbackActions.OPEN_NEWEST_POSTS),
_MenuItem(
label: tr("Friends"),
icon: Icon(Icons.group),
action: BarCallbackActions.OPEN_FRIENDS),
_MenuItem(
label: tr("Menu"),
icon: Icon(Icons.more_vert),
isMenu: true,
action: BarCallbackActions.NONE)
];
/// List of menu actions items
final _menuActionsItem = <_ActionMenuItem>[
_ActionMenuItem(
label: tr("My Page"), action: BarCallbackActions.OPEN_MY_PAGE),
2020-04-15 10:04:19 +00:00
_ActionMenuItem(label: tr("Groups"), action: BarCallbackActions.OPEN_GROUPS),
2020-04-17 08:30:29 +00:00
_ActionMenuItem(
label: tr("Search"), action: BarCallbackActions.OPEN_SEARCH_PAGE),
_ActionMenuItem(
label: tr("Account settings"),
action: BarCallbackActions.OPEN_ACCOUNT_SETTINGS),
2019-11-01 08:59:22 +00:00
_ActionMenuItem(
label: tr("App settings"), action: BarCallbackActions.OPEN_APP_SETTINGS),
2020-04-16 07:29:37 +00:00
_ActionMenuItem(
label: tr("About Comunic"), action: BarCallbackActions.OPEN_ABOUT_DIALOG),
_ActionMenuItem(
label: tr("Sign out"), action: BarCallbackActions.ACTION_LOGOUT),
];
2019-07-01 09:51:23 +00:00
/// Public widget
class ComunicAppBar extends StatefulWidget implements PreferredSizeWidget {
2019-07-01 09:51:23 +00:00
final OnSelectMenuAction onTap;
final BarCallbackActions selectedAction;
const ComunicAppBar(
{Key key, @required this.onTap, @required this.selectedAction})
: assert(onTap != null),
super(key: key);
@override
_ComunicAppBarState createState() => _ComunicAppBarState();
@override
Size get preferredSize => Size.fromHeight(40);
}
class _ComunicAppBarState extends SafeState<ComunicAppBar> {
CountUnreadNotifications _unreadNotifications;
@override
void initState() {
_refreshCountUnread();
super.initState();
}
void _refreshCountUnread() async {
try {
final count = await NotificationsHelper().countUnread();
setState(() {
_unreadNotifications = count;
});
} catch (e, stack) {
print("Could not refresh the number of unread notifications: $e");
print(stack);
}
}
/// Get the number of unread notifications for the selected notice
int getNumberUnread(BarCallbackActions action) {
if (_unreadNotifications == null) return 0;
switch (action) {
case BarCallbackActions.OPEN_NOTIFICATIONS:
return _unreadNotifications.notifications;
case BarCallbackActions.OPEN_CONVERSATIONS:
return _unreadNotifications.conversations;
default:
return 0;
}
}
2019-07-01 09:51:23 +00:00
@override
Widget build(BuildContext context) {
return Material(
2019-11-01 13:17:46 +00:00
color: darkTheme() ? Colors.black : Colors.blue,
2019-07-01 09:51:23 +00:00
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: List.generate(
_menuItems.length,
(i) => _MenuItemWidget(
2019-11-01 08:59:22 +00:00
item: _menuItems[i],
onTap: widget.onTap,
isSelected: _menuItems[i].action == widget.selectedAction,
newNotice: getNumberUnread(_menuItems[i].action),
2019-11-01 08:59:22 +00:00
),
2019-07-01 09:51:23 +00:00
),
),
);
}
}
/// The [Widget] part of a menu item
class _MenuItemWidget extends StatelessWidget {
final _MenuItem item;
final OnSelectMenuAction onTap;
final bool isSelected;
2020-04-17 11:29:00 +00:00
/// Notifications notice
final int newNotice;
const _MenuItemWidget({
Key key,
@required this.item,
@required this.onTap,
@required this.isSelected,
this.newNotice = 0,
}) : assert(item != null),
2019-07-01 09:51:23 +00:00
assert(onTap != null),
assert(isSelected != null),
super(key: key);
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context));
return Expanded(
child: Material(
2019-11-01 13:17:46 +00:00
color: isSelected ? _secondaryColor() : _primaryColor(),
2019-07-01 09:51:23 +00:00
child: !item.isMenu
? InkWell(
child: _buildIconContainer(),
onTap: () => onTap(item.action),
)
: _buildContextMenuPopupButton(),
),
);
}
Widget _buildIconContainer() {
return Row(
2019-07-01 09:51:23 +00:00
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
2019-07-01 09:51:23 +00:00
children: <Widget>[
2020-04-17 11:29:00 +00:00
Spacer(flex: 2),
2019-07-01 09:51:23 +00:00
IconTheme(
data: IconThemeData(
2019-11-01 13:17:46 +00:00
color: isSelected ? _primaryColor() : _secondaryColor()),
2019-07-01 09:51:23 +00:00
child: item.icon,
),
newNotice > 0 ? Spacer() : Container(),
newNotice == 0
? Container()
: Material(
color: Colors.red,
child: Padding(
padding: const EdgeInsets.all(2.0),
child: Text(" $newNotice ",
style: TextStyle(color: Colors.white)),
),
borderRadius: BorderRadius.all(
Radius.circular(50.0),
)),
2020-04-17 11:29:00 +00:00
Spacer(flex: 2),
2019-07-01 09:51:23 +00:00
],
);
}
// Build context menu
Widget _buildContextMenuPopupButton() {
return PopupMenuButton<BarCallbackActions>(
child: _buildIconContainer(),
itemBuilder: (i) => _menuActionsItem
.map((f) => PopupMenuItem(
child: Text(f.label),
value: f.action,
))
.toList(),
2019-07-01 09:51:23 +00:00
onSelected: onTap,
);
}
}