From 526f698bf4111220ee184368455ee2b53ecf79bc Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 18 Apr 2020 14:33:07 +0200 Subject: [PATCH] Automatically change the number of unread conversations --- lib/helpers/events_helper.dart | 7 +++++++ lib/helpers/websocket_helper.dart | 6 ++++++ lib/ui/screens/conversations_list_screen.dart | 17 +++++++++++------ lib/ui/widgets/navbar_widget.dart | 8 ++++++-- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/helpers/events_helper.dart b/lib/helpers/events_helper.dart index d9c7984..fee32d9 100644 --- a/lib/helpers/events_helper.dart +++ b/lib/helpers/events_helper.dart @@ -16,6 +16,13 @@ class NewNumberNotifsEvent { NewNumberNotifsEvent(this.newNum); } +/// New number of unread conversations +class NewNumberUnreadConversations { + final int newNum; + + NewNumberUnreadConversations(this.newNum); +} + class EventsHelper { static EventBus _mgr = EventBus(); diff --git a/lib/helpers/websocket_helper.dart b/lib/helpers/websocket_helper.dart index 437242b..252ee9d 100644 --- a/lib/helpers/websocket_helper.dart +++ b/lib/helpers/websocket_helper.dart @@ -80,10 +80,16 @@ class WebSocketHelper { /// Process an unattended message static _processUnattendedMessage(WsMessage msg) { switch (msg.title) { + // New number of notifications case "number_notifs": EventsHelper.emit(NewNumberNotifsEvent(msg.data)); break; + // New number of unread conversations + case "number_unread_conversations": + EventsHelper.emit(NewNumberUnreadConversations(msg.data)); + break; + default: throw Exception("Unknown message type: ${msg.title}"); } diff --git a/lib/ui/screens/conversations_list_screen.dart b/lib/ui/screens/conversations_list_screen.dart index 2f7c6dc..8ff2e4b 100644 --- a/lib/ui/screens/conversations_list_screen.dart +++ b/lib/ui/screens/conversations_list_screen.dart @@ -1,5 +1,6 @@ import 'package:comunic/enums/load_error_level.dart'; import 'package:comunic/helpers/conversations_helper.dart'; +import 'package:comunic/helpers/events_helper.dart'; import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/lists/conversations_list.dart'; import 'package:comunic/models/conversation.dart'; @@ -7,6 +8,7 @@ import 'package:comunic/ui/routes/main_route.dart'; import 'package:comunic/ui/routes/update_conversation_route.dart'; import 'package:comunic/ui/screens/create_conversation_screen.dart'; import 'package:comunic/ui/tiles/conversation_tile.dart'; +import 'package:comunic/ui/widgets/safe_state.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; @@ -20,7 +22,7 @@ class ConversationsListScreen extends StatefulWidget { State createState() => _ConversationScreenState(); } -class _ConversationScreenState extends State { +class _ConversationScreenState extends SafeState { final ConversationsHelper _conversationsHelper = ConversationsHelper(); final UsersHelper _usersHelper = UsersHelper(); ConversationsList _list; @@ -29,14 +31,17 @@ class _ConversationScreenState extends State { GlobalKey(); @override - void didChangeDependencies() { - super.didChangeDependencies(); - _loadConversations(); + void initState() { + super.initState(); + + this.listen( + (d) => _refreshIndicatorKey.currentState.show()); } @override - void setState(fn) { - if (mounted) super.setState(fn); + void didChangeDependencies() { + super.didChangeDependencies(); + _loadConversations(); } void setError(LoadErrorLevel err) => setState(() => _error = err); diff --git a/lib/ui/widgets/navbar_widget.dart b/lib/ui/widgets/navbar_widget.dart index 7846b62..64314b4 100644 --- a/lib/ui/widgets/navbar_widget.dart +++ b/lib/ui/widgets/navbar_widget.dart @@ -114,7 +114,8 @@ class ComunicAppBar extends StatefulWidget implements PreferredSizeWidget { final OnSelectMenuAction onTap; final BarCallbackActions selectedAction; - const ComunicAppBar({Key key, @required this.onTap, @required this.selectedAction}) + const ComunicAppBar( + {Key key, @required this.onTap, @required this.selectedAction}) : assert(onTap != null), super(key: key); @@ -137,6 +138,8 @@ class _ComunicAppBarState extends SafeState { // Listen to notifications number update this.listenChangeState( (d) => _unreadNotifications.notifications = d.newNum); + this.listenChangeState( + (d) => _unreadNotifications.conversations = d.newNum); } void _refreshCountUnread() async { @@ -176,7 +179,8 @@ class _ComunicAppBarState extends SafeState { crossAxisAlignment: CrossAxisAlignment.stretch, children: List.generate( _menuItems.length, - (i) => _MenuItemWidget( + (i) => + _MenuItemWidget( item: _menuItems[i], onTap: widget.onTap, isSelected: _menuItems[i].action == widget.selectedAction,