TF-1217 Register cache adapter

This commit is contained in:
dab246
2022-11-23 15:07:35 +07:00
committed by Dat H. Pham
parent 5ed8e85c66
commit ad8ef6b608
3 changed files with 80 additions and 22 deletions
+15 -7
View File
@@ -13,7 +13,7 @@ class CachingManager {
final EmailCacheClient _emailCacheClient;
final RecentSearchCacheClient _recentSearchCacheClient;
final AccountCacheClient _accountCacheClient;
final FcmTokenCacheClient _firebaseCacheClient;
final FcmTokenCacheClient _fcmTokenCacheClient;
CachingManager(
this._mailboxCacheClient,
@@ -21,7 +21,7 @@ class CachingManager {
this._emailCacheClient,
this._recentSearchCacheClient,
this._accountCacheClient,
this._firebaseCacheClient,
this._fcmTokenCacheClient,
);
Future<void> clearAll() async {
@@ -32,6 +32,7 @@ class CachingManager {
_emailCacheClient.clearAllData(),
_recentSearchCacheClient.clearAllData(),
_accountCacheClient.clearAllData(),
_fcmTokenCacheClient.clearAllData(),
]);
} else {
await Future.wait([
@@ -40,15 +41,22 @@ class CachingManager {
_emailCacheClient.deleteBox(),
_recentSearchCacheClient.deleteBox(),
_accountCacheClient.deleteBox(),
_firebaseCacheClient.deleteBox(),
_fcmTokenCacheClient.deleteBox(),
]);
}
}
Future<void> cleanEmailCache() async {
await Future.wait([
_stateCacheClient.deleteItem(StateType.email.value),
_emailCacheClient.clearAllData(),
]);
if (kIsWeb) {
await Future.wait([
_stateCacheClient.deleteItem(StateType.email.value),
_emailCacheClient.clearAllData(),
]);
} else {
await Future.wait([
_stateCacheClient.deleteItem(StateType.email.value),
_emailCacheClient.deleteBox(),
]);
}
}
}
@@ -15,8 +15,9 @@ abstract class HiveCacheClient<T> {
Future<Box<T>> openBox() async {
if (Hive.isBoxOpen(tableName)) {
return Hive.box<T>(tableName);
} else {
return Hive.openBox<T>(tableName);
}
return Hive.openBox<T>(tableName);
}
Future<Box<T>> openBoxEncryption() async {
@@ -6,6 +6,7 @@ import 'package:core/utils/app_logger.dart';
import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart';
import 'package:tmail_ui_user/features/login/data/local/encryption_key_cache_manager.dart';
import 'package:tmail_ui_user/features/login/data/model/account_cache.dart';
import 'package:tmail_ui_user/features/login/data/model/authentication_info_cache.dart';
@@ -66,20 +67,68 @@ class HiveCacheConfig {
}
void registerAdapter() {
Hive.registerAdapter(MailboxCacheAdapter());
Hive.registerAdapter(MailboxRightsCacheAdapter());
Hive.registerAdapter(StateCacheAdapter());
Hive.registerAdapter(StateTypeAdapter());
Hive.registerAdapter(EmailAddressHiveCacheAdapter());
Hive.registerAdapter(EmailCacheAdapter());
Hive.registerAdapter(RecentSearchCacheAdapter());
Hive.registerAdapter(TokenOidcCacheAdapter());
Hive.registerAdapter(AccountCacheAdapter());
Hive.registerAdapter(EncryptionKeyCacheAdapter());
Hive.registerAdapter(AuthenticationInfoCacheAdapter());
Hive.registerAdapter(RecentLoginUrlCacheAdapter());
Hive.registerAdapter(RecentLoginUsernameCacheAdapter());
Hive.registerAdapter(FCMTokenCacheAdapter());
registerCacheAdapter<MailboxCache>(
MailboxCacheAdapter(),
CachingConstants.MAILBOX_CACHE_IDENTIFY
);
registerCacheAdapter<MailboxRightsCache>(
MailboxRightsCacheAdapter(),
CachingConstants.MAILBOX_RIGHTS_CACHE_IDENTIFY
);
registerCacheAdapter<StateCache>(
StateCacheAdapter(),
CachingConstants.STATE_CACHE_IDENTIFY
);
registerCacheAdapter<StateType>(
StateTypeAdapter(),
CachingConstants.STATE_TYPE_IDENTIFY
);
registerCacheAdapter<EmailAddressHiveCache>(
EmailAddressHiveCacheAdapter(),
CachingConstants.EMAIL_ADDRESS_HIVE_CACHE_IDENTIFY
);
registerCacheAdapter<EmailCache>(
EmailCacheAdapter(),
CachingConstants.EMAIL_CACHE_IDENTIFY
);
registerCacheAdapter<RecentSearchCache>(
RecentSearchCacheAdapter(),
CachingConstants.RECENT_SEARCH_HIVE_CACHE_IDENTIFY
);
registerCacheAdapter<TokenOidcCache>(
TokenOidcCacheAdapter(),
CachingConstants.TOKEN_OIDC_HIVE_CACHE_IDENTIFY
);
registerCacheAdapter<AccountCache>(
AccountCacheAdapter(),
CachingConstants.ACCOUNT_HIVE_CACHE_IDENTIFY
);
registerCacheAdapter<EncryptionKeyCache>(
EncryptionKeyCacheAdapter(),
CachingConstants.ENCRYPTION_KEY_HIVE_CACHE_IDENTIFY
);
registerCacheAdapter<AuthenticationInfoCache>(
AuthenticationInfoCacheAdapter(),
CachingConstants.AUTHENTICATION_INFO_HIVE_CACHE_IDENTIFY
);
registerCacheAdapter<RecentLoginUrlCache>(
RecentLoginUrlCacheAdapter(),
CachingConstants.RECENT_LOGIN_URL_HIVE_CACHE_IDENTITY
);
registerCacheAdapter<RecentLoginUsernameCache>(
RecentLoginUsernameCacheAdapter(),
CachingConstants.RECENT_LOGIN_USERNAME_HIVE_CACHE_IDENTITY
);
registerCacheAdapter<FCMTokenCache>(
FCMTokenCacheAdapter(),
CachingConstants.FCM_TOKEN_CACHE_IDENTITY
);
}
void registerCacheAdapter<T>(TypeAdapter<T> typeAdapter, int typeId) {
if (!Hive.isAdapterRegistered(typeId)) {
Hive.registerAdapter<T>(typeAdapter);
}
}
Future closeHive() async {