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();
}
}
@@ -91,7 +91,7 @@ class HomeController extends ReloadableController {
static void downloadCallback(String id, DownloadTaskStatus status, int progress) {}
void _cleanupCache() async {
await HiveCacheConfig().onUpgradeDatabase(cachingManager);
await HiveCacheConfig.instance.onUpgradeDatabase(cachingManager);
await Future.wait([
_cleanupEmailCacheInteractor.execute(EmailCleanupRule(Duration.defaultCacheInternal)),
@@ -86,7 +86,7 @@ class MailboxIsolateWorker {
) async {
final rootIsolateToken = args.isolateToken;
BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
await HiveCacheConfig().setUp();
await HiveCacheConfig.instance.setUp();
List<Email> emailListCompleted = List.empty(growable: true);
try {
@@ -78,7 +78,7 @@ class SendingEmailWorker extends Worker {
Future<void> bindDI() async {
await Future.wait([
MainBindings().dependencies(),
HiveCacheConfig().setUp()
HiveCacheConfig.instance.setUp()
]);
await Future.sync(() {
@@ -178,7 +178,7 @@ class FcmMessageController extends FcmBaseController {
Future<void> _initialAppConfig() async {
await Future.wait([
MainBindings().dependencies(),
HiveCacheConfig().setUp()
HiveCacheConfig.instance.setUp()
]);
await Future.sync(() {
@@ -68,7 +68,7 @@ class ThreadIsolateWorker {
) async {
final rootIsolateToken = args.isolateToken;
BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
await HiveCacheConfig().setUp();
await HiveCacheConfig.instance.setUp();
List<EmailId> emailListCompleted = List.empty(growable: true);
try {
@@ -86,7 +86,7 @@ class FileUploader {
) async {
final rootIsolateToken = argsUpload.isolateToken;
BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
await HiveCacheConfig().setUp();
await HiveCacheConfig.instance.setUp();
final headerParam = argsUpload.dioClient.getHeaders();
headerParam[HttpHeaders.contentTypeHeader] = argsUpload.mobileFileUpload.mimeType;
+2 -2
View File
@@ -21,11 +21,11 @@ void main() async {
await Future.wait([
MainBindings().dependencies(),
HiveCacheConfig().setUp(),
HiveCacheConfig.instance.setUp(),
Executor().warmUp(),
AppUtils.loadEnvFile()
]);
await HiveCacheConfig.initializeEncryptionKey();
await HiveCacheConfig.instance.initializeEncryptionKey();
setPathUrlStrategy();
@@ -14,7 +14,7 @@ void main() {
late EmailCacheClient emailCacheClient;
setUpAll(() {
HiveCacheConfig().setUp(cachePath: Directory.current.path);
HiveCacheConfig.instance.setUp(cachePath: Directory.current.path);
});
setUp(() {
@@ -14,7 +14,7 @@ void main() {
late MailboxCacheClient mailboxCacheClient;
setUpAll(() {
HiveCacheConfig().setUp(cachePath: Directory.current.path);
HiveCacheConfig.instance.setUp(cachePath: Directory.current.path);
});
setUp(() {
@@ -18,7 +18,7 @@ void main() {
late MailboxStateWorkerQueue workerQueue;
setUpAll(() {
HiveCacheConfig().setUp(cachePath: Directory.current.path);
HiveCacheConfig.instance.setUp(cachePath: Directory.current.path);
});
setUp(() {