2020-05-06 16:52:35 +00:00
|
|
|
import 'package:comunic/helpers/events_helper.dart';
|
|
|
|
import 'package:comunic/helpers/notifications_helper.dart';
|
|
|
|
import 'package:comunic/models/count_unread_notifications.dart';
|
2020-05-11 11:53:15 +00:00
|
|
|
import 'package:comunic/ui/routes/main_route/main_route.dart';
|
2020-05-07 16:16:33 +00:00
|
|
|
import 'package:comunic/ui/screens/notifications_screen.dart';
|
2020-05-08 13:40:13 +00:00
|
|
|
import 'package:comunic/ui/screens/unread_conversations_screen.dart';
|
2020-05-06 16:52:35 +00:00
|
|
|
import 'package:comunic/ui/widgets/safe_state.dart';
|
2020-05-07 11:49:40 +00:00
|
|
|
import 'package:comunic/ui/widgets/tablet_mode/appbar_custom_dropdown_widget.dart';
|
2020-05-11 16:56:28 +00:00
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
2020-05-06 16:52:35 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
2020-05-06 16:54:32 +00:00
|
|
|
/// Comunic tablet AppBar widget
|
2020-05-06 16:52:35 +00:00
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
2020-05-06 16:54:32 +00:00
|
|
|
class ComunicTabletAppBarWidget extends StatefulWidget
|
2020-05-06 16:52:35 +00:00
|
|
|
implements PreferredSizeWidget {
|
|
|
|
@override
|
2020-05-06 16:54:32 +00:00
|
|
|
_ComunicTabletAppBarWidgetState createState() =>
|
|
|
|
_ComunicTabletAppBarWidgetState();
|
2020-05-06 16:52:35 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Size get preferredSize => Size.fromHeight(kToolbarHeight);
|
|
|
|
}
|
|
|
|
|
2020-05-06 16:54:32 +00:00
|
|
|
class _ComunicTabletAppBarWidgetState
|
|
|
|
extends SafeState<ComunicTabletAppBarWidget> {
|
2020-05-07 16:12:11 +00:00
|
|
|
final notificationsDropdownKey = GlobalKey<AppBarCustomDropDownWidgetState>();
|
|
|
|
final conversationsDropdownKey = GlobalKey<AppBarCustomDropDownWidgetState>();
|
|
|
|
|
2020-05-06 16:52:35 +00:00
|
|
|
var _unreadNotifications =
|
|
|
|
CountUnreadNotifications(notifications: 0, conversations: 0);
|
|
|
|
|
|
|
|
void _refreshCountUnread() async {
|
|
|
|
try {
|
|
|
|
final count = await NotificationsHelper().countUnread();
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
_unreadNotifications = count;
|
|
|
|
});
|
|
|
|
} catch (e, s) {
|
|
|
|
print("Could not refresh the number of unread notifications: $e\n$s");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
_refreshCountUnread();
|
|
|
|
|
|
|
|
// Listen to notifications number update
|
|
|
|
this.listenChangeState<NewNumberNotifsEvent>(
|
|
|
|
(d) => _unreadNotifications.notifications = d.newNum);
|
|
|
|
this.listenChangeState<NewNumberUnreadConversations>(
|
|
|
|
(d) => _unreadNotifications.conversations = d.newNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AppBar(
|
2020-05-11 11:53:15 +00:00
|
|
|
title: GestureDetector(
|
|
|
|
child: Text("Comunic"),
|
|
|
|
onTap: () => MainController.of(context).openLatestPostsPage()),
|
2020-05-06 16:52:35 +00:00
|
|
|
actions: <Widget>[
|
2020-05-07 11:49:40 +00:00
|
|
|
AppBarCustomDropDownWidget(
|
2020-05-07 16:12:11 +00:00
|
|
|
key: notificationsDropdownKey,
|
2020-05-06 16:52:35 +00:00
|
|
|
icon: Icon(Icons.notifications),
|
2020-05-07 11:49:40 +00:00
|
|
|
notificationsBadge: _unreadNotifications.notifications,
|
2020-05-07 16:16:33 +00:00
|
|
|
onBuildOverlay: (c) => Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
2020-05-07 17:13:22 +00:00
|
|
|
child: NotificationsScreen(useSmallDeleteButton: true),
|
2020-05-07 16:16:33 +00:00
|
|
|
),
|
2020-05-06 16:52:35 +00:00
|
|
|
),
|
2020-05-07 11:49:40 +00:00
|
|
|
AppBarCustomDropDownWidget(
|
2020-05-07 16:12:11 +00:00
|
|
|
key: conversationsDropdownKey,
|
2020-05-06 16:52:35 +00:00
|
|
|
icon: Icon(Icons.message),
|
2020-05-07 11:49:40 +00:00
|
|
|
notificationsBadge: _unreadNotifications.conversations,
|
2020-05-08 19:05:56 +00:00
|
|
|
onBuildOverlay: (c) => Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: UnreadConversationsScreen(),
|
|
|
|
),
|
2020-05-06 16:52:35 +00:00
|
|
|
),
|
2020-05-11 16:56:28 +00:00
|
|
|
PopupMenuButton<_MenuItemCallback>(
|
|
|
|
offset: Offset(0, 50),
|
|
|
|
itemBuilder: (c) => [
|
2020-05-11 16:57:37 +00:00
|
|
|
// Sign out
|
2020-05-11 16:56:28 +00:00
|
|
|
_MainMenuItem(
|
|
|
|
label: tr("Sign out"),
|
|
|
|
icon: Icons.power_settings_new,
|
|
|
|
onTap: () => MainController.of(context).requestLogout()),
|
|
|
|
],
|
|
|
|
onSelected: (v) => v(),
|
|
|
|
),
|
2020-05-06 16:52:35 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 16:56:28 +00:00
|
|
|
|
|
|
|
typedef _MenuItemCallback = void Function();
|
|
|
|
|
|
|
|
class _MainMenuItem extends PopupMenuEntry<_MenuItemCallback> {
|
|
|
|
final String label;
|
|
|
|
final IconData icon;
|
|
|
|
final _MenuItemCallback onTap;
|
|
|
|
|
|
|
|
const _MainMenuItem({
|
|
|
|
@required this.label,
|
|
|
|
@required this.icon,
|
|
|
|
@required this.onTap,
|
|
|
|
}) : assert(label != null),
|
|
|
|
assert(icon != null),
|
|
|
|
assert(onTap != null);
|
|
|
|
|
|
|
|
@override
|
|
|
|
__MainMenuItemState createState() => __MainMenuItemState();
|
|
|
|
|
|
|
|
@override
|
|
|
|
double get height => kMinInteractiveDimension;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool represents(_MenuItemCallback value) => onTap == value;
|
|
|
|
}
|
|
|
|
|
|
|
|
class __MainMenuItemState extends State<_MainMenuItem> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return ListTile(
|
|
|
|
leading: Icon(widget.icon),
|
|
|
|
title: Text(widget.label),
|
|
|
|
onTap: () => Navigator.of(context).pop(widget.onTap),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|