Auto refresh mails when resume app from background

Signed-off-by: dab246 <tdvu@linagora.com>
This commit is contained in:
dab246
2025-06-17 17:03:39 +07:00
committed by Dat H. Pham
parent 4bd5ecc5d4
commit 23019ae030
17 changed files with 35 additions and 266 deletions
@@ -12,9 +12,9 @@ class EmailUIAction extends UIAction {
}
class RefreshChangeEmailAction extends EmailUIAction {
final jmap.State? newState;
final jmap.State newState;
RefreshChangeEmailAction(this.newState);
RefreshChangeEmailAction({required this.newState});
@override
List<Object?> get props => [newState];
@@ -15,9 +15,9 @@ class MailboxUIAction extends UIAction {
class SelectMailboxDefaultAction extends MailboxUIAction {}
class RefreshChangeMailboxAction extends MailboxUIAction {
final jmap.State? newState;
final jmap.State newState;
RefreshChangeMailboxAction(this.newState);
RefreshChangeMailboxAction({required this.newState});
@override
List<Object?> get props => [newState];
@@ -521,12 +521,12 @@ class MailboxController extends BaseMailboxController
}
}
void _refreshMailboxChanges({jmap.State? newState}) {
void _refreshMailboxChanges({required jmap.State newState}) {
log('MailboxController::_refreshMailboxChanges():newState: $newState');
if (accountId == null ||
session == null ||
currentMailboxState == null ||
newState == null) {
currentMailboxState == newState) {
_newFolderId = null;
return;
}
@@ -536,11 +536,6 @@ class MailboxController extends BaseMailboxController
Future<void> _handleWebSocketMessage(WebSocketMessage message) async {
try {
if (currentMailboxState == message.newState) {
log('MailboxController::_handleWebSocketMessage:Skipping redundant state: ${message.newState}');
return Future.value();
}
final refreshViewState = await refreshAllMailboxInteractor!.execute(
session!,
accountId!,
@@ -136,12 +136,6 @@ import 'package:tmail_ui_user/features/manage_account/presentation/model/account
import 'package:tmail_ui_user/features/manage_account/presentation/model/manage_account_arguments.dart';
import 'package:tmail_ui_user/features/network_connection/presentation/network_connection_controller.dart'
if (dart.library.html) 'package:tmail_ui_user/features/network_connection/presentation/web_network_connection_controller.dart';
import 'package:tmail_ui_user/features/push_notification/domain/state/get_email_state_to_refresh_state.dart';
import 'package:tmail_ui_user/features/push_notification/domain/state/get_mailbox_state_to_refresh_state.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/delete_email_state_to_refresh_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/delete_mailbox_state_to_refresh_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_email_state_to_refresh_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_mailbox_state_to_refresh_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/controller/web_socket_controller.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_manager.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/services/fcm_service.dart';
@@ -232,10 +226,6 @@ class MailboxDashBoardController extends ReloadableController
GetAllVacationInteractor? _getAllVacationInteractor;
UpdateVacationInteractor? _updateVacationInteractor;
GetEmailStateToRefreshInteractor? _getEmailStateToRefreshInteractor;
DeleteEmailStateToRefreshInteractor? _deleteEmailStateToRefreshInteractor;
GetMailboxStateToRefreshInteractor? _getMailboxStateToRefreshInteractor;
DeleteMailboxStateToRefreshInteractor? _deleteMailboxStateToRefreshInteractor;
GetAutoCompleteInteractor? _getAutoCompleteInteractor;
IOSNotificationManager? _iosNotificationManager;
GetServerSettingInteractor? getServerSettingInteractor;
@@ -386,12 +376,6 @@ class MailboxDashBoardController extends ReloadableController
AppLocalizations.of(currentContext!).your_email_being_sent,
leadingSVGIcon: imagePaths.icSendToast);
}
} else if (success is GetEmailStateToRefreshSuccess) {
dispatchEmailUIAction(RefreshChangeEmailAction(success.storedState));
_deleteEmailStateToRefreshAction();
} else if (success is GetMailboxStateToRefreshSuccess) {
dispatchMailboxUIAction(RefreshChangeMailboxAction(success.storedState));
_deleteMailboxStateToRefreshAction();
} else if (success is SendEmailSuccess) {
_handleSendEmailSuccess(success);
} else if (success is SaveEmailAsDraftsSuccess) {
@@ -1877,7 +1861,9 @@ class MailboxDashBoardController extends ReloadableController
if (result is Tuple2) {
if (result.value1 is VacationResponse) {
vacationResponse.value = result.value1;
dispatchMailboxUIAction(RefreshChangeMailboxAction(null));
dispatchMailboxUIAction(RefreshChangeMailboxAction(
newState: jmap.State('vacation-updated-state'),
));
}
await Future.delayed(
const Duration(milliseconds: 500),
@@ -1943,7 +1929,9 @@ class MailboxDashBoardController extends ReloadableController
if (result is Tuple2) {
if (result.value1 is VacationResponse) {
vacationResponse.value = result.value1;
dispatchMailboxUIAction(RefreshChangeMailboxAction(null));
dispatchMailboxUIAction(RefreshChangeMailboxAction(
newState: jmap.State('vacation-updated-state'),
));
}
await Future.delayed(
const Duration(milliseconds: 500),
@@ -2176,42 +2164,12 @@ class MailboxDashBoardController extends ReloadableController
void _handleRefreshActionWhenBackToApp(RefreshActionViewEvent viewEvent) {
log('MailboxDashBoardController::_handleRefreshActionWhenBackToApp():');
try {
_getEmailStateToRefreshInteractor = getBinding<GetEmailStateToRefreshInteractor>();
_getMailboxStateToRefreshInteractor = getBinding<GetMailboxStateToRefreshInteractor>();
} catch (e) {
logError('MailboxDashBoardController::_handleRefreshActionWhenBackToApp(): $e');
}
if (_getEmailStateToRefreshInteractor != null && accountId.value != null && sessionCurrent != null) {
consumeState(_getEmailStateToRefreshInteractor!.execute(accountId.value!, sessionCurrent!.username));
}
if (_getMailboxStateToRefreshInteractor != null && accountId.value != null && sessionCurrent != null) {
consumeState(_getMailboxStateToRefreshInteractor!.execute(accountId.value!, sessionCurrent!.username));
}
}
void _deleteEmailStateToRefreshAction() {
log('MailboxDashBoardController::_deleteEmailStateToRefreshAction():');
try {
_deleteEmailStateToRefreshInteractor = getBinding<DeleteEmailStateToRefreshInteractor>();
} catch (e) {
logError('MailboxDashBoardController::_deleteEmailStateToRefreshAction(): $e');
}
if (_deleteEmailStateToRefreshInteractor != null && accountId.value != null && sessionCurrent != null) {
consumeState(_deleteEmailStateToRefreshInteractor!.execute(accountId.value!, sessionCurrent!.username));
}
}
void _deleteMailboxStateToRefreshAction() {
log('MailboxDashBoardController::_deleteMailboxStateToRefreshAction():');
try {
_deleteMailboxStateToRefreshInteractor = getBinding<DeleteMailboxStateToRefreshInteractor>();
} catch (e) {
logError('MailboxDashBoardController::_deleteMailboxStateToRefreshAction(): $e');
}
if (_deleteMailboxStateToRefreshInteractor != null && accountId.value != null && sessionCurrent != null) {
consumeState(_deleteMailboxStateToRefreshInteractor!.execute(accountId.value!, sessionCurrent!.username));
}
dispatchEmailUIAction(RefreshChangeEmailAction(
newState: jmap.State('refresh-action-when-back-to-app-state'),
));
dispatchMailboxUIAction(RefreshChangeMailboxAction(
newState: jmap.State('refresh-action-when-back-to-app-state'),
));
}
void _handleClickLocalNotificationOnForeground(NotificationResponse? response) {
@@ -3029,7 +2987,9 @@ class MailboxDashBoardController extends ReloadableController
void _handleGetRestoredDeletedMessageSuccess(GetRestoredDeletedMessageSuccess success) async {
if (selectedMailbox.value != null && selectedMailbox.value!.isRecovered) {
dispatchEmailUIAction(RefreshChangeEmailAction(null));
dispatchEmailUIAction(RefreshChangeEmailAction(
newState: jmap.State('restored-deleted-message-success-state'),
));
}
if (success is GetRestoredDeletedMessageCompleted) {
@@ -1,18 +0,0 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
class DeleteEmailStateToRefreshLoading extends UIState {}
class DeleteEmailStateToRefreshSuccess extends UIState {
DeleteEmailStateToRefreshSuccess();
@override
List<Object> get props => [];
}
class DeleteEmailStateToRefreshFailure extends FeatureFailure {
DeleteEmailStateToRefreshFailure(dynamic exception) : super(exception: exception);
}
@@ -1,18 +0,0 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
class DeleteMailboxStateToRefreshLoading extends UIState {}
class DeleteMailboxStateToRefreshSuccess extends UIState {
DeleteMailboxStateToRefreshSuccess();
@override
List<Object> get props => [];
}
class DeleteMailboxStateToRefreshFailure extends FeatureFailure {
DeleteMailboxStateToRefreshFailure(dynamic exception) : super(exception: exception);
}
@@ -1,21 +0,0 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:jmap_dart_client/jmap/core/state.dart' as jmap;
class GetEmailStateToRefreshLoading extends UIState {}
class GetEmailStateToRefreshSuccess extends UIState {
final jmap.State storedState;
GetEmailStateToRefreshSuccess(this.storedState);
@override
List<Object> get props => [storedState];
}
class GetEmailStateToRefreshFailure extends FeatureFailure {
GetEmailStateToRefreshFailure(dynamic exception) : super(exception: exception);
}
@@ -1,21 +0,0 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:jmap_dart_client/jmap/core/state.dart' as jmap;
class GetMailboxStateToRefreshLoading extends UIState {}
class GetMailboxStateToRefreshSuccess extends UIState {
final jmap.State storedState;
GetMailboxStateToRefreshSuccess(this.storedState);
@override
List<Object> get props => [storedState];
}
class GetMailboxStateToRefreshFailure extends FeatureFailure {
GetMailboxStateToRefreshFailure(dynamic exception) : super(exception: exception);
}
@@ -1,24 +0,0 @@
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/account_id.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart';
import 'package:tmail_ui_user/features/push_notification/domain/state/delete_email_state_to_refresh_state.dart';
class DeleteEmailStateToRefreshInteractor {
final FCMRepository _fcmRepository;
DeleteEmailStateToRefreshInteractor(this._fcmRepository);
Stream<Either<Failure, Success>> execute(AccountId accountId, UserName userName) async* {
try {
yield Right<Failure, Success>(DeleteEmailStateToRefreshLoading());
await _fcmRepository.deleteStateToRefresh(accountId, userName, TypeName.emailType);
yield Right<Failure, Success>(DeleteEmailStateToRefreshSuccess());
} catch (e) {
yield Left<Failure, Success>(DeleteEmailStateToRefreshFailure(e));
}
}
}
@@ -1,24 +0,0 @@
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/account_id.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart';
import 'package:tmail_ui_user/features/push_notification/domain/state/delete_mailbox_state_to_refresh_state.dart';
class DeleteMailboxStateToRefreshInteractor {
final FCMRepository _fcmRepository;
DeleteMailboxStateToRefreshInteractor(this._fcmRepository);
Stream<Either<Failure, Success>> execute(AccountId accountId, UserName userName) async* {
try {
yield Right<Failure, Success>(DeleteMailboxStateToRefreshLoading());
await _fcmRepository.deleteStateToRefresh(accountId, userName, TypeName.mailboxType);
yield Right<Failure, Success>(DeleteMailboxStateToRefreshSuccess());
} catch (e) {
yield Left<Failure, Success>(DeleteMailboxStateToRefreshFailure(e));
}
}
}
@@ -1,24 +0,0 @@
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/account_id.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart';
import 'package:tmail_ui_user/features/push_notification/domain/state/get_email_state_to_refresh_state.dart';
class GetEmailStateToRefreshInteractor {
final FCMRepository _fcmRepository;
GetEmailStateToRefreshInteractor(this._fcmRepository);
Stream<Either<Failure, Success>> execute(AccountId accountId, UserName userName) async* {
try {
yield Right<Failure, Success>(GetEmailStateToRefreshLoading());
final storedState = await _fcmRepository.getStateToRefresh(accountId, userName, TypeName.emailType);
yield Right<Failure, Success>(GetEmailStateToRefreshSuccess(storedState));
} catch (e) {
yield Left<Failure, Success>(GetEmailStateToRefreshFailure(e));
}
}
}
@@ -1,24 +0,0 @@
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/account_id.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart';
import 'package:tmail_ui_user/features/push_notification/domain/state/get_mailbox_state_to_refresh_state.dart';
class GetMailboxStateToRefreshInteractor {
final FCMRepository _fcmRepository;
GetMailboxStateToRefreshInteractor(this._fcmRepository);
Stream<Either<Failure, Success>> execute(AccountId accountId, UserName userName) async* {
try {
yield Right<Failure, Success>(GetMailboxStateToRefreshLoading());
final storedState = await _fcmRepository.getStateToRefresh(accountId, userName, TypeName.mailboxType);
yield Right<Failure, Success>(GetMailboxStateToRefreshSuccess(storedState));
} catch (e) {
yield Left<Failure, Success>(GetMailboxStateToRefreshFailure(e));
}
}
}
@@ -43,14 +43,11 @@ import 'package:tmail_ui_user/features/push_notification/data/local/fcm_cache_ma
import 'package:tmail_ui_user/features/push_notification/data/network/fcm_api.dart';
import 'package:tmail_ui_user/features/push_notification/data/repository/fcm_repository_impl.dart';
import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/delete_email_state_to_refresh_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/delete_firebase_registration_cache_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/destroy_firebase_registration_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_email_changes_to_push_notification_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_email_changes_to_remove_notification_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_email_state_to_refresh_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_firebase_registration_by_device_id_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_mailbox_state_to_refresh_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_mailboxes_not_put_notifications_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_new_receive_email_from_notification_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_stored_email_delivery_state_interactor.dart';
@@ -147,11 +144,8 @@ class FcmInteractorBindings extends InteractorsBindings {
Get.lazyPut(() => GetEmailChangesToRemoveNotificationInteractor(
Get.find<FCMRepositoryImpl>(),
Get.find<EmailRepository>()));
Get.lazyPut(() => GetEmailStateToRefreshInteractor(Get.find<FCMRepositoryImpl>()));
Get.lazyPut(() => DeleteEmailStateToRefreshInteractor(Get.find<FCMRepositoryImpl>()));
Get.lazyPut(() => GetFirebaseRegistrationByDeviceIdInteractor(Get.find<FCMRepositoryImpl>()));
Get.lazyPut(() => RegisterNewFirebaseRegistrationTokenInteractor(Get.find<FCMRepositoryImpl>()));
Get.lazyPut(() => GetMailboxStateToRefreshInteractor(Get.find<FCMRepositoryImpl>()));
Get.lazyPut(() => StoreFirebaseRegistrationInteractor(Get.find<FCMRepositoryImpl>()));
Get.lazyPut(() => GetStoredFirebaseRegistrationInteractor(Get.find<FCMRepositoryImpl>()));
Get.lazyPut(() => DestroyFirebaseRegistrationInteractor(Get.find<FCMRepositoryImpl>()));
@@ -114,9 +114,9 @@ class EmailChangeListener extends ChangeListener {
void _synchronizeEmailOnForegroundAction(jmap.State newState) {
log('EmailChangeListener::_synchronizeEmailAction():newState: $newState');
if (_dashBoardController != null) {
_dashBoardController!.dispatchEmailUIAction(RefreshChangeEmailAction(newState));
}
_dashBoardController?.dispatchEmailUIAction(RefreshChangeEmailAction(
newState: newState,
));
}
void _pushNotificationAction(jmap.State newState, AccountId accountId, UserName userName, Session? session) {
@@ -1,4 +1,3 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
@@ -11,18 +10,19 @@ import 'package:tmail_ui_user/features/push_notification/presentation/listener/c
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()}');
logError(
'MailboxChangeListener::_internal(): IS NOT REGISTERED: ${e.toString()}');
}
}
static final MailboxChangeListener _instance = MailboxChangeListener._internal();
static final MailboxChangeListener _instance =
MailboxChangeListener._internal();
static MailboxChangeListener get instance => _instance;
@@ -48,8 +48,8 @@ class MailboxChangeListener extends ChangeListener {
void _synchronizeMailboxOnForegroundAction(jmap.State newState) {
log('MailboxChangeListener::_synchronizeMailboxOnForegroundAction():newState: $newState');
if (_dashBoardController != null) {
_dashBoardController!.dispatchMailboxUIAction(RefreshChangeMailboxAction(newState));
}
_dashBoardController?.dispatchMailboxUIAction(RefreshChangeMailboxAction(
newState: newState,
));
}
}
}
@@ -619,12 +619,12 @@ class ThreadController extends BaseController with EmailActionController {
return limit;
}
void _refreshEmailChanges({jmap.State? newState}) {
void _refreshEmailChanges({required jmap.State newState}) {
log('ThreadController::_refreshEmailChanges(): newState: $newState');
if (_accountId == null ||
_session == null ||
mailboxDashBoardController.currentEmailState == null ||
newState == null) {
mailboxDashBoardController.currentEmailState == newState) {
return;
}
@@ -633,12 +633,6 @@ class ThreadController extends BaseController with EmailActionController {
Future<void> _handleWebSocketMessage(WebSocketMessage message) async {
try {
if (mailboxDashBoardController.currentEmailState == null ||
mailboxDashBoardController.currentEmailState == message.newState) {
log('ThreadController::_handleWebSocketMessage:Skipping redundant state: ${message.newState}');
return Future.value();
}
if (searchController.isSearchEmailRunning) {
await _refreshChangeSearchEmail();
} else {
@@ -332,7 +332,7 @@ void main() {
threadController.onInit();
mockMailboxDashBoardController.emailUIAction.value =
RefreshChangeEmailAction(State('new-state'));
RefreshChangeEmailAction(newState: State('new-state'));
await untilCalled(mockSearchEmailInteractor.execute(
any,