Fix: Update EmailDeliveryState to Keychain

Signed-off-by: dab246 <tdvu@linagora.com>
(cherry picked from commit ac72825c7bb5840a8d779d0d1a0a336c75ade232)
This commit is contained in:
dab246
2024-01-11 01:29:34 +07:00
committed by Dat Vu
parent 360d5a542b
commit 987b018bb7
15 changed files with 88 additions and 5 deletions
@@ -59,6 +59,7 @@ import 'package:tmail_ui_user/features/upload/presentation/controller/upload_con
import 'package:tmail_ui_user/main/bindings/network/binding_tag.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
import 'package:uuid/uuid.dart';
import 'package:worker_manager/worker_manager.dart';
@@ -98,6 +99,7 @@ class ComposerBindings extends BaseBindings {
Get.find<RemoteExceptionThrower>()));
Get.lazyPut(() => StateDataSourceImpl(
Get.find<StateCacheManager>(),
Get.find<IOSSharingManager>(),
Get.find<CacheExceptionThrower>()
));
Get.lazyPut(() => EmailHiveCacheDataSourceImpl(
@@ -28,6 +28,7 @@ import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart';
import 'package:tmail_ui_user/features/thread/data/network/thread_isolate_worker.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
class DestinationPickerBindings extends BaseBindings {
@@ -72,6 +73,7 @@ class DestinationPickerBindings extends BaseBindings {
Get.find<CacheExceptionThrower>()));
Get.lazyPut(() => StateDataSourceImpl(
Get.find<StateCacheManager>(),
Get.find<IOSSharingManager>(),
Get.find<CacheExceptionThrower>()
));
Get.lazyPut(() => EmailDataSourceImpl(
@@ -47,6 +47,7 @@ import 'package:tmail_ui_user/features/offline_mode/manager/sending_email_cache_
import 'package:tmail_ui_user/features/thread/data/local/email_cache_manager.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
class EmailBindings extends BaseBindings {
@@ -92,6 +93,7 @@ class EmailBindings extends BaseBindings {
Get.find<RemoteExceptionThrower>()));
Get.lazyPut(() => StateDataSourceImpl(
Get.find<StateCacheManager>(),
Get.find<IOSSharingManager>(),
Get.find<CacheExceptionThrower>()
));
Get.lazyPut(() => EmailHiveCacheDataSourceImpl(
@@ -1,4 +1,5 @@
import 'package:core/core.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
@@ -7,13 +8,19 @@ import 'package:tmail_ui_user/features/mailbox/data/local/state_cache_manager.da
import 'package:tmail_ui_user/features/mailbox/data/model/state_cache.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/state_type.dart';
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
class StateDataSourceImpl extends StateDataSource {
final StateCacheManager _stateCacheManager;
final IOSSharingManager _iosSharingManager;
final ExceptionThrower _exceptionThrower;
StateDataSourceImpl(this._stateCacheManager, this._exceptionThrower);
StateDataSourceImpl(
this._stateCacheManager,
this._iosSharingManager,
this._exceptionThrower
);
@override
Future<State?> getState(AccountId accountId, UserName userName, StateType stateType) {
@@ -25,7 +32,14 @@ class StateDataSourceImpl extends StateDataSource {
@override
Future<void> saveState(AccountId accountId, UserName userName, StateCache stateCache) {
return Future.sync(() async {
return await _stateCacheManager.saveState(accountId, userName, stateCache);
await _stateCacheManager.saveState(accountId, userName, stateCache);
if (PlatformInfo.isIOS && stateCache.type == StateType.email) {
final emailDeliveryStateKeychain = await _iosSharingManager.getEmailDeliveryStateFromKeychain(accountId);
if (emailDeliveryStateKeychain == null || emailDeliveryStateKeychain.isEmpty) {
await _iosSharingManager.updateEmailDeliveryStateInKeyChain(accountId, stateCache.state);
}
}
return Future.value(null);
}).catchError(_exceptionThrower.throwException);
}
}
@@ -33,6 +33,7 @@ import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart';
import 'package:tmail_ui_user/features/thread/data/network/thread_isolate_worker.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
class MailboxBindings extends BaseBindings {
@@ -82,6 +83,7 @@ class MailboxBindings extends BaseBindings {
Get.find<CacheExceptionThrower>()));
Get.lazyPut(() => StateDataSourceImpl(
Get.find<StateCacheManager>(),
Get.find<IOSSharingManager>(),
Get.find<CacheExceptionThrower>()
));
Get.lazyPut(() => EmailDataSourceImpl(
@@ -110,6 +110,7 @@ import 'package:tmail_ui_user/features/thread/domain/usecases/search_more_email_
import 'package:tmail_ui_user/features/thread/presentation/thread_bindings.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
class MailboxDashBoardBindings extends BaseBindings {
@@ -202,6 +203,7 @@ class MailboxDashBoardBindings extends BaseBindings {
Get.lazyPut(() => LocalThreadDataSourceImpl(Get.find<EmailCacheManager>(), Get.find<CacheExceptionThrower>()));
Get.lazyPut(() => StateDataSourceImpl(
Get.find<StateCacheManager>(),
Get.find<IOSSharingManager>(),
Get.find<CacheExceptionThrower>()
));
Get.lazyPut(() => MailboxDataSourceImpl(
@@ -18,6 +18,7 @@ import 'package:tmail_ui_user/features/mailbox/domain/usecases/subscribe_mailbox
import 'package:tmail_ui_user/features/mailbox/domain/usecases/subscribe_multiple_mailbox_interactor.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
class MailboxVisibilityInteractorBindings extends InteractorsBindings {
@@ -38,6 +39,7 @@ class MailboxVisibilityInteractorBindings extends InteractorsBindings {
Get.find<CacheExceptionThrower>()));
Get.lazyPut(() => StateDataSourceImpl(
Get.find<StateCacheManager>(),
Get.find<IOSSharingManager>(),
Get.find<CacheExceptionThrower>()
));
}
@@ -31,6 +31,7 @@ import 'package:tmail_ui_user/features/offline_mode/manager/sending_email_cache_
import 'package:tmail_ui_user/features/thread/data/local/email_cache_manager.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
class SendEmailInteractorBindings extends InteractorsBindings {
@@ -59,6 +60,7 @@ class SendEmailInteractorBindings extends InteractorsBindings {
Get.find<RemoteExceptionThrower>()));
Get.lazyPut(() => StateDataSourceImpl(
Get.find<StateCacheManager>(),
Get.find<IOSSharingManager>(),
Get.find<CacheExceptionThrower>()));
Get.lazyPut(() => EmailHiveCacheDataSourceImpl(
Get.find<NewEmailCacheManager>(),
@@ -0,0 +1,19 @@
import 'package:tmail_ui_user/features/push_notification/data/keychain/keychain_sharing_session.dart';
extension KeychainSharingSessionExtension on KeychainSharingSession {
KeychainSharingSession updating({String? emailDeliveryState}) {
return KeychainSharingSession(
accountId: accountId,
userName: userName,
authenticationType: authenticationType,
apiUrl: apiUrl,
emailState: emailDeliveryState ?? emailDeliveryState,
tokenOIDC: tokenOIDC,
basicAuth: basicAuth,
tokenEndpoint: tokenEndpoint,
oidcScopes: oidcScopes,
);
}
}
@@ -59,6 +59,7 @@ import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart';
import 'package:tmail_ui_user/features/thread/data/network/thread_isolate_worker.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
class FcmInteractorBindings extends InteractorsBindings {
@@ -102,6 +103,7 @@ class FcmInteractorBindings extends InteractorsBindings {
Get.find<RemoteExceptionThrower>()));
Get.lazyPut(() => StateDataSourceImpl(
Get.find<StateCacheManager>(),
Get.find<IOSSharingManager>(),
Get.find<CacheExceptionThrower>()
));
Get.lazyPut(() => EmailHiveCacheDataSourceImpl(
@@ -47,6 +47,7 @@ import 'package:tmail_ui_user/features/push_notification/presentation/listener/c
import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_manager.dart';
import 'package:tmail_ui_user/features/thread/domain/constants/thread_constants.dart';
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
class EmailChangeListener extends ChangeListener {
@@ -62,6 +63,7 @@ class EmailChangeListener extends ChangeListener {
GetListDetailedEmailByIdInteractor? _getListDetailedEmailByIdInteractor;
DynamicUrlInterceptors? _dynamicUrlInterceptors;
StoreListNewEmailInteractor? _storeListNewEmailInteractor;
IOSSharingManager? _iosSharingManager;
jmap.State? _newStateEmailDelivery;
AccountId? _accountId;
@@ -83,6 +85,7 @@ class EmailChangeListener extends ChangeListener {
_getListDetailedEmailByIdInteractor = getBinding<GetListDetailedEmailByIdInteractor>();
_dynamicUrlInterceptors = getBinding<DynamicUrlInterceptors>();
_storeListNewEmailInteractor = getBinding<StoreListNewEmailInteractor>();
_iosSharingManager = getBinding<IOSSharingManager>();
} catch (e) {
logError('EmailChangeListener::_internal(): IS NOT REGISTERED: ${e.toString()}');
}
@@ -133,8 +136,11 @@ class EmailChangeListener extends ChangeListener {
_userName = userName;
log('EmailChangeListener::_pushNotificationAction():newState: $newState');
if (PlatformInfo.isWeb || PlatformInfo.isIOS) {
if (PlatformInfo.isWeb) {
_storeEmailDeliveryStateAction(accountId, userName, _newStateEmailDelivery!);
} else if (PlatformInfo.isIOS) {
_storeEmailDeliveryStateAction(accountId, userName, _newStateEmailDelivery!);
_updateKeychainSharingSession(accountId, _newStateEmailDelivery!.value);
} else if (PlatformInfo.isAndroid) {
_getStoredEmailDeliveryState(accountId, userName);
}
@@ -322,4 +328,15 @@ class EmailChangeListener extends ChangeListener {
));
}
}
void _updateKeychainSharingSession(AccountId accountId, String emailDeliveryState) async {
if (_iosSharingManager == null) {
return;
}
final emailDeliveryStateKeychain = await _iosSharingManager!.getEmailDeliveryStateFromKeychain(accountId);
if (emailDeliveryStateKeychain == null || emailDeliveryStateKeychain.isEmpty) {
await _iosSharingManager!.updateEmailDeliveryStateInKeyChain(accountId, emailDeliveryState);
}
}
}
@@ -18,6 +18,7 @@ import 'package:tmail_ui_user/features/mailbox_creator/domain/usecases/verify_na
import 'package:tmail_ui_user/features/rules_filter_creator/presentation/rules_filter_creator_controller.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
class RulesFilterCreatorBindings extends BaseBindings {
@@ -57,6 +58,7 @@ class RulesFilterCreatorBindings extends BaseBindings {
Get.find<CacheExceptionThrower>()));
Get.lazyPut(() => StateDataSourceImpl(
Get.find<StateCacheManager>(),
Get.find<IOSSharingManager>(),
Get.find<CacheExceptionThrower>()
));
}
@@ -27,6 +27,7 @@ import 'package:tmail_ui_user/features/mailbox_creator/domain/usecases/verify_na
import 'package:tmail_ui_user/features/search/mailbox/presentation/search_mailbox_controller.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
class SearchMailboxBindings extends BaseBindings {
@@ -66,6 +67,7 @@ class SearchMailboxBindings extends BaseBindings {
));
Get.lazyPut(() => StateDataSourceImpl(
Get.find<StateCacheManager>(),
Get.find<IOSSharingManager>(),
Get.find<CacheExceptionThrower>()
));
}
@@ -22,6 +22,7 @@ import 'package:tmail_ui_user/features/thread/domain/usecases/search_more_email_
import 'package:tmail_ui_user/features/thread/presentation/thread_controller.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
class ThreadBindings extends BaseBindings {
@@ -52,6 +53,7 @@ class ThreadBindings extends BaseBindings {
Get.lazyPut(() => LocalThreadDataSourceImpl(Get.find<EmailCacheManager>(), Get.find<CacheExceptionThrower>()));
Get.lazyPut(() => StateDataSourceImpl(
Get.find<StateCacheManager>(),
Get.find<IOSSharingManager>(),
Get.find<CacheExceptionThrower>()
));
}
+13 -2
View File
@@ -11,6 +11,7 @@ import 'package:tmail_ui_user/features/login/data/local/authentication_info_cach
import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart';
import 'package:tmail_ui_user/features/mailbox/data/local/state_cache_manager.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/state_type.dart';
import 'package:tmail_ui_user/features/push_notification/data/extensions/keychain_sharing_session_extension.dart';
import 'package:tmail_ui_user/features/push_notification/data/keychain/keychain_sharing_manager.dart';
import 'package:tmail_ui_user/features/push_notification/data/keychain/keychain_sharing_session.dart';
@@ -33,7 +34,7 @@ class IOSSharingManager {
personalAccount.apiUrl != null;
}
Future<String?> _getEmailDeliveryStateFromKeychain(AccountId accountId) async {
Future<String?> getEmailDeliveryStateFromKeychain(AccountId accountId) async {
try {
if (await _keychainSharingManager.isSessionExist(accountId)) {
final keychainSharingStored = await _keychainSharingManager.getSharingSession(accountId);
@@ -122,7 +123,7 @@ class IOSSharingManager {
required UserName userName
}) async {
try {
String? emailDeliveryState = await _getEmailDeliveryStateFromKeychain(accountId);
String? emailDeliveryState = await getEmailDeliveryStateFromKeychain(accountId);
if (emailDeliveryState == null || emailDeliveryState.isEmpty) {
final emailState = await _stateCacheManager.getState(
accountId,
@@ -137,4 +138,14 @@ class IOSSharingManager {
return null;
}
}
Future updateEmailDeliveryStateInKeyChain(AccountId accountId, String newEmailDeliveryState) async {
final keychainSharingSession = await getKeychainSharingSession(accountId);
log('IOSSharingManager::updateEmailDeliveryStateInKeyChain:keychainSharingSession: $keychainSharingSession');
if (keychainSharingSession == null) {
return;
}
final newKeychain = keychainSharingSession.updating(emailDeliveryState: newEmailDeliveryState);
await _keychainSharingManager.save(newKeychain);
}
}