Remove closeBox when open box in hive database

Signed-off-by: dab246 <tdvu@linagora.com>
This commit is contained in:
dab246
2025-05-30 10:08:02 +07:00
committed by Dat H. Pham
parent 19cede1a5f
commit 02c2e08e40
12 changed files with 11 additions and 45 deletions
@@ -52,12 +52,8 @@ abstract class HiveCacheClient<T> {
});
}
Future<T?> getItem(String key, {bool needToReopen = false}) {
log('$runtimeType::getItem() key: $encryption - needToReopen: $needToReopen');
Future<T?> getItem(String key) {
return Future.sync(() async {
if (needToReopen) {
await closeBox();
}
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
@@ -47,6 +47,4 @@ class AccountCacheManager {
log('AccountCacheManager::deleteCurrentAccount(): $hashId');
return _accountCacheClient.deleteItem(hashId);
}
Future<void> closeAccountHiveCacheBox() => _accountCacheClient.closeBox();
}
@@ -25,6 +25,4 @@ class AuthenticationInfoCacheManager {
Future<void> removeAuthenticationInfo() {
return _authenticationInfoCacheClient.deleteItem(AuthenticationInfoCache.keyCacheValue);
}
Future<void> closeAuthenticationInfoHiveCacheBox() => _authenticationInfoCacheClient.closeBox();
}
@@ -34,6 +34,4 @@ class TokenOidcCacheManager {
Future<void> deleteTokenOidc() async {
await _tokenOidcCacheClient.clearAllData();
}
Future<void> closeTokenOIDCHiveCacheBox() => _tokenOidcCacheClient.closeBox();
}
@@ -29,6 +29,4 @@ class StateCacheManager {
final stateKey = TupleKey(stateType.name, accountId.asString, userName.value).encodeKey;
return await _stateCacheClient.deleteItem(stateKey);
}
Future<void> closeStateHiveCacheBox() => _stateCacheClient.closeBox();
}
@@ -76,13 +76,11 @@ class NewEmailCacheManager {
EmailId emailId
) async {
final keyCache = TupleKey(emailId.asString, accountId.asString, userName.value).encodeKey;
final detailedEmailCache = await _cacheClient.getItem(keyCache, needToReopen: true);
final detailedEmailCache = await _cacheClient.getItem(keyCache);
if (detailedEmailCache != null) {
return detailedEmailCache;
} else {
throw NotFoundStoredNewEmailException();
}
}
Future<void> closeNewEmailHiveCacheBox() => _cacheClient.closeBox();
}
@@ -74,7 +74,7 @@ class OpenedEmailCacheManager {
EmailId emailId
) async {
final keyCache = TupleKey(emailId.asString, accountId.asString, userName.value).encodeKey;
final detailedEmailCache = await _cacheClient.getItem(keyCache, needToReopen: true);
final detailedEmailCache = await _cacheClient.getItem(keyCache);
if (detailedEmailCache != null) {
return detailedEmailCache;
} else {
@@ -23,7 +23,7 @@ class FCMCacheManager {
Future<jmap.State> getStateToRefresh(AccountId accountId, UserName userName, TypeName typeName) async {
final stateKeyCache = TupleKey(typeName.value, accountId.asString, userName.value).encodeKey;
final stateValue = await _fcmCacheClient.getItem(stateKeyCache, needToReopen: true);
final stateValue = await _fcmCacheClient.getItem(stateKeyCache);
if (stateValue != null) {
return jmap.State(stateValue);
} else {
@@ -52,7 +52,7 @@ class FCMCacheManager {
}
Future<FirebaseRegistrationCache> getStoredFirebaseRegistration() async {
final firebaseRegistration = await _firebaseRegistrationCacheClient.getItem(FirebaseRegistrationCache.keyCacheValue, needToReopen: true);
final firebaseRegistration = await _firebaseRegistrationCacheClient.getItem(FirebaseRegistrationCache.keyCacheValue);
if (firebaseRegistration == null) {
throw NotFoundFirebaseRegistrationCacheException();
} else {
@@ -17,14 +17,10 @@ import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions
import 'package:tmail_ui_user/features/home/domain/state/get_session_state.dart';
import 'package:tmail_ui_user/features/home/domain/usecases/get_session_interactor.dart';
import 'package:tmail_ui_user/features/home/presentation/home_bindings.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/token_oidc_cache_manager.dart';
import 'package:tmail_ui_user/features/login/data/network/interceptors/authorization_interceptors.dart';
import 'package:tmail_ui_user/features/login/domain/state/get_credential_state.dart';
import 'package:tmail_ui_user/features/login/domain/state/get_stored_token_oidc_state.dart';
import 'package:tmail_ui_user/features/login/domain/usecases/get_authenticated_account_interactor.dart';
import 'package:tmail_ui_user/features/mailbox/data/local/state_cache_manager.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/bindings/mailbox_dashboard_bindings.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/bindings/fcm_interactor_bindings.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/controller/fcm_token_controller.dart';
@@ -43,11 +39,6 @@ class FcmMessageController extends PushBaseController {
AuthorizationInterceptors? _authorizationInterceptors;
GetSessionInteractor? _getSessionInteractor;
AccountCacheManager? _accountCacheManager;
TokenOidcCacheManager? _tokenOidcCacheManager;
StateCacheManager? _stateCacheManager;
AuthenticationInfoCacheManager? _authenticationInfoCacheManager;
FcmMessageController._internal();
static final FcmMessageController _instance = FcmMessageController._internal();
@@ -96,17 +87,6 @@ class FcmMessageController extends PushBaseController {
});
_getInteractorBindings();
await Future.wait([
if (_accountCacheManager != null)
_accountCacheManager!.closeAccountHiveCacheBox(),
if (_tokenOidcCacheManager != null)
_tokenOidcCacheManager!.closeTokenOIDCHiveCacheBox(),
if (_stateCacheManager != null)
_stateCacheManager!.closeStateHiveCacheBox(),
if (_authenticationInfoCacheManager != null)
_authenticationInfoCacheManager!.closeAuthenticationInfoHiveCacheBox(),
]);
}
void _getInteractorBindings() {
@@ -114,10 +94,6 @@ class FcmMessageController extends PushBaseController {
_dynamicUrlInterceptors = getBinding<DynamicUrlInterceptors>();
_authorizationInterceptors = getBinding<AuthorizationInterceptors>();
_getSessionInteractor = getBinding<GetSessionInteractor>();
_accountCacheManager = getBinding<AccountCacheManager>();
_tokenOidcCacheManager = getBinding<TokenOidcCacheManager>();
_stateCacheManager = getBinding<StateCacheManager>();
_authenticationInfoCacheManager = getBinding<AuthenticationInfoCacheManager>();
FcmTokenController.instance.initialBindingInteractor();
}
@@ -56,12 +56,16 @@ extension EmailCacheExtension on EmailCache {
bool expireTimeCaching(EmailCleanupRule cleanupRule) {
final currentTime = DateTime.now();
log('EmailCacheExtension::expireTimeCaching():currentTime: $currentTime | receivedAt: $receivedAt');
if (receivedAt != null) {
final countDay = currentTime.daysBetween(receivedAt!.toLocal());
log('EmailCacheExtension::expireTimeCaching():countDay: $countDay | cleanupRule.cachingEmailPeriod.countDay: ${cleanupRule.cachingEmailPeriod.countDay}');
if (countDay >= cleanupRule.cachingEmailPeriod.countDay) {
log('EmailCacheExtension::expireTimeCaching():true');
return true;
}
}
log('EmailCacheExtension::expireTimeCaching():false');
return false;
}
}
@@ -111,7 +111,7 @@ class EmailCacheManager {
Future<EmailCache> getStoredEmail(AccountId accountId, UserName userName, EmailId emailId) async {
final keyCache = TupleKey(emailId.asString, accountId.asString, userName.value).encodeKey;
final emailCache = await _emailCacheClient.getItem(keyCache, needToReopen: true);
final emailCache = await _emailCacheClient.getItem(keyCache);
if (emailCache != null) {
return emailCache;
} else {
@@ -48,7 +48,7 @@ class MemoryAccountCacheClient implements AccountCacheClient {
}
@override
Future<AccountCache?> getItem(String key, {bool needToReopen = false}) {
Future<AccountCache?> getItem(String key) {
return Future.value(_cache[key]);
}