Migrate Hive box To IsolatedHive box
Signed-off-by: dab246 <tdvu@linagora.com>
This commit is contained in:
@@ -5,81 +5,89 @@ 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/account_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/email_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/fcm_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/firebase_registration_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/hive_cache_version_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/mailbox_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/new_email_hive_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/opened_email_hive_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/recent_login_url_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/recent_login_username_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/recent_search_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/session_hive_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/state_cache_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/mailbox_dashboard/data/local/local_spam_report_manager.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 MailboxCacheClient _mailboxCacheClient;
|
||||
final StateCacheClient _stateCacheClient;
|
||||
final EmailCacheClient _emailCacheClient;
|
||||
final RecentSearchCacheClient _recentSearchCacheClient;
|
||||
final RecentLoginUrlCacheClient _recentLoginUrlCacheClient;
|
||||
final RecentLoginUsernameCacheClient _recentLoginUsernameCacheClient;
|
||||
final AccountCacheClient _accountCacheClient;
|
||||
final FcmCacheClient _fcmCacheClient;
|
||||
final FirebaseRegistrationCacheClient _firebaseRegistrationCacheClient;
|
||||
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 NewEmailHiveCacheClient _newEmailHiveCacheClient;
|
||||
final OpenedEmailHiveCacheClient _openedEmailHiveCacheClient;
|
||||
final NewEmailCacheManager _newEmailCacheManager;
|
||||
final OpenedEmailCacheManager _openedEmailCacheManager;
|
||||
final FileUtils _fileUtils;
|
||||
final SendingEmailCacheManager _sendingEmailCacheManager;
|
||||
final SessionHiveCacheClient _sessionHiveCacheClient;
|
||||
final SessionCacheManager _sessionCacheManager;
|
||||
final LocalSpamReportManager _localSpamReportManager;
|
||||
final KeychainSharingManager _keychainSharingManager;
|
||||
final TokenOidcCacheManager _tokenOidcCacheManager;
|
||||
final OidcConfigurationCacheManager _oidcConfigurationCacheManager;
|
||||
final EncryptionKeyCacheManager _encryptionKeyCacheManager;
|
||||
final AuthenticationInfoCacheManager _authenticationInfoCacheManager;
|
||||
|
||||
CachingManager(
|
||||
this._mailboxCacheClient,
|
||||
this._stateCacheClient,
|
||||
this._emailCacheClient,
|
||||
this._recentSearchCacheClient,
|
||||
this._recentLoginUrlCacheClient,
|
||||
this._recentLoginUsernameCacheClient,
|
||||
this._accountCacheClient,
|
||||
this._fcmCacheClient,
|
||||
this._firebaseRegistrationCacheClient,
|
||||
this._mailboxCacheManager,
|
||||
this._stateCacheManager,
|
||||
this._emailCacheManager,
|
||||
this._recentSearchCacheManager,
|
||||
this._recentLoginUrlCacheManager,
|
||||
this._recentLoginUsernameCacheManager,
|
||||
this._accountCacheManager,
|
||||
this._fcmCacheManager,
|
||||
this._hiveCacheVersionClient,
|
||||
this._newEmailHiveCacheClient,
|
||||
this._openedEmailHiveCacheClient,
|
||||
this._newEmailCacheManager,
|
||||
this._openedEmailCacheManager,
|
||||
this._fileUtils,
|
||||
this._sendingEmailCacheManager,
|
||||
this._sessionHiveCacheClient,
|
||||
this._sessionCacheManager,
|
||||
this._localSpamReportManager,
|
||||
this._keychainSharingManager,
|
||||
this._tokenOidcCacheManager,
|
||||
this._oidcConfigurationCacheManager,
|
||||
this._encryptionKeyCacheManager,
|
||||
this._authenticationInfoCacheManager,
|
||||
);
|
||||
|
||||
Future<void> clearAll() async {
|
||||
await Future.wait([
|
||||
_stateCacheClient.clearAllData(),
|
||||
_mailboxCacheClient.clearAllData(),
|
||||
_emailCacheClient.clearAllData(),
|
||||
_fcmCacheClient.clearAllData(),
|
||||
_firebaseRegistrationCacheClient.clearAllData(),
|
||||
_accountCacheClient.clearAllData(),
|
||||
_stateCacheManager.clear(),
|
||||
_mailboxCacheManager.clear(),
|
||||
_emailCacheManager.clear(),
|
||||
_fcmCacheManager.clear(),
|
||||
_accountCacheManager.clear(),
|
||||
_localSpamReportManager.clear(),
|
||||
if (PlatformInfo.isMobile)
|
||||
...[
|
||||
_sessionHiveCacheClient.clearAllData(),
|
||||
_newEmailHiveCacheClient.clearAllData(),
|
||||
_openedEmailHiveCacheClient.clearAllData(),
|
||||
_sessionCacheManager.clear(),
|
||||
_newEmailCacheManager.clear(),
|
||||
_openedEmailCacheManager.clear(),
|
||||
_sendingEmailCacheManager.clearAllSendingEmails(),
|
||||
],
|
||||
if (PlatformInfo.isIOS)
|
||||
@@ -94,16 +102,16 @@ class CachingManager {
|
||||
final stateKey = StateType.email.getTupleKeyStored(accountId, userName);
|
||||
|
||||
return Future.wait([
|
||||
_emailCacheClient.clearAllDataContainKey(emailKey),
|
||||
_stateCacheClient.deleteItem(stateKey),
|
||||
_emailCacheManager.deleteByKey(emailKey),
|
||||
_stateCacheManager.deleteByKey(stateKey),
|
||||
if (PlatformInfo.isMobile)
|
||||
clearFCMEmailStateCache(accountId: accountId, userName: userName),
|
||||
]);
|
||||
} else {
|
||||
final stateKey = StateType.email.getTupleKeyStoredWithoutAccount();
|
||||
return Future.wait([
|
||||
_emailCacheClient.clearAllData(),
|
||||
_stateCacheClient.deleteItem(stateKey),
|
||||
_emailCacheManager.clear(),
|
||||
_stateCacheManager.deleteByKey(stateKey),
|
||||
if (PlatformInfo.isMobile) clearFCMEmailStateCache(),
|
||||
]);
|
||||
}
|
||||
@@ -114,14 +122,14 @@ class CachingManager {
|
||||
if (accountId != null && userName != null) {
|
||||
final emailKey = TupleKey(accountId.asString, userName.value).encodeKey;
|
||||
return Future.wait([
|
||||
_newEmailHiveCacheClient.clearAllDataContainKey(emailKey),
|
||||
_openedEmailHiveCacheClient.clearAllDataContainKey(emailKey),
|
||||
_newEmailCacheManager.deleteByKey(emailKey),
|
||||
_openedEmailCacheManager.deleteByKey(emailKey),
|
||||
clearAllFileInStorage(),
|
||||
]);
|
||||
} else {
|
||||
return Future.wait([
|
||||
_newEmailHiveCacheClient.clearAllData(),
|
||||
_openedEmailHiveCacheClient.clearAllData(),
|
||||
_newEmailCacheManager.clear(),
|
||||
_openedEmailCacheManager.clear(),
|
||||
clearAllFileInStorage(),
|
||||
]);
|
||||
}
|
||||
@@ -138,8 +146,10 @@ class CachingManager {
|
||||
|
||||
Future<void> clearMailboxCache() {
|
||||
return Future.wait([
|
||||
_stateCacheClient.deleteItem(StateType.mailbox.getTupleKeyStoredWithoutAccount()),
|
||||
_mailboxCacheClient.clearAllData(),
|
||||
_stateCacheManager.deleteByKey(
|
||||
StateType.mailbox.getTupleKeyStoredWithoutAccount(),
|
||||
),
|
||||
_mailboxCacheManager.clear(),
|
||||
], eagerError: true);
|
||||
}
|
||||
|
||||
@@ -152,9 +162,8 @@ class CachingManager {
|
||||
return _hiveCacheVersionClient.getLatestVersion();
|
||||
}
|
||||
|
||||
Future<void> closeHive() async {
|
||||
return await HiveCacheConfig.instance.closeHive();
|
||||
}
|
||||
Future<void> closeHive({bool isolated = true}) =>
|
||||
HiveCacheConfig.instance.closeHive(isolated: isolated);
|
||||
|
||||
Future<void> clearAllFileInStorage() async {
|
||||
await _fileUtils.removeFolder(CachingConstants.openedEmailContentFolderName);
|
||||
@@ -163,26 +172,50 @@ class CachingManager {
|
||||
|
||||
Future<void> clearFCMEmailStateCache({AccountId? accountId, UserName? userName}) async {
|
||||
if (accountId != null && userName != null) {
|
||||
await _fcmCacheClient.deleteItem(
|
||||
await _fcmCacheManager.deleteByKey(
|
||||
TypeName.emailDelivery.getTupleKeyStored(accountId, userName));
|
||||
await _fcmCacheClient.deleteItem(
|
||||
await _fcmCacheManager.deleteByKey(
|
||||
TypeName.emailType.getTupleKeyStored(accountId, userName));
|
||||
} else {
|
||||
await _fcmCacheClient.deleteItem(
|
||||
await _fcmCacheManager.deleteByKey(
|
||||
TypeName.emailDelivery.getTupleKeyStoredWithoutAccount());
|
||||
await _fcmCacheClient.deleteItem(
|
||||
await _fcmCacheManager.deleteByKey(
|
||||
TypeName.emailType.getTupleKeyStoredWithoutAccount());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> clearLoginRecentData() async {
|
||||
await Future.wait([
|
||||
_recentLoginUrlCacheClient.clearAllData(),
|
||||
_recentLoginUsernameCacheClient.clearAllData(),
|
||||
_recentLoginUrlCacheManager.clear(),
|
||||
_recentLoginUsernameCacheManager.clear(),
|
||||
]);
|
||||
}
|
||||
|
||||
Future<void> clearRecentSearchData() async {
|
||||
await _recentSearchCacheClient.clearAllData();
|
||||
await _recentSearchCacheManager.clear();
|
||||
}
|
||||
|
||||
Future<void> migrateHiveToIsolatedHive() async {
|
||||
await Future.wait([
|
||||
_tokenOidcCacheManager.migrateHiveToIsolatedHive(),
|
||||
_accountCacheManager.migrateHiveToIsolatedHive(),
|
||||
_stateCacheManager.migrateHiveToIsolatedHive(),
|
||||
_mailboxCacheManager.migrateHiveToIsolatedHive(),
|
||||
_emailCacheManager.migrateHiveToIsolatedHive(),
|
||||
_fcmCacheManager.migrateHiveToIsolatedHive(),
|
||||
_oidcConfigurationCacheManager.migrateHiveToIsolatedHive(),
|
||||
_encryptionKeyCacheManager.migrateHiveToIsolatedHive(),
|
||||
_authenticationInfoCacheManager.migrateHiveToIsolatedHive(),
|
||||
_recentLoginUrlCacheManager.migrateHiveToIsolatedHive(),
|
||||
_recentLoginUsernameCacheManager.migrateHiveToIsolatedHive(),
|
||||
_recentSearchCacheManager.migrateHiveToIsolatedHive(),
|
||||
if (PlatformInfo.isMobile)
|
||||
...[
|
||||
_sessionCacheManager.migrateHiveToIsolatedHive(),
|
||||
_sendingEmailCacheManager.migrateHiveToIsolatedHive(),
|
||||
_newEmailCacheManager.migrateHiveToIsolatedHive(),
|
||||
_openedEmailCacheManager.migrateHiveToIsolatedHive(),
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,84 +14,170 @@ abstract class HiveCacheClient<T> {
|
||||
|
||||
Future<Uint8List?> _getEncryptionKey() => HiveCacheConfig.instance.getEncryptionKey();
|
||||
|
||||
Future<IsolatedBox<T>> openBox() async {
|
||||
Future<IsolatedBox<T>> openIsolatedBox() async {
|
||||
if (IsolatedHive.isBoxOpen(tableName)) {
|
||||
return IsolatedHive.box<T>(tableName);
|
||||
} else {
|
||||
return IsolatedHive.openBox<T>(tableName);
|
||||
}
|
||||
}
|
||||
|
||||
Future<IsolatedBox<T>> openBoxEncryption() async {
|
||||
final encryptionKey = await _getEncryptionKey();
|
||||
if (IsolatedHive.isBoxOpen(tableName)) {
|
||||
return IsolatedHive.box<T>(tableName);
|
||||
} else {
|
||||
return IsolatedHive.openBox<T>(tableName,
|
||||
if (encryption) {
|
||||
final encryptionKey = await _getEncryptionKey();
|
||||
return IsolatedHive.openBox<T>(
|
||||
tableName,
|
||||
encryptionCipher:
|
||||
encryptionKey != null ? HiveAesCipher(encryptionKey) : null);
|
||||
encryptionKey != null ? HiveAesCipher(encryptionKey) : null,
|
||||
);
|
||||
} else {
|
||||
return IsolatedHive.openBox<T>(tableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> insertItem(String key, T newObject) {
|
||||
log('$runtimeType::insertItem:encryption: $encryption - key = $key');
|
||||
Future<Box<T>> openBox() async {
|
||||
if (Hive.isBoxOpen(tableName)) {
|
||||
return Hive.box<T>(tableName);
|
||||
} else {
|
||||
if (encryption) {
|
||||
final encryptionKey = await _getEncryptionKey();
|
||||
return Hive.openBox<T>(
|
||||
tableName,
|
||||
encryptionCipher:
|
||||
encryptionKey != null ? HiveAesCipher(encryptionKey) : null,
|
||||
);
|
||||
} else {
|
||||
return Hive.openBox<T>(tableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> insertItem(
|
||||
String key,
|
||||
T newObject, {
|
||||
bool isolated = true,
|
||||
}) {
|
||||
log('$runtimeType::insertItem:encryption: $encryption - key = $key - isolated');
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
return boxItem.put(key, newObject);
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
return boxItem.put(key, newObject);
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
return boxItem.put(key, newObject);
|
||||
}
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> insertMultipleItem(Map<String, T> mapObject) {
|
||||
Future<void> insertMultipleItem(
|
||||
Map<String, T> mapObject, {
|
||||
bool isolated = true,
|
||||
}) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
return boxItem.putAll(mapObject);
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
return boxItem.putAll(mapObject);
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
return boxItem.putAll(mapObject);
|
||||
}
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<T?> getItem(String key) {
|
||||
Future<T?> getItem(
|
||||
String key, {
|
||||
bool isolated = true,
|
||||
}) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.get(key);
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
return boxItem.get(key);
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
return boxItem.get(key);
|
||||
}
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<T>> getAll() {
|
||||
Future<List<T>> getAll({bool isolated = true}) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
final items = await boxItem.values;
|
||||
Iterable<T> items;
|
||||
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
items = await boxItem.values;
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
items = boxItem.values;
|
||||
}
|
||||
log('$runtimeType::getAll: Length of items is ${items.length}');
|
||||
return items.toList();
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<T>> getListByNestedKey(String nestedKey) {
|
||||
Future<Map<String, T>> getMapItems({bool isolated = true}) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
final mapItems = await boxItem.toMap();
|
||||
late Map<dynamic, T> mapItems;
|
||||
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
mapItems = await boxItem.toMap();
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
mapItems = boxItem.toMap();
|
||||
}
|
||||
log('$runtimeType::getMapItems: Length of mapItems is ${mapItems.length}');
|
||||
return mapItems.map((key, value) => MapEntry(key.toString(), value));
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<T>> getListByNestedKey(
|
||||
String nestedKey, {
|
||||
bool isolated = true,
|
||||
}) {
|
||||
return Future.sync(() async {
|
||||
late Map<dynamic, T> mapItems;
|
||||
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
mapItems = await boxItem.toMap();
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
mapItems = boxItem.toMap();
|
||||
}
|
||||
|
||||
final listItem = mapItems
|
||||
.where((key, value) => _matchedNestedKey(key, nestedKey))
|
||||
.values
|
||||
.toList();
|
||||
log('$runtimeType::getListByNestedKey:listItem: ${listItem.length}');
|
||||
log('$runtimeType::getListByNestedKey: Length of listItem is ${listItem.length}');
|
||||
return listItem;
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<T>> getValuesByListKey(List<String> listKeys) {
|
||||
Future<List<T>> getValuesByListKey(
|
||||
List<String> listKeys, {
|
||||
bool isolated = true,
|
||||
}) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
final mapItems = await boxItem.toMap();
|
||||
late Map<dynamic, T> mapItems;
|
||||
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
mapItems = await boxItem.toMap();
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
mapItems = boxItem.toMap();
|
||||
}
|
||||
|
||||
return mapItems
|
||||
.where((key, value) => listKeys.contains(key))
|
||||
.values
|
||||
@@ -107,88 +193,149 @@ abstract class HiveCacheClient<T> {
|
||||
return decodedKey.contains(decodedNestedKey);
|
||||
}
|
||||
|
||||
Future<void> updateItem(String key, T newObject) {
|
||||
Future<void> updateItem(
|
||||
String key,
|
||||
T newObject, {
|
||||
bool isolated = true,
|
||||
}) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
return boxItem.put(key, newObject);
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
return boxItem.put(key, newObject);
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
return boxItem.put(key, newObject);
|
||||
}
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> updateMultipleItem(Map<String, T> mapObject) {
|
||||
Future<void> updateMultipleItem(
|
||||
Map<String, T> mapObject, {
|
||||
bool isolated = true,
|
||||
}) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
return boxItem.putAll(mapObject);
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
return boxItem.putAll(mapObject);
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
return boxItem.putAll(mapObject);
|
||||
}
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> deleteItem(String key) {
|
||||
Future<void> deleteItem(
|
||||
String key, {
|
||||
bool isolated = true,
|
||||
}) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
return boxItem.delete(key);
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
return boxItem.delete(key);
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
return boxItem.delete(key);
|
||||
}
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> deleteMultipleItem(List<String> listKey) {
|
||||
Future<void> deleteMultipleItem(
|
||||
List<String> listKey, {
|
||||
bool isolated = true,
|
||||
}) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.deleteAll(listKey);
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
return boxItem.deleteAll(listKey);
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
return boxItem.deleteAll(listKey);
|
||||
}
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<bool> isExistItem(String key) {
|
||||
Future<bool> isExistItem(
|
||||
String key, {
|
||||
bool isolated = true,
|
||||
}) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.containsKey(key);
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
return boxItem.containsKey(key);
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
return boxItem.containsKey(key);
|
||||
}
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> deleteBox() {
|
||||
return IsolatedHive.deleteBoxFromDisk(tableName);
|
||||
}
|
||||
|
||||
Future<void> clearAllData() {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.clear();
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> clearAllDataContainKey(String nestedKey) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
final mapItems = await boxItem.toMap();
|
||||
final listKeys = mapItems
|
||||
.where((key, value) => _matchedNestedKey(key, nestedKey))
|
||||
.keys;
|
||||
log('$runtimeType::clearAllDataContainKey:listKeys: ${listKeys.length}');
|
||||
return boxItem.deleteAll(listKeys);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> closeBox() async {
|
||||
if (IsolatedHive.isBoxOpen(tableName)) {
|
||||
await IsolatedHive.box<T>(tableName).close();
|
||||
Future<void> deleteBox({bool isolated = true}) {
|
||||
if (isolated) {
|
||||
return IsolatedHive.deleteBoxFromDisk(tableName);
|
||||
} else {
|
||||
return Hive.deleteBoxFromDisk(tableName);
|
||||
}
|
||||
return Future.value();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> clearAllData({bool isolated = true}) {
|
||||
return Future.sync(() async {
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
return boxItem.clear();
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
return boxItem.clear();
|
||||
}
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> clearAllDataContainKey(
|
||||
String nestedKey, {
|
||||
bool isolated = true,
|
||||
}) {
|
||||
return Future.sync(() async {
|
||||
if (isolated) {
|
||||
final boxItem = await openIsolatedBox();
|
||||
final mapItems = await boxItem.toMap();
|
||||
final listKeys = mapItems
|
||||
.where((key, value) => _matchedNestedKey(key, nestedKey))
|
||||
.keys;
|
||||
log('$runtimeType::clearAllDataContainKey: Length of keys is ${listKeys.length}');
|
||||
return boxItem.deleteAll(listKeys);
|
||||
} else {
|
||||
final boxItem = await openBox();
|
||||
final listKeys = boxItem.toMap()
|
||||
.where((key, value) => _matchedNestedKey(key, nestedKey))
|
||||
.keys;
|
||||
log('$runtimeType::clearAllDataContainKey: Length of keys is ${listKeys.length}');
|
||||
return boxItem.deleteAll(listKeys);
|
||||
}
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> closeBox({bool isolated = true}) async {
|
||||
if (isolated) {
|
||||
if (IsolatedHive.isBoxOpen(tableName)) {
|
||||
await IsolatedHive.box<T>(tableName).close();
|
||||
}
|
||||
} else {
|
||||
if (Hive.isBoxOpen(tableName)) {
|
||||
await Hive.box<T>(tableName).close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,12 +51,15 @@ class HiveCacheConfig {
|
||||
|
||||
static HiveCacheConfig get instance => _instance;
|
||||
|
||||
Future<void> setUp({String? cachePath}) async {
|
||||
await initializeDatabase(databasePath: cachePath);
|
||||
_registerAdapter();
|
||||
Future<void> setUp({String? cachePath, bool isolated = true}) async {
|
||||
await initializeDatabase(databasePath: cachePath, isolated: isolated);
|
||||
_registerAdapter(isolated: isolated);
|
||||
}
|
||||
|
||||
Future<void> initializeDatabase({String? databasePath}) async {
|
||||
Future<void> initializeDatabase({
|
||||
String? databasePath,
|
||||
bool isolated = true,
|
||||
}) async {
|
||||
if (databasePath == null && PlatformInfo.isMobile) {
|
||||
Directory directory = await path_provider.getApplicationDocumentsDirectory();
|
||||
databasePath = directory.path;
|
||||
@@ -64,10 +67,14 @@ class HiveCacheConfig {
|
||||
|
||||
if (databasePath == null) return;
|
||||
|
||||
await IsolatedHive.init(
|
||||
databasePath,
|
||||
isolateNameServer: const FcmIsolateNameServer(),
|
||||
);
|
||||
if (isolated) {
|
||||
await IsolatedHive.init(
|
||||
databasePath,
|
||||
isolateNameServer: const FcmIsolateNameServer(),
|
||||
);
|
||||
} else {
|
||||
Hive.init(databasePath);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> onUpgradeDatabase(CachingManager cachingManager) async {
|
||||
@@ -123,96 +130,130 @@ class HiveCacheConfig {
|
||||
}
|
||||
}
|
||||
|
||||
void _registerAdapter() {
|
||||
void _registerAdapter({bool isolated = true}) {
|
||||
registerCacheAdapter<MailboxCache>(
|
||||
MailboxCacheAdapter(),
|
||||
CachingConstants.MAILBOX_CACHE_IDENTIFY
|
||||
CachingConstants.MAILBOX_CACHE_IDENTIFY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<MailboxRightsCache>(
|
||||
MailboxRightsCacheAdapter(),
|
||||
CachingConstants.MAILBOX_RIGHTS_CACHE_IDENTIFY
|
||||
CachingConstants.MAILBOX_RIGHTS_CACHE_IDENTIFY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<StateCache>(
|
||||
StateCacheAdapter(),
|
||||
CachingConstants.STATE_CACHE_IDENTIFY
|
||||
CachingConstants.STATE_CACHE_IDENTIFY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<StateType>(
|
||||
StateTypeAdapter(),
|
||||
CachingConstants.STATE_TYPE_IDENTIFY
|
||||
CachingConstants.STATE_TYPE_IDENTIFY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<EmailAddressHiveCache>(
|
||||
EmailAddressHiveCacheAdapter(),
|
||||
CachingConstants.EMAIL_ADDRESS_HIVE_CACHE_IDENTIFY
|
||||
CachingConstants.EMAIL_ADDRESS_HIVE_CACHE_IDENTIFY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<EmailCache>(
|
||||
EmailCacheAdapter(),
|
||||
CachingConstants.EMAIL_CACHE_IDENTIFY
|
||||
CachingConstants.EMAIL_CACHE_IDENTIFY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<RecentSearchCache>(
|
||||
RecentSearchCacheAdapter(),
|
||||
CachingConstants.RECENT_SEARCH_HIVE_CACHE_IDENTIFY
|
||||
CachingConstants.RECENT_SEARCH_HIVE_CACHE_IDENTIFY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<TokenOidcCache>(
|
||||
TokenOidcCacheAdapter(),
|
||||
CachingConstants.TOKEN_OIDC_HIVE_CACHE_IDENTIFY
|
||||
CachingConstants.TOKEN_OIDC_HIVE_CACHE_IDENTIFY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<AccountCache>(
|
||||
AccountCacheAdapter(),
|
||||
CachingConstants.ACCOUNT_HIVE_CACHE_IDENTIFY
|
||||
CachingConstants.ACCOUNT_HIVE_CACHE_IDENTIFY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<EncryptionKeyCache>(
|
||||
EncryptionKeyCacheAdapter(),
|
||||
CachingConstants.ENCRYPTION_KEY_HIVE_CACHE_IDENTIFY
|
||||
CachingConstants.ENCRYPTION_KEY_HIVE_CACHE_IDENTIFY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<AuthenticationInfoCache>(
|
||||
AuthenticationInfoCacheAdapter(),
|
||||
CachingConstants.AUTHENTICATION_INFO_HIVE_CACHE_IDENTIFY
|
||||
CachingConstants.AUTHENTICATION_INFO_HIVE_CACHE_IDENTIFY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<RecentLoginUrlCache>(
|
||||
RecentLoginUrlCacheAdapter(),
|
||||
CachingConstants.RECENT_LOGIN_URL_HIVE_CACHE_IDENTITY
|
||||
CachingConstants.RECENT_LOGIN_URL_HIVE_CACHE_IDENTITY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<RecentLoginUsernameCache>(
|
||||
RecentLoginUsernameCacheAdapter(),
|
||||
CachingConstants.RECENT_LOGIN_USERNAME_HIVE_CACHE_IDENTITY
|
||||
CachingConstants.RECENT_LOGIN_USERNAME_HIVE_CACHE_IDENTITY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<FirebaseRegistrationCache>(
|
||||
FirebaseRegistrationCacheAdapter(),
|
||||
CachingConstants.FIREBASE_REGISTRATION_HIVE_CACHE_IDENTITY
|
||||
CachingConstants.FIREBASE_REGISTRATION_HIVE_CACHE_IDENTITY,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<AttachmentHiveCache>(
|
||||
AttachmentHiveCacheAdapter(),
|
||||
CachingConstants.ATTACHMENT_HIVE_CACHE_ID
|
||||
CachingConstants.ATTACHMENT_HIVE_CACHE_ID,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<EmailHeaderHiveCache>(
|
||||
EmailHeaderHiveCacheAdapter(),
|
||||
CachingConstants.EMAIL_HEADER_HIVE_CACHE_ID
|
||||
CachingConstants.EMAIL_HEADER_HIVE_CACHE_ID,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<DetailedEmailHiveCache>(
|
||||
DetailedEmailHiveCacheAdapter(),
|
||||
CachingConstants.DETAILED_EMAIL_HIVE_CACHE_ID
|
||||
CachingConstants.DETAILED_EMAIL_HIVE_CACHE_ID,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<SendingEmailHiveCache>(
|
||||
SendingEmailHiveCacheAdapter(),
|
||||
CachingConstants.SENDING_EMAIL_HIVE_CACHE_ID
|
||||
CachingConstants.SENDING_EMAIL_HIVE_CACHE_ID,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<SessionHiveObj>(
|
||||
SessionHiveObjAdapter(),
|
||||
CachingConstants.SESSION_HIVE_CACHE_ID
|
||||
CachingConstants.SESSION_HIVE_CACHE_ID,
|
||||
isolated: isolated,
|
||||
);
|
||||
registerCacheAdapter<OidcConfigurationCache>(
|
||||
OidcConfigurationCacheAdapter(),
|
||||
CachingConstants.OIDC_CONFIGURATION_CACHE_ID,
|
||||
isolated: isolated,
|
||||
);
|
||||
}
|
||||
|
||||
void registerCacheAdapter<T>(TypeAdapter<T> typeAdapter, int typeId) {
|
||||
if (!IsolatedHive.isAdapterRegistered(typeId)) {
|
||||
IsolatedHive.registerAdapter<T>(typeAdapter);
|
||||
void registerCacheAdapter<T>(
|
||||
TypeAdapter<T> typeAdapter,
|
||||
int typeId, {
|
||||
bool isolated = true,
|
||||
}) {
|
||||
if (isolated) {
|
||||
if (!IsolatedHive.isAdapterRegistered(typeId)) {
|
||||
IsolatedHive.registerAdapter<T>(typeAdapter);
|
||||
}
|
||||
} else {
|
||||
if (!Hive.isAdapterRegistered(typeId)) {
|
||||
Hive.registerAdapter<T>(typeAdapter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> closeHive() async {
|
||||
return await IsolatedHive.close();
|
||||
Future<void> closeHive({bool isolated = true}) async {
|
||||
if (isolated) {
|
||||
await IsolatedHive.close();
|
||||
} else {
|
||||
await Hive.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
abstract class CacheManagerInteraction {
|
||||
Future<void> migrateHiveToIsolatedHive();
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/session_hive_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/caching/interaction/cache_manager_interaction.dart';
|
||||
import 'package:tmail_ui_user/features/home/data/exceptions/session_exceptions.dart';
|
||||
import 'package:tmail_ui_user/features/home/data/model/session_hive_obj.dart';
|
||||
|
||||
class SessionCacheManager extends CacheManagerInteraction {
|
||||
final SessionHiveCacheClient _cacheClient;
|
||||
|
||||
SessionCacheManager(this._cacheClient);
|
||||
|
||||
Future<void> insertSession(SessionHiveObj sessionHiveObj) =>
|
||||
_cacheClient.insertItem(SessionHiveObj.keyValue, sessionHiveObj);
|
||||
|
||||
Future<SessionHiveObj> getStoredSession() async {
|
||||
final sessionHiveObj = await _cacheClient.getItem(SessionHiveObj.keyValue);
|
||||
if (sessionHiveObj != null) {
|
||||
return sessionHiveObj;
|
||||
} else {
|
||||
throw NotFoundSessionException();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> clear() => _cacheClient.clearAllData();
|
||||
|
||||
@override
|
||||
Future<void> migrateHiveToIsolatedHive() async {
|
||||
try {
|
||||
final legacyMapItems = await _cacheClient.getMapItems(
|
||||
isolated: false,
|
||||
);
|
||||
log('$runtimeType::migrateHiveToIsolatedHive(): Length of legacyMapItems: ${legacyMapItems.length}');
|
||||
await _cacheClient.insertMultipleItem(legacyMapItems);
|
||||
log('$runtimeType::migrateHiveToIsolatedHive(): ✅ Migrate Hive box "${_cacheClient.tableName}" → IsolatedHive DONE');
|
||||
} catch (e) {
|
||||
logError('$runtimeType::migrateHiveToIsolatedHive(): ❌ Migrate Hive box "${_cacheClient.tableName}" → IsolatedHive FAILED, Error: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user