import 'package:core/utils/app_logger.dart'; import 'package:core/utils/file_utils.dart'; import 'package:core/utils/platform_info.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:model/extensions/account_id_extensions.dart'; import 'package:tmail_ui_user/features/caching/clients/hive_cache_version_client.dart'; import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart'; import 'package:tmail_ui_user/features/caching/manager/session_cache_manger.dart'; import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart'; import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart'; import 'package:tmail_ui_user/features/cleanup/data/local/recent_login_url_cache_manager.dart'; import 'package:tmail_ui_user/features/cleanup/data/local/recent_login_username_cache_manager.dart'; import 'package:tmail_ui_user/features/cleanup/data/local/recent_search_cache_manager.dart'; import 'package:tmail_ui_user/features/login/data/local/account_cache_manager.dart'; import 'package:tmail_ui_user/features/login/data/local/authentication_info_cache_manager.dart'; import 'package:tmail_ui_user/features/login/data/local/encryption_key_cache_manager.dart'; import 'package:tmail_ui_user/features/login/data/local/oidc_configuration_cache_manager.dart'; import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart'; import 'package:tmail_ui_user/features/mailbox/data/local/mailbox_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/offline_mode/manager/new_email_cache_manager.dart'; import 'package:tmail_ui_user/features/offline_mode/manager/opened_email_cache_manager.dart'; import 'package:tmail_ui_user/features/offline_mode/manager/sending_email_cache_manager.dart'; import 'package:tmail_ui_user/features/push_notification/data/keychain/keychain_sharing_manager.dart'; import 'package:tmail_ui_user/features/push_notification/data/local/fcm_cache_manager.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/extensions/type_name_extension.dart'; import 'package:tmail_ui_user/features/thread/data/local/email_cache_manager.dart'; class CachingManager { final MailboxCacheManager _mailboxCacheManager; final StateCacheManager _stateCacheManager; final EmailCacheManager _emailCacheManager; final RecentSearchCacheManager _recentSearchCacheManager; final RecentLoginUrlCacheManager _recentLoginUrlCacheManager; final RecentLoginUsernameCacheManager _recentLoginUsernameCacheManager; final AccountCacheManager _accountCacheManager; final FCMCacheManager _fcmCacheManager; final HiveCacheVersionClient _hiveCacheVersionClient; final NewEmailCacheManager _newEmailCacheManager; final OpenedEmailCacheManager _openedEmailCacheManager; final FileUtils _fileUtils; final SendingEmailCacheManager _sendingEmailCacheManager; final SessionCacheManager _sessionCacheManager; final KeychainSharingManager _keychainSharingManager; final TokenOidcCacheManager _tokenOidcCacheManager; final OidcConfigurationCacheManager _oidcConfigurationCacheManager; final EncryptionKeyCacheManager _encryptionKeyCacheManager; final AuthenticationInfoCacheManager _authenticationInfoCacheManager; CachingManager( this._mailboxCacheManager, this._stateCacheManager, this._emailCacheManager, this._recentSearchCacheManager, this._recentLoginUrlCacheManager, this._recentLoginUsernameCacheManager, this._accountCacheManager, this._fcmCacheManager, this._hiveCacheVersionClient, this._newEmailCacheManager, this._openedEmailCacheManager, this._fileUtils, this._sendingEmailCacheManager, this._sessionCacheManager, this._keychainSharingManager, this._tokenOidcCacheManager, this._oidcConfigurationCacheManager, this._encryptionKeyCacheManager, this._authenticationInfoCacheManager, ); Future clearAll() async { await Future.wait([ clearMailDataCached(), _fcmCacheManager.clear(), _accountCacheManager.clear(), if (PlatformInfo.isIOS) _keychainSharingManager.delete() ], eagerError: true); } Future clearMailDataCached() async { await Future.wait([ _stateCacheManager.clear(), _mailboxCacheManager.clear(), _emailCacheManager.clear(), if (PlatformInfo.isMobile) ...[ _sessionCacheManager.clear(), _newEmailCacheManager.clear(), _openedEmailCacheManager.clear(), _sendingEmailCacheManager.clearAllSendingEmails(), ], ], eagerError: true); } Future clearEmailAndStateCache({AccountId? accountId, UserName? userName}) { log('CachingManager::clearEmailAndStateCache:userName=$userName'); final hasUserContext = accountId != null && userName != null; return Future.wait([ if (hasUserContext) ..._clearEmailAndStateWithUser(accountId, userName) else ..._clearEmailAndStateWithoutUser(), if (PlatformInfo.isMobile) clearFCMEmailStateCache(accountId: accountId, userName: userName), ]); } Future clearDetailedEmailCache({AccountId? accountId, UserName? userName}) { log('CachingManager::clearDetailedEmailCache:userName=$userName'); final hasUserContext = accountId != null && userName != null; return Future.wait([ if (hasUserContext) ..._clearDetailedEmailWithUser(accountId, userName) else ..._clearDetailedEmailWithoutUser(), ]); } Future clearAllEmailAndStateCache({AccountId? accountId, UserName? userName}) { log('CachingManager::clearAllEmailAndStateCache:userName=$userName'); return Future.wait([ clearEmailAndStateCache(accountId: accountId, userName: userName), if (PlatformInfo.isMobile) clearDetailedEmailCache(accountId: accountId, userName: userName), ]); } Future clearMailboxCache() { return Future.wait([ _stateCacheManager.deleteContainKey( StateType.mailbox.getTupleKeyStoredWithoutAccount(), ), _mailboxCacheManager.clear(), ], eagerError: true); } Future storeCacheVersion(int newVersion) async { log('CachingManager::storeCacheVersion():newVersion = $newVersion'); return _hiveCacheVersionClient.storeVersion(newVersion); } Future getLatestVersion() { return _hiveCacheVersionClient.getLatestVersion(); } Future closeHive({bool isolated = true}) => HiveCacheConfig.instance.closeHive(isolated: isolated); Future clearAllFileInStorage() async { await _fileUtils.removeFolder(CachingConstants.openedEmailContentFolderName); await _fileUtils.removeFolder(CachingConstants.newEmailsContentFolderName); } Future clearFCMEmailStateCache({ AccountId? accountId, UserName? userName, }) async { final hasUserContext = accountId != null && userName != null; if (hasUserContext) { await _fcmCacheManager.deleteByKey( TypeName.emailDelivery.getTupleKeyStored(accountId, userName), ); await _fcmCacheManager.deleteByKey( TypeName.emailType.getTupleKeyStored(accountId, userName), ); } else { await _fcmCacheManager.deleteContainKey( TypeName.emailDelivery.getTupleKeyStoredWithoutAccount(), ); await _fcmCacheManager.deleteContainKey( TypeName.emailType.getTupleKeyStoredWithoutAccount(), ); } } List> _clearEmailAndStateWithUser( AccountId accountId, UserName userName, ) { final emailKey = TupleKey(accountId.asString, userName.value).encodeKey; final stateKey = StateType.email.getTupleKeyStored(accountId, userName); return [ _emailCacheManager.deleteContainKey(emailKey), _stateCacheManager.deleteByKey(stateKey), ]; } List> _clearEmailAndStateWithoutUser() { final stateKey = StateType.email.getTupleKeyStoredWithoutAccount(); return [ _emailCacheManager.clear(), _stateCacheManager.deleteContainKey(stateKey), ]; } List> _clearDetailedEmailWithUser( AccountId accountId, UserName userName, ) { final emailKey = TupleKey(accountId.asString, userName.value).encodeKey; return [ _newEmailCacheManager.deleteContainKey(emailKey), _openedEmailCacheManager.deleteContainKey(emailKey), clearAllFileInStorage(), ]; } List> _clearDetailedEmailWithoutUser() { return [ _newEmailCacheManager.clear(), _openedEmailCacheManager.clear(), clearAllFileInStorage(), ]; } Future clearLoginRecentData() async { await Future.wait([ _recentLoginUrlCacheManager.clear(), _recentLoginUsernameCacheManager.clear(), ]); } Future clearRecentSearchData() async { await _recentSearchCacheManager.clear(); } Future migrateHiveToIsolatedHive() async { await Future.wait([ _tokenOidcCacheManager.migrateHiveToIsolatedHive(), _accountCacheManager.migrateHiveToIsolatedHive(), _fcmCacheManager.migrateHiveToIsolatedHive(), _oidcConfigurationCacheManager.migrateHiveToIsolatedHive(), _encryptionKeyCacheManager.migrateHiveToIsolatedHive(), _authenticationInfoCacheManager.migrateHiveToIsolatedHive(), _recentLoginUrlCacheManager.migrateHiveToIsolatedHive(), _recentLoginUsernameCacheManager.migrateHiveToIsolatedHive(), _recentSearchCacheManager.migrateHiveToIsolatedHive(), if (PlatformInfo.isMobile) ...[ _sessionCacheManager.migrateHiveToIsolatedHive(), _sendingEmailCacheManager.migrateHiveToIsolatedHive(), ] ]); } }