From 569a04844b4bb969fb5f2c24ec08a76451dd7ab0 Mon Sep 17 00:00:00 2001 From: dab246 Date: Wed, 30 Nov 2022 11:51:46 +0700 Subject: [PATCH] TF-437 Store state change of mailbox in background/terminate --- ...> store_email_state_to_refresh_state.dart} | 0 .../store_mailbox_state_to_refresh_state.dart | 22 +++++++++++++++++ ...ore_email_state_to_refresh_interactor.dart | 6 ++--- ...e_mailbox_state_to_refresh_interactor.dart | 23 ++++++++++++++++++ .../presentation/action/fcm_action.dart | 14 +++++++++++ .../bindings/fcm_interactor_bindings.dart | 2 ++ .../controller/fcm_controller.dart | 2 ++ .../listener/change_listener.dart | 22 +++++++++++++++++ .../listener/email_change_listener.dart | 24 ++++--------------- .../listener/mailbox_change_listener.dart | 24 +++++++++++++++++++ 10 files changed, 117 insertions(+), 22 deletions(-) rename lib/features/push_notification/domain/state/{store_fcm_state_change_state.dart => store_email_state_to_refresh_state.dart} (100%) create mode 100644 lib/features/push_notification/domain/state/store_mailbox_state_to_refresh_state.dart create mode 100644 lib/features/push_notification/domain/usecases/store_mailbox_state_to_refresh_interactor.dart diff --git a/lib/features/push_notification/domain/state/store_fcm_state_change_state.dart b/lib/features/push_notification/domain/state/store_email_state_to_refresh_state.dart similarity index 100% rename from lib/features/push_notification/domain/state/store_fcm_state_change_state.dart rename to lib/features/push_notification/domain/state/store_email_state_to_refresh_state.dart diff --git a/lib/features/push_notification/domain/state/store_mailbox_state_to_refresh_state.dart b/lib/features/push_notification/domain/state/store_mailbox_state_to_refresh_state.dart new file mode 100644 index 000000000..e8f39f44c --- /dev/null +++ b/lib/features/push_notification/domain/state/store_mailbox_state_to_refresh_state.dart @@ -0,0 +1,22 @@ + +import 'package:core/presentation/state/failure.dart'; +import 'package:core/presentation/state/success.dart'; + +class StoreMailboxStateToRefreshLoading extends UIState {} + +class StoreMailboxStateToRefreshSuccess extends UIState { + + StoreMailboxStateToRefreshSuccess(); + + @override + List get props => []; +} + +class StoreMailboxStateToRefreshFailure extends FeatureFailure { + final dynamic exception; + + StoreMailboxStateToRefreshFailure(this.exception); + + @override + List get props => [exception]; +} \ No newline at end of file diff --git a/lib/features/push_notification/domain/usecases/store_email_state_to_refresh_interactor.dart b/lib/features/push_notification/domain/usecases/store_email_state_to_refresh_interactor.dart index 10aef9502..2f1398d0d 100644 --- a/lib/features/push_notification/domain/usecases/store_email_state_to_refresh_interactor.dart +++ b/lib/features/push_notification/domain/usecases/store_email_state_to_refresh_interactor.dart @@ -4,17 +4,17 @@ import 'package:dartz/dartz.dart'; import 'package:fcm/model/type_name.dart'; import 'package:jmap_dart_client/jmap/core/state.dart' as jmap; import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart'; -import 'package:tmail_ui_user/features/push_notification/domain/state/store_fcm_state_change_state.dart'; +import 'package:tmail_ui_user/features/push_notification/domain/state/store_email_state_to_refresh_state.dart'; class StoreEmailStateToRefreshInteractor { final FCMRepository _fcmRepository; StoreEmailStateToRefreshInteractor(this._fcmRepository); - Stream> execute(TypeName typeName, jmap.State newState) async* { + Stream> execute(jmap.State newState) async* { try { yield Right(StoreEmailStateToRefreshLoading()); - await _fcmRepository.storeStateToRefresh(typeName, newState); + await _fcmRepository.storeStateToRefresh(TypeName.emailType, newState); yield Right(StoreEmailStateToRefreshSuccess()); } catch (e) { yield Left(StoreEmailStateToRefreshFailure(e)); diff --git a/lib/features/push_notification/domain/usecases/store_mailbox_state_to_refresh_interactor.dart b/lib/features/push_notification/domain/usecases/store_mailbox_state_to_refresh_interactor.dart new file mode 100644 index 000000000..660db313a --- /dev/null +++ b/lib/features/push_notification/domain/usecases/store_mailbox_state_to_refresh_interactor.dart @@ -0,0 +1,23 @@ +import 'package:core/presentation/state/failure.dart'; +import 'package:core/presentation/state/success.dart'; +import 'package:dartz/dartz.dart'; +import 'package:fcm/model/type_name.dart'; +import 'package:jmap_dart_client/jmap/core/state.dart' as jmap; +import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart'; +import 'package:tmail_ui_user/features/push_notification/domain/state/store_mailbox_state_to_refresh_state.dart'; + +class StoreMailboxStateToRefreshInteractor { + final FCMRepository _fcmRepository; + + StoreMailboxStateToRefreshInteractor(this._fcmRepository); + + Stream> execute(jmap.State newState) async* { + try { + yield Right(StoreMailboxStateToRefreshLoading()); + await _fcmRepository.storeStateToRefresh(TypeName.mailboxType, newState); + yield Right(StoreMailboxStateToRefreshSuccess()); + } catch (e) { + yield Left(StoreMailboxStateToRefreshFailure(e)); + } + } +} \ 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 b190c2f6f..9f8205334 100644 --- a/lib/features/push_notification/presentation/action/fcm_action.dart +++ b/lib/features/push_notification/presentation/action/fcm_action.dart @@ -56,6 +56,20 @@ class SynchronizeMailboxOnForegroundAction extends FcmStateChangeAction { this.accountId ) : super(typeName, newState); + @override + List get props => [typeName, newState, accountId]; +} + +class StoreMailboxStateToRefreshAction extends FcmStateChangeAction { + + final AccountId accountId; + + StoreMailboxStateToRefreshAction( + 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/bindings/fcm_interactor_bindings.dart b/lib/features/push_notification/presentation/bindings/fcm_interactor_bindings.dart index d58b612db..ca36ab09c 100644 --- a/lib/features/push_notification/presentation/bindings/fcm_interactor_bindings.dart +++ b/lib/features/push_notification/presentation/bindings/fcm_interactor_bindings.dart @@ -18,6 +18,7 @@ import 'package:tmail_ui_user/features/push_notification/domain/usecases/registe import 'package:tmail_ui_user/features/push_notification/domain/usecases/store_device_id_interactor.dart'; import 'package:tmail_ui_user/features/push_notification/domain/usecases/store_email_delivery_state_interactor.dart'; import 'package:tmail_ui_user/features/push_notification/domain/usecases/store_email_state_to_refresh_interactor.dart'; +import 'package:tmail_ui_user/features/push_notification/domain/usecases/store_mailbox_state_to_refresh_interactor.dart'; import 'package:tmail_ui_user/features/thread/data/datasource/thread_datasource.dart'; import 'package:tmail_ui_user/features/thread/data/datasource_impl/thread_datasource_impl.dart'; import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart'; @@ -62,6 +63,7 @@ class FcmInteractorBindings extends InteractorsBindings { Get.lazyPut(() => GetFirebaseSubscriptionInteractor(Get.find())); Get.lazyPut(() => RegisterNewTokenInteractor(Get.find())); Get.lazyPut(() => GetDeviceIdInteractor(Get.find())); + Get.lazyPut(() => StoreMailboxStateToRefreshInteractor(Get.find())); } @override diff --git a/lib/features/push_notification/presentation/controller/fcm_controller.dart b/lib/features/push_notification/presentation/controller/fcm_controller.dart index 351425490..613ded819 100644 --- a/lib/features/push_notification/presentation/controller/fcm_controller.dart +++ b/lib/features/push_notification/presentation/controller/fcm_controller.dart @@ -147,6 +147,8 @@ class FcmController extends BaseController { } else if (typeName == TypeName.mailboxType) { if (isForeground) { return SynchronizeMailboxOnForegroundAction(typeName, newState, accountId); + } else { + return StoreMailboxStateToRefreshAction(typeName, newState, accountId); } } return null; diff --git a/lib/features/push_notification/presentation/listener/change_listener.dart b/lib/features/push_notification/presentation/listener/change_listener.dart index 10213ffad..24bf50f57 100644 --- a/lib/features/push_notification/presentation/listener/change_listener.dart +++ b/lib/features/push_notification/presentation/listener/change_listener.dart @@ -1,6 +1,28 @@ +import 'package:core/presentation/state/failure.dart'; +import 'package:core/presentation/state/success.dart'; +import 'package:core/utils/app_logger.dart'; +import 'package:dartz/dartz.dart'; import 'package:tmail_ui_user/features/base/action/ui_action.dart'; abstract class ChangeListener { void dispatchActions(List actions); + + void consumeState(Stream> newStateStream) { + newStateStream.listen( + _handleStateStream, + onError: (error, stackTrace) { + logError('ChangeListener::consumeState():onError:error: $error'); + logError('ChangeListener::consumeState():onError:stackTrace: $stackTrace'); + } + ); + } + + void _handleStateStream(Either newState) { + newState.fold(handleFailureViewState, handleSuccessViewState); + } + + void handleFailureViewState(Failure failure) {} + + void handleSuccessViewState(Success success) {} } \ No newline at end of file diff --git a/lib/features/push_notification/presentation/listener/email_change_listener.dart b/lib/features/push_notification/presentation/listener/email_change_listener.dart index ca6a7c24a..edf053408 100644 --- a/lib/features/push_notification/presentation/listener/email_change_listener.dart +++ b/lib/features/push_notification/presentation/listener/email_change_listener.dart @@ -2,8 +2,6 @@ import 'package:core/presentation/state/failure.dart'; import 'package:core/presentation/state/success.dart'; import 'package:core/utils/app_logger.dart'; -import 'package:dartz/dartz.dart'; -import 'package:fcm/model/type_name.dart'; import 'package:get/get.dart'; import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/core/state.dart' as jmap; @@ -123,21 +121,8 @@ class EmailChangeListener extends ChangeListener { ); } - void consumeState(Stream> newStateStream) { - newStateStream.listen( - _handleStateStream, - onError: (error, stackTrace) { - logError('EmailChangeListener::consumeState():onError:error: $error'); - logError('EmailChangeListener::consumeState():onError:stackTrace: $stackTrace'); - } - ); - } - - void _handleStateStream(Either newState) { - newState.fold(_handleFailureViewState, _handleSuccessViewState); - } - - void _handleFailureViewState(Failure failure) { + @override + void handleFailureViewState(Failure failure) { log('EmailChangeListener::_handleFailureViewState(): $failure'); if (failure is GetStoredEmailDeliveryStateFailure && failure.exception is NotFoundEmailDeliveryStateException) { @@ -145,7 +130,8 @@ class EmailChangeListener extends ChangeListener { } } - void _handleSuccessViewState(Success success) { + @override + void handleSuccessViewState(Success success) { log('EmailChangeListener::_handleSuccessViewState(): $success'); if (success is GetStoredEmailDeliveryStateSuccess) { if (_newState != success.state) { @@ -169,7 +155,7 @@ class EmailChangeListener extends ChangeListener { void _handleStoreEmailStateToRefreshAction(jmap.State newState) { log('EmailChangeListener::_handleStoreEmailStateToRefreshAction():newState: $newState'); if (_storeEmailStateToRefreshInteractor != null) { - consumeState(_storeEmailStateToRefreshInteractor!.execute(TypeName.emailType, newState)); + consumeState(_storeEmailStateToRefreshInteractor!.execute(newState)); } else { logError('EmailChangeListener::_handleStoreEmailStateToRefreshAction():_storeEmailStateToRefreshInteractor is 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 index 651d3bbf6..2ed18fd6d 100644 --- a/lib/features/push_notification/presentation/listener/mailbox_change_listener.dart +++ b/lib/features/push_notification/presentation/listener/mailbox_change_listener.dart @@ -1,9 +1,12 @@ +import 'package:core/presentation/state/failure.dart'; +import 'package:core/presentation/state/success.dart'; 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/domain/usecases/store_mailbox_state_to_refresh_interactor.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'; @@ -11,10 +14,12 @@ import 'package:tmail_ui_user/main/routes/route_navigation.dart'; class MailboxChangeListener extends ChangeListener { MailboxDashBoardController? _dashBoardController; + StoreMailboxStateToRefreshInteractor? _storeMailboxStateToRefreshInteractor; MailboxChangeListener._internal() { try { _dashBoardController = getBinding(); + _storeMailboxStateToRefreshInteractor = getBinding(); } catch (e) { logError('MailboxChangeListener::_internal(): IS NOT REGISTERED: ${e.toString()}'); } @@ -30,14 +35,33 @@ class MailboxChangeListener extends ChangeListener { for (var action in actions) { if (action is SynchronizeMailboxOnForegroundAction) { _synchronizeMailboxOnForegroundAction(action.newState); + } else if (action is StoreMailboxStateToRefreshAction) { + _handleStoreMailboxStateToRefreshAction(action.newState); } } } + @override + void handleFailureViewState(Failure failure) { + log('MailboxChangeListener::_handleFailureViewState(): $failure'); + } + + @override + void handleSuccessViewState(Success success) { + log('MailboxChangeListener::_handleSuccessViewState(): $success'); + } + void _synchronizeMailboxOnForegroundAction(jmap.State newState) { log('MailboxChangeListener::_synchronizeMailboxOnForegroundAction():newState: $newState'); if (_dashBoardController != null) { _dashBoardController!.dispatchAction(RefreshChangeMailboxAction(newState)); } } + + void _handleStoreMailboxStateToRefreshAction(jmap.State newState) { + log('MailboxChangeListener::_handleStoreMailboxStateToRefreshAction():newState: $newState'); + if (_storeMailboxStateToRefreshInteractor != null) { + consumeState(_storeMailboxStateToRefreshInteractor!.execute(newState)); + } + } } \ No newline at end of file