1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 22:39:22 +00:00

Automatically change the number of unread conversations

This commit is contained in:
Pierre HUBERT 2020-04-18 14:33:07 +02:00
parent a8c358fd58
commit 526f698bf4
4 changed files with 30 additions and 8 deletions

View File

@ -16,6 +16,13 @@ class NewNumberNotifsEvent {
NewNumberNotifsEvent(this.newNum); NewNumberNotifsEvent(this.newNum);
} }
/// New number of unread conversations
class NewNumberUnreadConversations {
final int newNum;
NewNumberUnreadConversations(this.newNum);
}
class EventsHelper { class EventsHelper {
static EventBus _mgr = EventBus(); static EventBus _mgr = EventBus();

View File

@ -80,10 +80,16 @@ class WebSocketHelper {
/// Process an unattended message /// Process an unattended message
static _processUnattendedMessage(WsMessage msg) { static _processUnattendedMessage(WsMessage msg) {
switch (msg.title) { switch (msg.title) {
// New number of notifications
case "number_notifs": case "number_notifs":
EventsHelper.emit(NewNumberNotifsEvent(msg.data)); EventsHelper.emit(NewNumberNotifsEvent(msg.data));
break; break;
// New number of unread conversations
case "number_unread_conversations":
EventsHelper.emit(NewNumberUnreadConversations(msg.data));
break;
default: default:
throw Exception("Unknown message type: ${msg.title}"); throw Exception("Unknown message type: ${msg.title}");
} }

View File

@ -1,5 +1,6 @@
import 'package:comunic/enums/load_error_level.dart'; import 'package:comunic/enums/load_error_level.dart';
import 'package:comunic/helpers/conversations_helper.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/helpers/users_helper.dart';
import 'package:comunic/lists/conversations_list.dart'; import 'package:comunic/lists/conversations_list.dart';
import 'package:comunic/models/conversation.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/routes/update_conversation_route.dart';
import 'package:comunic/ui/screens/create_conversation_screen.dart'; import 'package:comunic/ui/screens/create_conversation_screen.dart';
import 'package:comunic/ui/tiles/conversation_tile.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/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart'; import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -20,7 +22,7 @@ class ConversationsListScreen extends StatefulWidget {
State<StatefulWidget> createState() => _ConversationScreenState(); State<StatefulWidget> createState() => _ConversationScreenState();
} }
class _ConversationScreenState extends State<ConversationsListScreen> { class _ConversationScreenState extends SafeState<ConversationsListScreen> {
final ConversationsHelper _conversationsHelper = ConversationsHelper(); final ConversationsHelper _conversationsHelper = ConversationsHelper();
final UsersHelper _usersHelper = UsersHelper(); final UsersHelper _usersHelper = UsersHelper();
ConversationsList _list; ConversationsList _list;
@ -29,14 +31,17 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
GlobalKey<RefreshIndicatorState>(); GlobalKey<RefreshIndicatorState>();
@override @override
void didChangeDependencies() { void initState() {
super.didChangeDependencies(); super.initState();
_loadConversations();
this.listen<NewNumberUnreadConversations>(
(d) => _refreshIndicatorKey.currentState.show());
} }
@override @override
void setState(fn) { void didChangeDependencies() {
if (mounted) super.setState(fn); super.didChangeDependencies();
_loadConversations();
} }
void setError(LoadErrorLevel err) => setState(() => _error = err); void setError(LoadErrorLevel err) => setState(() => _error = err);

View File

@ -114,7 +114,8 @@ class ComunicAppBar extends StatefulWidget implements PreferredSizeWidget {
final OnSelectMenuAction onTap; final OnSelectMenuAction onTap;
final BarCallbackActions selectedAction; 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), : assert(onTap != null),
super(key: key); super(key: key);
@ -137,6 +138,8 @@ class _ComunicAppBarState extends SafeState<ComunicAppBar> {
// Listen to notifications number update // Listen to notifications number update
this.listenChangeState<NewNumberNotifsEvent>( this.listenChangeState<NewNumberNotifsEvent>(
(d) => _unreadNotifications.notifications = d.newNum); (d) => _unreadNotifications.notifications = d.newNum);
this.listenChangeState<NewNumberUnreadConversations>(
(d) => _unreadNotifications.conversations = d.newNum);
} }
void _refreshCountUnread() async { void _refreshCountUnread() async {
@ -176,7 +179,8 @@ class _ComunicAppBarState extends SafeState<ComunicAppBar> {
crossAxisAlignment: CrossAxisAlignment.stretch, crossAxisAlignment: CrossAxisAlignment.stretch,
children: List.generate( children: List.generate(
_menuItems.length, _menuItems.length,
(i) => _MenuItemWidget( (i) =>
_MenuItemWidget(
item: _menuItems[i], item: _menuItems[i],
onTap: widget.onTap, onTap: widget.onTap,
isSelected: _menuItems[i].action == widget.selectedAction, isSelected: _menuItems[i].action == widget.selectedAction,