1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 04:49:21 +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);
}
/// New number of unread conversations
class NewNumberUnreadConversations {
final int newNum;
NewNumberUnreadConversations(this.newNum);
}
class EventsHelper {
static EventBus _mgr = EventBus();

View File

@ -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}");
}

View File

@ -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<StatefulWidget> createState() => _ConversationScreenState();
}
class _ConversationScreenState extends State<ConversationsListScreen> {
class _ConversationScreenState extends SafeState<ConversationsListScreen> {
final ConversationsHelper _conversationsHelper = ConversationsHelper();
final UsersHelper _usersHelper = UsersHelper();
ConversationsList _list;
@ -29,14 +31,17 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
GlobalKey<RefreshIndicatorState>();
@override
void didChangeDependencies() {
super.didChangeDependencies();
_loadConversations();
void initState() {
super.initState();
this.listen<NewNumberUnreadConversations>(
(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);

View File

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