TF-437 Handle mailbox/changes in foreground

This commit is contained in:
dab246
2022-11-30 11:34:19 +07:00
committed by Dat H. Pham
parent 045c7383e6
commit 49069a595c
5 changed files with 90 additions and 0 deletions
@@ -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();
}
}
});
}
@@ -139,6 +139,16 @@ class RefreshChangeEmailAction extends DashBoardAction {
RefreshChangeEmailAction(this.newState);
@override
List<Object?> get props => [newState];
}
class RefreshChangeMailboxAction extends DashBoardAction {
final jmap.State? newState;
RefreshChangeMailboxAction(this.newState);
@override
List<Object?> get props => [newState];
}
@@ -42,6 +42,20 @@ class StoreEmailStateToRefreshAction extends FcmStateChangeAction {
this.accountId
) : super(typeName, newState);
@override
List<Object?> 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<Object?> get props => [typeName, newState, accountId];
}
@@ -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;
}
@@ -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<MailboxDashBoardController>();
} 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<Action> 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));
}
}
}