Close hive boxes in Background (notificaiton)
This commit is contained in:
committed by
Dat H. Pham
parent
7b5c0d5a75
commit
adaab2198d
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:core/presentation/extensions/map_extensions.dart';
|
import 'package:core/presentation/extensions/map_extensions.dart';
|
||||||
@@ -28,19 +27,16 @@ abstract class HiveCacheClient<T> {
|
|||||||
if (Hive.isBoxOpen(tableName)) {
|
if (Hive.isBoxOpen(tableName)) {
|
||||||
return Hive.box<T>(tableName);
|
return Hive.box<T>(tableName);
|
||||||
} else {
|
} else {
|
||||||
return Hive.openBox<T>(
|
return Hive.openBox<T>(tableName,
|
||||||
tableName,
|
encryptionCipher:
|
||||||
encryptionCipher: encryptionKey != null
|
encryptionKey != null ? HiveAesCipher(encryptionKey) : null);
|
||||||
? HiveAesCipher(encryptionKey)
|
|
||||||
: null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> insertItem(String key, T newObject) {
|
Future<void> insertItem(String key, T newObject) {
|
||||||
|
log('$runtimeType::insertItem:encryption: $encryption - key = $key');
|
||||||
return Future.sync(() async {
|
return Future.sync(() async {
|
||||||
final boxItem = encryption
|
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||||
? await openBoxEncryption()
|
|
||||||
: await openBox();
|
|
||||||
return boxItem.put(key, newObject);
|
return boxItem.put(key, newObject);
|
||||||
}).catchError((error) {
|
}).catchError((error) {
|
||||||
throw error;
|
throw error;
|
||||||
@@ -49,9 +45,7 @@ abstract class HiveCacheClient<T> {
|
|||||||
|
|
||||||
Future<void> insertMultipleItem(Map<String, T> mapObject) {
|
Future<void> insertMultipleItem(Map<String, T> mapObject) {
|
||||||
return Future.sync(() async {
|
return Future.sync(() async {
|
||||||
final boxItem = encryption
|
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||||
? await openBoxEncryption()
|
|
||||||
: await openBox();
|
|
||||||
return boxItem.putAll(mapObject);
|
return boxItem.putAll(mapObject);
|
||||||
}).catchError((error) {
|
}).catchError((error) {
|
||||||
throw error;
|
throw error;
|
||||||
@@ -59,6 +53,7 @@ abstract class HiveCacheClient<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<T?> getItem(String key, {bool needToReopen = false}) {
|
Future<T?> getItem(String key, {bool needToReopen = false}) {
|
||||||
|
log('$runtimeType::getItem() key: $encryption - needToReopen: $needToReopen');
|
||||||
return Future.sync(() async {
|
return Future.sync(() async {
|
||||||
if (needToReopen) {
|
if (needToReopen) {
|
||||||
await closeBox();
|
await closeBox();
|
||||||
@@ -88,7 +83,7 @@ abstract class HiveCacheClient<T> {
|
|||||||
.where((key, value) => _matchedNestedKey(key, nestedKey))
|
.where((key, value) => _matchedNestedKey(key, nestedKey))
|
||||||
.values
|
.values
|
||||||
.toList();
|
.toList();
|
||||||
log('HiveCacheClient::getListByNestedKey:listItem: ${listItem.length}');
|
log('$runtimeType::getListByNestedKey:listItem: ${listItem.length}');
|
||||||
return listItem;
|
return listItem;
|
||||||
}).catchError((error) {
|
}).catchError((error) {
|
||||||
throw error;
|
throw error;
|
||||||
@@ -180,8 +175,10 @@ abstract class HiveCacheClient<T> {
|
|||||||
Future<void> clearAllDataContainKey(String nestedKey) {
|
Future<void> clearAllDataContainKey(String nestedKey) {
|
||||||
return Future.sync(() async {
|
return Future.sync(() async {
|
||||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||||
final listKeys = boxItem.toMap().where((key, value) => _matchedNestedKey(key, nestedKey)).keys;
|
final listKeys = boxItem.toMap()
|
||||||
log('HiveCacheClient::clearAllDataContainKey:listKeys: ${listKeys.length}');
|
.where((key, value) => _matchedNestedKey(key, nestedKey))
|
||||||
|
.keys;
|
||||||
|
log('$runtimeType::clearAllDataContainKey:listKeys: ${listKeys.length}');
|
||||||
return boxItem.deleteAll(listKeys);
|
return boxItem.deleteAll(listKeys);
|
||||||
}).catchError((error) {
|
}).catchError((error) {
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ class AccountCacheManager {
|
|||||||
log('AccountCacheManager::setCurrentAccount(): $newAccount');
|
log('AccountCacheManager::setCurrentAccount(): $newAccount');
|
||||||
final newAccountCache = newAccount.toCache();
|
final newAccountCache = newAccount.toCache();
|
||||||
final allAccounts = await _accountCacheClient.getAll();
|
final allAccounts = await _accountCacheClient.getAll();
|
||||||
log('AccountCacheManager::setCurrentAccount::allAccounts(): $allAccounts');
|
log('AccountCacheManager::setCurrentAccount::allAccounts(): length: ${allAccounts.length}, $allAccounts');
|
||||||
if (allAccounts.isNotEmpty) {
|
if (allAccounts.isNotEmpty) {
|
||||||
final newAllAccounts = allAccounts
|
final newAllAccounts = allAccounts
|
||||||
.unselected()
|
.unselected()
|
||||||
@@ -47,4 +47,6 @@ class AccountCacheManager {
|
|||||||
log('AccountCacheManager::deleteCurrentAccount(): $hashId');
|
log('AccountCacheManager::deleteCurrentAccount(): $hashId');
|
||||||
return _accountCacheClient.deleteItem(hashId);
|
return _accountCacheClient.deleteItem(hashId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> closeAccountHiveCacheBox() => _accountCacheClient.closeBox();
|
||||||
}
|
}
|
||||||
@@ -25,4 +25,6 @@ class AuthenticationInfoCacheManager {
|
|||||||
Future<void> removeAuthenticationInfo() {
|
Future<void> removeAuthenticationInfo() {
|
||||||
return _authenticationInfoCacheClient.deleteItem(AuthenticationInfoCache.keyCacheValue);
|
return _authenticationInfoCacheClient.deleteItem(AuthenticationInfoCache.keyCacheValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> closeAuthenticationInfoHiveCacheBox() => _authenticationInfoCacheClient.closeBox();
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@ class TokenOidcCacheManager {
|
|||||||
TokenOidcCacheManager(this._tokenOidcCacheClient);
|
TokenOidcCacheManager(this._tokenOidcCacheClient);
|
||||||
|
|
||||||
Future<TokenOIDC> getTokenOidc(String tokenIdHash) async {
|
Future<TokenOIDC> getTokenOidc(String tokenIdHash) async {
|
||||||
|
log('TokenOidcCacheManager::getTokenOidc(): tokenIdHash: $tokenIdHash');
|
||||||
final tokenCache = await _tokenOidcCacheClient.getItem(tokenIdHash);
|
final tokenCache = await _tokenOidcCacheClient.getItem(tokenIdHash);
|
||||||
log('TokenOidcCacheManager::getTokenOidc(): tokenCache: $tokenCache');
|
log('TokenOidcCacheManager::getTokenOidc(): tokenCache: $tokenCache');
|
||||||
if (tokenCache == null) {
|
if (tokenCache == null) {
|
||||||
@@ -27,9 +28,12 @@ class TokenOidcCacheManager {
|
|||||||
log('TokenOidcCacheManager::persistOneTokenOidc(): key\'s hash: ${tokenOIDC.tokenIdHash}');
|
log('TokenOidcCacheManager::persistOneTokenOidc(): key\'s hash: ${tokenOIDC.tokenIdHash}');
|
||||||
log('TokenOidcCacheManager::persistOneTokenOidc(): token: ${tokenOIDC.token}');
|
log('TokenOidcCacheManager::persistOneTokenOidc(): token: ${tokenOIDC.token}');
|
||||||
await _tokenOidcCacheClient.insertItem(tokenOIDC.tokenIdHash, tokenOIDC.toTokenOidcCache());
|
await _tokenOidcCacheClient.insertItem(tokenOIDC.tokenIdHash, tokenOIDC.toTokenOidcCache());
|
||||||
|
log('TokenOidcCacheManager::persistOneTokenOidc(): done');
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> deleteTokenOidc() async {
|
Future<void> deleteTokenOidc() async {
|
||||||
await _tokenOidcCacheClient.clearAllData();
|
await _tokenOidcCacheClient.clearAllData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> closeTokenOIDCHiveCacheBox() => _tokenOidcCacheClient.closeBox();
|
||||||
}
|
}
|
||||||
@@ -24,4 +24,6 @@ class StateCacheManager {
|
|||||||
final stateKey = TupleKey(stateCache.type.name, accountId.asString, userName.value).encodeKey;
|
final stateKey = TupleKey(stateCache.type.name, accountId.asString, userName.value).encodeKey;
|
||||||
return await _stateCacheClient.insertItem(stateKey, stateCache);
|
return await _stateCacheClient.insertItem(stateKey, stateCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> closeStateHiveCacheBox() => _stateCacheClient.closeBox();
|
||||||
}
|
}
|
||||||
@@ -21,10 +21,14 @@ 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/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/domain/usecases/get_session_interactor.dart';
|
||||||
import 'package:tmail_ui_user/features/home/presentation/home_bindings.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/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_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/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/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/mailbox_dashboard/presentation/bindings/mailbox_dashboard_bindings.dart';
|
||||||
import 'package:tmail_ui_user/features/push_notification/presentation/action/fcm_action.dart';
|
import 'package:tmail_ui_user/features/push_notification/presentation/action/fcm_action.dart';
|
||||||
import 'package:tmail_ui_user/features/push_notification/presentation/bindings/fcm_interactor_bindings.dart';
|
import 'package:tmail_ui_user/features/push_notification/presentation/bindings/fcm_interactor_bindings.dart';
|
||||||
@@ -49,6 +53,11 @@ class FcmMessageController extends FcmBaseController {
|
|||||||
AuthorizationInterceptors? _authorizationInterceptors;
|
AuthorizationInterceptors? _authorizationInterceptors;
|
||||||
GetSessionInteractor? _getSessionInteractor;
|
GetSessionInteractor? _getSessionInteractor;
|
||||||
|
|
||||||
|
AccountCacheManager? _accountCacheManager;
|
||||||
|
TokenOidcCacheManager? _tokenOidcCacheManager;
|
||||||
|
StateCacheManager? _stateCacheManager;
|
||||||
|
AuthenticationInfoCacheManager? _authenticationInfoCacheManager;
|
||||||
|
|
||||||
FcmMessageController._internal();
|
FcmMessageController._internal();
|
||||||
|
|
||||||
static final FcmMessageController _instance = FcmMessageController._internal();
|
static final FcmMessageController _instance = FcmMessageController._internal();
|
||||||
@@ -187,6 +196,17 @@ class FcmMessageController extends FcmBaseController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
_getInteractorBindings();
|
_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() {
|
void _getInteractorBindings() {
|
||||||
@@ -194,6 +214,10 @@ class FcmMessageController extends FcmBaseController {
|
|||||||
_dynamicUrlInterceptors = getBinding<DynamicUrlInterceptors>();
|
_dynamicUrlInterceptors = getBinding<DynamicUrlInterceptors>();
|
||||||
_authorizationInterceptors = getBinding<AuthorizationInterceptors>();
|
_authorizationInterceptors = getBinding<AuthorizationInterceptors>();
|
||||||
_getSessionInteractor = getBinding<GetSessionInteractor>();
|
_getSessionInteractor = getBinding<GetSessionInteractor>();
|
||||||
|
_accountCacheManager = getBinding<AccountCacheManager>();
|
||||||
|
_tokenOidcCacheManager = getBinding<TokenOidcCacheManager>();
|
||||||
|
_stateCacheManager = getBinding<StateCacheManager>();
|
||||||
|
_authenticationInfoCacheManager = getBinding<AuthenticationInfoCacheManager>();
|
||||||
|
|
||||||
FcmTokenController.instance.initialBindingInteractor();
|
FcmTokenController.instance.initialBindingInteractor();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user