From 49069a595cc435bcb708e7daf40d5165d3888800 Mon Sep 17 00:00:00 2001 From: dab246 Date: Wed, 30 Nov 2022 11:34:19 +0700 Subject: [PATCH] TF-437 Handle `mailbox/changes` in foreground --- .../presentation/mailbox_controller.dart | 5 +++ .../presentation/action/dashboard_action.dart | 10 +++++ .../presentation/action/fcm_action.dart | 14 ++++++ .../controller/fcm_controller.dart | 18 ++++++++ .../listener/mailbox_change_listener.dart | 43 +++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 lib/features/push_notification/presentation/listener/mailbox_change_listener.dart diff --git a/lib/features/mailbox/presentation/mailbox_controller.dart b/lib/features/mailbox/presentation/mailbox_controller.dart index 34a5dd1e7..ba21fc483 100644 --- a/lib/features/mailbox/presentation/mailbox_controller.dart +++ b/lib/features/mailbox/presentation/mailbox_controller.dart @@ -257,12 +257,17 @@ class MailboxController extends BaseMailboxController { }); dashboardActionWorker = ever(mailboxDashBoardController.dashBoardAction, (action) { + log('MailboxController::_registerListenerWorker():action: $action'); if (action is ClearSearchEmailAction) { _switchBackToMailboxDefault(); } else if (action is SelectMailboxDefaultAction) { if (mailboxDashBoardController.selectedMailbox.value == null) { _switchBackToMailboxDefault(); } + } else if (action is RefreshChangeMailboxAction) { + if (action.newState != _currentMailboxState) { + refreshMailboxChanges(); + } } }); } diff --git a/lib/features/mailbox_dashboard/presentation/action/dashboard_action.dart b/lib/features/mailbox_dashboard/presentation/action/dashboard_action.dart index fccbe9de3..cf889f988 100644 --- a/lib/features/mailbox_dashboard/presentation/action/dashboard_action.dart +++ b/lib/features/mailbox_dashboard/presentation/action/dashboard_action.dart @@ -139,6 +139,16 @@ class RefreshChangeEmailAction extends DashBoardAction { RefreshChangeEmailAction(this.newState); + @override + List get props => [newState]; +} + +class RefreshChangeMailboxAction extends DashBoardAction { + + final jmap.State? newState; + + RefreshChangeMailboxAction(this.newState); + @override List get props => [newState]; } \ No newline at end of file diff --git a/lib/features/push_notification/presentation/action/fcm_action.dart b/lib/features/push_notification/presentation/action/fcm_action.dart index d26d93a3b..b190c2f6f 100644 --- a/lib/features/push_notification/presentation/action/fcm_action.dart +++ b/lib/features/push_notification/presentation/action/fcm_action.dart @@ -42,6 +42,20 @@ class StoreEmailStateToRefreshAction extends FcmStateChangeAction { this.accountId ) : super(typeName, newState); + @override + List get props => [typeName, newState, accountId]; +} + +class SynchronizeMailboxOnForegroundAction extends FcmStateChangeAction { + + final AccountId accountId; + + SynchronizeMailboxOnForegroundAction( + TypeName typeName, + jmap.State newState, + this.accountId + ) : super(typeName, newState); + @override List get props => [typeName, newState, accountId]; } \ No newline at end of file diff --git a/lib/features/push_notification/presentation/controller/fcm_controller.dart b/lib/features/push_notification/presentation/controller/fcm_controller.dart index 914e39825..351425490 100644 --- a/lib/features/push_notification/presentation/controller/fcm_controller.dart +++ b/lib/features/push_notification/presentation/controller/fcm_controller.dart @@ -29,6 +29,7 @@ import 'package:tmail_ui_user/features/push_notification/presentation/bindings/f import 'package:tmail_ui_user/features/push_notification/presentation/controller/fcm_token_handler.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/extensions/state_change_extension.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/listener/email_change_listener.dart'; +import 'package:tmail_ui_user/features/push_notification/presentation/listener/mailbox_change_listener.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/services/fcm_service.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/utils/fcm_utils.dart'; import 'package:tmail_ui_user/features/session/domain/state/get_session_state.dart'; @@ -102,6 +103,7 @@ class FcmController extends BaseController { .toList(); final listEmailActions = listTypeName + .where((typeName) => typeName == TypeName.emailType || typeName == TypeName.emailDelivery) .map((typeName) => toFcmAction(typeName, accountId, mapTypeState, isForeground)) .whereNotNull() .toList(); @@ -111,6 +113,18 @@ class FcmController extends BaseController { if (listEmailActions.isNotEmpty) { EmailChangeListener.instance.dispatchActions(listEmailActions); } + + final listMailboxActions = listTypeName + .where((typeName) => typeName == TypeName.mailboxType) + .map((typeName) => toFcmAction(typeName, accountId, mapTypeState, isForeground)) + .whereNotNull() + .toList(); + + log('FcmController::_mappingTypeStateToAction():listMailboxActions: $listEmailActions'); + + if (listMailboxActions.isNotEmpty) { + MailboxChangeListener.instance.dispatchActions(listMailboxActions); + } } FcmAction? toFcmAction( @@ -130,6 +144,10 @@ class FcmController extends BaseController { if (!isForeground) { return PushNotificationAction(typeName, newState, accountId); } + } else if (typeName == TypeName.mailboxType) { + if (isForeground) { + return SynchronizeMailboxOnForegroundAction(typeName, newState, accountId); + } } return null; } diff --git a/lib/features/push_notification/presentation/listener/mailbox_change_listener.dart b/lib/features/push_notification/presentation/listener/mailbox_change_listener.dart new file mode 100644 index 000000000..651d3bbf6 --- /dev/null +++ b/lib/features/push_notification/presentation/listener/mailbox_change_listener.dart @@ -0,0 +1,43 @@ + +import 'package:core/utils/app_logger.dart'; +import 'package:jmap_dart_client/jmap/core/state.dart' as jmap; +import 'package:tmail_ui_user/features/base/action/ui_action.dart'; +import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/action/dashboard_action.dart'; +import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart'; +import 'package:tmail_ui_user/features/push_notification/presentation/action/fcm_action.dart'; +import 'package:tmail_ui_user/features/push_notification/presentation/listener/change_listener.dart'; +import 'package:tmail_ui_user/main/routes/route_navigation.dart'; + +class MailboxChangeListener extends ChangeListener { + + MailboxDashBoardController? _dashBoardController; + + MailboxChangeListener._internal() { + try { + _dashBoardController = getBinding(); + } catch (e) { + logError('MailboxChangeListener::_internal(): IS NOT REGISTERED: ${e.toString()}'); + } + } + + static final MailboxChangeListener _instance = MailboxChangeListener._internal(); + + static MailboxChangeListener get instance => _instance; + + @override + void dispatchActions(List actions) { + log('MailboxChangeListener::dispatchActions():actions: $actions'); + for (var action in actions) { + if (action is SynchronizeMailboxOnForegroundAction) { + _synchronizeMailboxOnForegroundAction(action.newState); + } + } + } + + void _synchronizeMailboxOnForegroundAction(jmap.State newState) { + log('MailboxChangeListener::_synchronizeMailboxOnForegroundAction():newState: $newState'); + if (_dashBoardController != null) { + _dashBoardController!.dispatchAction(RefreshChangeMailboxAction(newState)); + } + } +} \ No newline at end of file