TF-2310 Use singleton for HiveCacheConfig to avoid creating multiple instance

This commit is contained in:
dab246
2024-04-09 10:28:38 +07:00
committed by Dat H. Pham
parent 779bd224b1
commit 017c7135d3
13 changed files with 27 additions and 21 deletions
+1 -1
View File
@@ -129,7 +129,7 @@ class CachingManager {
}
Future<void> closeHive() async {
return await HiveCacheConfig().closeHive();
return await HiveCacheConfig.instance.closeHive();
}
Future<void> clearAllFileInStorage() async {
@@ -13,7 +13,7 @@ abstract class HiveCacheClient<T> {
bool get encryption => false;
Future<Uint8List?> _getEncryptionKey() => HiveCacheConfig.getEncryptionKey();
Future<Uint8List?> _getEncryptionKey() => HiveCacheConfig.instance.getEncryptionKey();
Future<Box<T>> openBox() async {
if (Hive.isBoxOpen(tableName)) {
@@ -34,12 +34,18 @@ import 'package:tmail_ui_user/main/routes/route_navigation.dart';
class HiveCacheConfig {
Future setUp({String? cachePath}) async {
HiveCacheConfig._internal();
static final HiveCacheConfig _instance = HiveCacheConfig._internal();
static HiveCacheConfig get instance => _instance;
Future<void> setUp({String? cachePath}) async {
await initializeDatabase(databasePath: cachePath);
registerAdapter();
_registerAdapter();
}
Future initializeDatabase({String? databasePath}) async {
Future<void> initializeDatabase({String? databasePath}) async {
if (databasePath != null) {
Hive.init(databasePath);
} else {
@@ -59,7 +65,7 @@ class HiveCacheConfig {
}
}
static Future<void> initializeEncryptionKey() async {
Future<void> initializeEncryptionKey() async {
final encryptionKeyCacheManager = getBinding<EncryptionKeyCacheManager>() ?? getBinding<EncryptionKeyCacheManager>(tag: BindingTag.isolateTag);
if (encryptionKeyCacheManager == null) {
log('HiveCacheConfig::_initializeEncryptionKey(): encryptionKeyCacheManager not found');
@@ -74,7 +80,7 @@ class HiveCacheConfig {
}
}
static Future<Uint8List?> getEncryptionKey() async {
Future<Uint8List?> getEncryptionKey() async {
final encryptionKeyCacheManager = getBinding<EncryptionKeyCacheManager>() ?? getBinding<EncryptionKeyCacheManager>(tag: BindingTag.isolateTag);
if (encryptionKeyCacheManager == null) {
log('HiveCacheConfig::getEncryptionKey(): encryptionKeyCacheManager not found');
@@ -92,7 +98,7 @@ class HiveCacheConfig {
}
}
void registerAdapter() {
void _registerAdapter() {
registerCacheAdapter<MailboxCache>(
MailboxCacheAdapter(),
CachingConstants.MAILBOX_CACHE_IDENTIFY
@@ -177,7 +183,7 @@ class HiveCacheConfig {
}
}
Future closeHive() async {
await Hive.close();
Future<void> closeHive() async {
return await Hive.close();
}
}