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 { Future<void> closeHive() async {
return await HiveCacheConfig().closeHive(); return await HiveCacheConfig.instance.closeHive();
} }
Future<void> clearAllFileInStorage() async { Future<void> clearAllFileInStorage() async {
@@ -13,7 +13,7 @@ abstract class HiveCacheClient<T> {
bool get encryption => false; bool get encryption => false;
Future<Uint8List?> _getEncryptionKey() => HiveCacheConfig.getEncryptionKey(); Future<Uint8List?> _getEncryptionKey() => HiveCacheConfig.instance.getEncryptionKey();
Future<Box<T>> openBox() async { Future<Box<T>> openBox() async {
if (Hive.isBoxOpen(tableName)) { if (Hive.isBoxOpen(tableName)) {
@@ -34,12 +34,18 @@ import 'package:tmail_ui_user/main/routes/route_navigation.dart';
class HiveCacheConfig { 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); await initializeDatabase(databasePath: cachePath);
registerAdapter(); _registerAdapter();
} }
Future initializeDatabase({String? databasePath}) async { Future<void> initializeDatabase({String? databasePath}) async {
if (databasePath != null) { if (databasePath != null) {
Hive.init(databasePath); Hive.init(databasePath);
} else { } 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); final encryptionKeyCacheManager = getBinding<EncryptionKeyCacheManager>() ?? getBinding<EncryptionKeyCacheManager>(tag: BindingTag.isolateTag);
if (encryptionKeyCacheManager == null) { if (encryptionKeyCacheManager == null) {
log('HiveCacheConfig::_initializeEncryptionKey(): encryptionKeyCacheManager not found'); 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); final encryptionKeyCacheManager = getBinding<EncryptionKeyCacheManager>() ?? getBinding<EncryptionKeyCacheManager>(tag: BindingTag.isolateTag);
if (encryptionKeyCacheManager == null) { if (encryptionKeyCacheManager == null) {
log('HiveCacheConfig::getEncryptionKey(): encryptionKeyCacheManager not found'); log('HiveCacheConfig::getEncryptionKey(): encryptionKeyCacheManager not found');
@@ -92,7 +98,7 @@ class HiveCacheConfig {
} }
} }
void registerAdapter() { void _registerAdapter() {
registerCacheAdapter<MailboxCache>( registerCacheAdapter<MailboxCache>(
MailboxCacheAdapter(), MailboxCacheAdapter(),
CachingConstants.MAILBOX_CACHE_IDENTIFY CachingConstants.MAILBOX_CACHE_IDENTIFY
@@ -177,7 +183,7 @@ class HiveCacheConfig {
} }
} }
Future closeHive() async { Future<void> closeHive() async {
await Hive.close(); return await Hive.close();
} }
} }
@@ -91,7 +91,7 @@ class HomeController extends ReloadableController {
static void downloadCallback(String id, DownloadTaskStatus status, int progress) {} static void downloadCallback(String id, DownloadTaskStatus status, int progress) {}
void _cleanupCache() async { void _cleanupCache() async {
await HiveCacheConfig().onUpgradeDatabase(cachingManager); await HiveCacheConfig.instance.onUpgradeDatabase(cachingManager);
await Future.wait([ await Future.wait([
_cleanupEmailCacheInteractor.execute(EmailCleanupRule(Duration.defaultCacheInternal)), _cleanupEmailCacheInteractor.execute(EmailCleanupRule(Duration.defaultCacheInternal)),
@@ -86,7 +86,7 @@ class MailboxIsolateWorker {
) async { ) async {
final rootIsolateToken = args.isolateToken; final rootIsolateToken = args.isolateToken;
BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken); BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
await HiveCacheConfig().setUp(); await HiveCacheConfig.instance.setUp();
List<Email> emailListCompleted = List.empty(growable: true); List<Email> emailListCompleted = List.empty(growable: true);
try { try {
@@ -78,7 +78,7 @@ class SendingEmailWorker extends Worker {
Future<void> bindDI() async { Future<void> bindDI() async {
await Future.wait([ await Future.wait([
MainBindings().dependencies(), MainBindings().dependencies(),
HiveCacheConfig().setUp() HiveCacheConfig.instance.setUp()
]); ]);
await Future.sync(() { await Future.sync(() {
@@ -178,7 +178,7 @@ class FcmMessageController extends FcmBaseController {
Future<void> _initialAppConfig() async { Future<void> _initialAppConfig() async {
await Future.wait([ await Future.wait([
MainBindings().dependencies(), MainBindings().dependencies(),
HiveCacheConfig().setUp() HiveCacheConfig.instance.setUp()
]); ]);
await Future.sync(() { await Future.sync(() {
@@ -68,7 +68,7 @@ class ThreadIsolateWorker {
) async { ) async {
final rootIsolateToken = args.isolateToken; final rootIsolateToken = args.isolateToken;
BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken); BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
await HiveCacheConfig().setUp(); await HiveCacheConfig.instance.setUp();
List<EmailId> emailListCompleted = List.empty(growable: true); List<EmailId> emailListCompleted = List.empty(growable: true);
try { try {
@@ -86,7 +86,7 @@ class FileUploader {
) async { ) async {
final rootIsolateToken = argsUpload.isolateToken; final rootIsolateToken = argsUpload.isolateToken;
BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken); BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
await HiveCacheConfig().setUp(); await HiveCacheConfig.instance.setUp();
final headerParam = argsUpload.dioClient.getHeaders(); final headerParam = argsUpload.dioClient.getHeaders();
headerParam[HttpHeaders.contentTypeHeader] = argsUpload.mobileFileUpload.mimeType; headerParam[HttpHeaders.contentTypeHeader] = argsUpload.mobileFileUpload.mimeType;
+2 -2
View File
@@ -21,11 +21,11 @@ void main() async {
await Future.wait([ await Future.wait([
MainBindings().dependencies(), MainBindings().dependencies(),
HiveCacheConfig().setUp(), HiveCacheConfig.instance.setUp(),
Executor().warmUp(), Executor().warmUp(),
AppUtils.loadEnvFile() AppUtils.loadEnvFile()
]); ]);
await HiveCacheConfig.initializeEncryptionKey(); await HiveCacheConfig.instance.initializeEncryptionKey();
setPathUrlStrategy(); setPathUrlStrategy();
@@ -14,7 +14,7 @@ void main() {
late EmailCacheClient emailCacheClient; late EmailCacheClient emailCacheClient;
setUpAll(() { setUpAll(() {
HiveCacheConfig().setUp(cachePath: Directory.current.path); HiveCacheConfig.instance.setUp(cachePath: Directory.current.path);
}); });
setUp(() { setUp(() {
@@ -14,7 +14,7 @@ void main() {
late MailboxCacheClient mailboxCacheClient; late MailboxCacheClient mailboxCacheClient;
setUpAll(() { setUpAll(() {
HiveCacheConfig().setUp(cachePath: Directory.current.path); HiveCacheConfig.instance.setUp(cachePath: Directory.current.path);
}); });
setUp(() { setUp(() {
@@ -18,7 +18,7 @@ void main() {
late MailboxStateWorkerQueue workerQueue; late MailboxStateWorkerQueue workerQueue;
setUpAll(() { setUpAll(() {
HiveCacheConfig().setUp(cachePath: Directory.current.path); HiveCacheConfig.instance.setUp(cachePath: Directory.current.path);
}); });
setUp(() { setUp(() {