TF-437 Store state change of mailbox in background/terminate

This commit is contained in:
dab246
2022-11-30 11:51:46 +07:00
committed by Dat H. Pham
parent 49069a595c
commit 569a04844b
10 changed files with 117 additions and 22 deletions
@@ -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<Object> get props => [];
}
class StoreMailboxStateToRefreshFailure extends FeatureFailure {
final dynamic exception;
StoreMailboxStateToRefreshFailure(this.exception);
@override
List<Object> get props => [exception];
}
@@ -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<Either<Failure, Success>> execute(TypeName typeName, jmap.State newState) async* {
Stream<Either<Failure, Success>> execute(jmap.State newState) async* {
try {
yield Right<Failure, Success>(StoreEmailStateToRefreshLoading());
await _fcmRepository.storeStateToRefresh(typeName, newState);
await _fcmRepository.storeStateToRefresh(TypeName.emailType, newState);
yield Right<Failure, Success>(StoreEmailStateToRefreshSuccess());
} catch (e) {
yield Left<Failure, Success>(StoreEmailStateToRefreshFailure(e));
@@ -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<Either<Failure, Success>> execute(jmap.State newState) async* {
try {
yield Right<Failure, Success>(StoreMailboxStateToRefreshLoading());
await _fcmRepository.storeStateToRefresh(TypeName.mailboxType, newState);
yield Right<Failure, Success>(StoreMailboxStateToRefreshSuccess());
} catch (e) {
yield Left<Failure, Success>(StoreMailboxStateToRefreshFailure(e));
}
}
}
@@ -56,6 +56,20 @@ class SynchronizeMailboxOnForegroundAction extends FcmStateChangeAction {
this.accountId
) : super(typeName, newState);
@override
List<Object?> 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<Object?> get props => [typeName, newState, accountId];
}
@@ -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<FCMRepositoryImpl>()));
Get.lazyPut(() => RegisterNewTokenInteractor(Get.find<FCMRepositoryImpl>()));
Get.lazyPut(() => GetDeviceIdInteractor(Get.find<FCMRepositoryImpl>()));
Get.lazyPut(() => StoreMailboxStateToRefreshInteractor(Get.find<FCMRepositoryImpl>()));
}
@override
@@ -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;
@@ -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<Action> actions);
void consumeState(Stream<Either<Failure, Success>> newStateStream) {
newStateStream.listen(
_handleStateStream,
onError: (error, stackTrace) {
logError('ChangeListener::consumeState():onError:error: $error');
logError('ChangeListener::consumeState():onError:stackTrace: $stackTrace');
}
);
}
void _handleStateStream(Either<Failure, Success> newState) {
newState.fold(handleFailureViewState, handleSuccessViewState);
}
void handleFailureViewState(Failure failure) {}
void handleSuccessViewState(Success success) {}
}
@@ -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<Either<Failure, Success>> newStateStream) {
newStateStream.listen(
_handleStateStream,
onError: (error, stackTrace) {
logError('EmailChangeListener::consumeState():onError:error: $error');
logError('EmailChangeListener::consumeState():onError:stackTrace: $stackTrace');
}
);
}
void _handleStateStream(Either<Failure, Success> 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');
}
@@ -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<MailboxDashBoardController>();
_storeMailboxStateToRefreshInteractor = getBinding<StoreMailboxStateToRefreshInteractor>();
} 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));
}
}
}