diff --git a/lib/features/caching/caching_manager.dart b/lib/features/caching/caching_manager.dart index 89c4b4be9..9b0f5da28 100644 --- a/lib/features/caching/caching_manager.dart +++ b/lib/features/caching/caching_manager.dart @@ -98,47 +98,37 @@ class CachingManager { } Future clearEmailAndStateCache({AccountId? accountId, UserName? userName}) { - log('CachingManager::clearEmailAndStateCache:userName = $userName'); - if (accountId != null && userName != null) { - final emailKey = TupleKey(accountId.asString, userName.value).encodeKey; - final stateKey = StateType.email.getTupleKeyStored(accountId, userName); + log('CachingManager::clearEmailAndStateCache:userName=$userName'); - return Future.wait([ - _emailCacheManager.deleteByKey(emailKey), - _stateCacheManager.deleteByKey(stateKey), - if (PlatformInfo.isMobile) - clearFCMEmailStateCache(accountId: accountId, userName: userName), - ]); - } else { - final stateKey = StateType.email.getTupleKeyStoredWithoutAccount(); - return Future.wait([ - _emailCacheManager.clear(), - _stateCacheManager.deleteByKey(stateKey), - if (PlatformInfo.isMobile) clearFCMEmailStateCache(), - ]); - } + 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'); - if (accountId != null && userName != null) { - final emailKey = TupleKey(accountId.asString, userName.value).encodeKey; - return Future.wait([ - _newEmailCacheManager.deleteByKey(emailKey), - _openedEmailCacheManager.deleteByKey(emailKey), - clearAllFileInStorage(), - ]); - } else { - return Future.wait([ - _newEmailCacheManager.clear(), - _openedEmailCacheManager.clear(), - clearAllFileInStorage(), - ]); - } + 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'); + log('CachingManager::clearAllEmailAndStateCache:userName=$userName'); + return Future.wait([ clearEmailAndStateCache(accountId: accountId, userName: userName), if (PlatformInfo.isMobile) @@ -148,7 +138,7 @@ class CachingManager { Future clearMailboxCache() { return Future.wait([ - _stateCacheManager.deleteByKey( + _stateCacheManager.deleteContainKey( StateType.mailbox.getTupleKeyStoredWithoutAccount(), ), _mailboxCacheManager.clear(), @@ -171,21 +161,71 @@ class CachingManager { await _fileUtils.removeFolder(CachingConstants.openedEmailContentFolderName); await _fileUtils.removeFolder(CachingConstants.newEmailsContentFolderName); } - - Future clearFCMEmailStateCache({AccountId? accountId, UserName? userName}) async { - if (accountId != null && userName != null) { + + Future clearFCMEmailStateCache({ + AccountId? accountId, + UserName? userName, + }) async { + final hasUserContext = accountId != null && userName != null; + + if (hasUserContext) { await _fcmCacheManager.deleteByKey( - TypeName.emailDelivery.getTupleKeyStored(accountId, userName)); + TypeName.emailDelivery.getTupleKeyStored(accountId, userName), + ); await _fcmCacheManager.deleteByKey( - TypeName.emailType.getTupleKeyStored(accountId, userName)); + TypeName.emailType.getTupleKeyStored(accountId, userName), + ); } else { - await _fcmCacheManager.deleteByKey( - TypeName.emailDelivery.getTupleKeyStoredWithoutAccount()); - await _fcmCacheManager.deleteByKey( - TypeName.emailType.getTupleKeyStoredWithoutAccount()); + 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(), diff --git a/lib/features/login/presentation/login_controller.dart b/lib/features/login/presentation/login_controller.dart index abdaf2825..f1997a39e 100644 --- a/lib/features/login/presentation/login_controller.dart +++ b/lib/features/login/presentation/login_controller.dart @@ -14,6 +14,7 @@ import 'package:get/get.dart'; import 'package:jmap_dart_client/jmap/core/session/session.dart'; import 'package:jmap_dart_client/jmap/core/user_name.dart'; import 'package:model/account/password.dart'; +import 'package:model/extensions/session_extension.dart'; import 'package:model/oidc/oidc_configuration.dart'; import 'package:model/oidc/request/oidc_request.dart'; import 'package:model/oidc/response/oidc_response.dart'; @@ -254,7 +255,10 @@ class LoginController extends ReloadableController { SmartDialog.dismiss(); if (PlatformInfo.isWeb && AppConfig.isForceEmailQueryEnabled) { - await cachingManager.clearAllEmailAndStateCache(); + await cachingManager.clearAllEmailAndStateCache( + accountId: session.safeAccountId, + userName: session.username + ); } popAndPush( diff --git a/lib/features/mailbox/data/local/state_cache_manager.dart b/lib/features/mailbox/data/local/state_cache_manager.dart index ba9b88496..1a4d29ec0 100644 --- a/lib/features/mailbox/data/local/state_cache_manager.dart +++ b/lib/features/mailbox/data/local/state_cache_manager.dart @@ -33,6 +33,9 @@ class StateCacheManager extends CacheManagerInteraction { Future deleteByKey(String key) => _stateCacheClient.deleteItem(key); + Future deleteContainKey(String key) => + _stateCacheClient.clearAllDataContainKey(key); + Future clear() => _stateCacheClient.clearAllData(); @override diff --git a/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart b/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart index 4f3658a8c..991461ee4 100644 --- a/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart +++ b/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart @@ -612,7 +612,10 @@ class MailboxDashBoardController extends ReloadableController Future onBeforeUnloadBrowserListener(html.Event event) async { log('MailboxDashBoardController::onBeforeUnloadBrowserListener:event = ${event.runtimeType} | hasComposer = ${twakeAppManager.hasComposer} | isExecutingBeforeReconnect = ${twakeAppManager.isExecutingBeforeReconnect}'); if (PlatformInfo.isWeb) { - await cachingManager.clearAllEmailAndStateCache(); + await cachingManager.clearAllEmailAndStateCache( + accountId: accountId.value, + userName: sessionCurrent?.username, + ); } if (event is html.BeforeUnloadEvent && diff --git a/lib/features/manage_account/presentation/manage_account_dashboard_controller.dart b/lib/features/manage_account/presentation/manage_account_dashboard_controller.dart index dfa531473..6201f8c06 100644 --- a/lib/features/manage_account/presentation/manage_account_dashboard_controller.dart +++ b/lib/features/manage_account/presentation/manage_account_dashboard_controller.dart @@ -140,7 +140,10 @@ class ManageAccountDashBoardController extends ReloadableController @override Future onBeforeUnloadBrowserListener(html.Event event) async { if (PlatformInfo.isWeb) { - await cachingManager.clearAllEmailAndStateCache(); + await cachingManager.clearAllEmailAndStateCache( + accountId: accountId.value, + userName: sessionCurrent?.username, + ); } } diff --git a/lib/features/offline_mode/manager/new_email_cache_manager.dart b/lib/features/offline_mode/manager/new_email_cache_manager.dart index 2690c2d91..3c52f20dc 100644 --- a/lib/features/offline_mode/manager/new_email_cache_manager.dart +++ b/lib/features/offline_mode/manager/new_email_cache_manager.dart @@ -85,7 +85,7 @@ class NewEmailCacheManager extends CacheManagerInteraction { Future clear() => _cacheClient.clearAllData(); - Future deleteByKey(String key) => + Future deleteContainKey(String key) => _cacheClient.clearAllDataContainKey(key); @override diff --git a/lib/features/offline_mode/manager/opened_email_cache_manager.dart b/lib/features/offline_mode/manager/opened_email_cache_manager.dart index 7e3da47e3..44780a885 100644 --- a/lib/features/offline_mode/manager/opened_email_cache_manager.dart +++ b/lib/features/offline_mode/manager/opened_email_cache_manager.dart @@ -89,7 +89,7 @@ class OpenedEmailCacheManager extends CacheManagerInteraction { Future clear() => _cacheClient.clearAllData(); - Future deleteByKey(String key) => + Future deleteContainKey(String key) => _cacheClient.clearAllDataContainKey(key); @override diff --git a/lib/features/push_notification/data/local/fcm_cache_manager.dart b/lib/features/push_notification/data/local/fcm_cache_manager.dart index 715d41242..59e505a51 100644 --- a/lib/features/push_notification/data/local/fcm_cache_manager.dart +++ b/lib/features/push_notification/data/local/fcm_cache_manager.dart @@ -91,6 +91,9 @@ class FCMCacheManager extends CacheManagerInteraction { Future deleteByKey(String key) => _fcmCacheClient.deleteItem(key); + Future deleteContainKey(String key) => + _fcmCacheClient.clearAllDataContainKey(key); + @override Future migrateHiveToIsolatedHive() async { await Future.wait([ diff --git a/lib/features/thread/data/local/email_cache_manager.dart b/lib/features/thread/data/local/email_cache_manager.dart index dadfc6060..3f8c69315 100644 --- a/lib/features/thread/data/local/email_cache_manager.dart +++ b/lib/features/thread/data/local/email_cache_manager.dart @@ -93,7 +93,7 @@ class EmailCacheManager extends CacheManagerInteraction { Future clear() => _emailCacheClient.clearAllData(); - Future deleteByKey(String key) => + Future deleteContainKey(String key) => _emailCacheClient.clearAllDataContainKey(key); Future storeEmail(AccountId accountId, UserName userName, EmailCache emailCache) { diff --git a/lib/features/thread/presentation/thread_controller.dart b/lib/features/thread/presentation/thread_controller.dart index 432b95366..bf2be0974 100644 --- a/lib/features/thread/presentation/thread_controller.dart +++ b/lib/features/thread/presentation/thread_controller.dart @@ -609,7 +609,10 @@ class ThreadController extends BaseController with EmailActionController { Future refreshAllEmail({bool forceEmailQuery = false}) async { if (forceEmailQuery) { - await cachingManager.clearAllEmailAndStateCache(); + await cachingManager.clearAllEmailAndStateCache( + accountId: mailboxDashBoardController.accountId.value, + userName: mailboxDashBoardController.sessionCurrent?.username, + ); } if (searchController.isSearchEmailRunning) { diff --git a/model/lib/extensions/session_extension.dart b/model/lib/extensions/session_extension.dart index 0d08cca4d..20b9f7b8d 100644 --- a/model/lib/extensions/session_extension.dart +++ b/model/lib/extensions/session_extension.dart @@ -3,6 +3,7 @@ import 'dart:ui'; import 'package:core/presentation/extensions/uri_extension.dart'; import 'package:core/utils/app_logger.dart'; +import 'package:get/get.dart'; import 'package:get/get_utils/src/extensions/string_extensions.dart'; import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/core/capability/calendar_event_capability.dart'; @@ -172,6 +173,15 @@ extension SessionExtension on Session { AccountId get accountId => personalAccount.accountId; + AccountId? get safeAccountId { + try { + return personalAccount.accountId; + } catch (e) { + logError('SessionExtension::safeAccountId:Exception: $e'); + return null; + } + } + ({ bool isAvailable, CalendarEventCapability? calendarEventCapability