Use Hive CE database to replace Hive database
Signed-off-by: dab246 <tdvu@linagora.com>
This commit is contained in:
@@ -87,23 +87,6 @@ class CachingManager {
|
||||
], eagerError: true);
|
||||
}
|
||||
|
||||
Future<void> clearData() async {
|
||||
await Future.wait([
|
||||
_stateCacheClient.clearAllData(),
|
||||
_mailboxCacheClient.clearAllData(),
|
||||
_emailCacheClient.clearAllData(),
|
||||
_fcmCacheClient.clearAllData(),
|
||||
_firebaseRegistrationCacheClient.clearAllData(),
|
||||
_localSpamReportManager.clear(),
|
||||
if (PlatformInfo.isMobile)
|
||||
...[
|
||||
_newEmailHiveCacheClient.clearAllData(),
|
||||
_openedEmailHiveCacheClient.clearAllData(),
|
||||
_sendingEmailCacheManager.clearAllSendingEmails(),
|
||||
]
|
||||
], eagerError: true);
|
||||
}
|
||||
|
||||
Future<void> clearEmailAndStateCache({AccountId? accountId, UserName? userName}) {
|
||||
log('CachingManager::clearEmailAndStateCache:userName = $userName');
|
||||
if (accountId != null && userName != null) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
class CacheVersion {
|
||||
static const int hiveDBVersion = 16;
|
||||
static const int hiveDBVersion = 17;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import 'dart:ui' as flutter;
|
||||
|
||||
import 'package:hive_ce/hive.dart';
|
||||
|
||||
class FcmIsolateNameServer extends IsolateNameServer {
|
||||
const FcmIsolateNameServer();
|
||||
|
||||
@override
|
||||
dynamic lookupPortByName(String name) =>
|
||||
flutter.IsolateNameServer.lookupPortByName(name);
|
||||
|
||||
@override
|
||||
bool registerPortWithName(dynamic port, String name) =>
|
||||
flutter.IsolateNameServer.registerPortWithName(port, name);
|
||||
|
||||
@override
|
||||
bool removePortNameMapping(String name) =>
|
||||
flutter.IsolateNameServer.removePortNameMapping(name);
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import 'dart:typed_data';
|
||||
|
||||
import 'package:core/presentation/extensions/map_extensions.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:hive_ce/hive.dart';
|
||||
import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart';
|
||||
import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart';
|
||||
|
||||
@@ -14,20 +14,20 @@ abstract class HiveCacheClient<T> {
|
||||
|
||||
Future<Uint8List?> _getEncryptionKey() => HiveCacheConfig.instance.getEncryptionKey();
|
||||
|
||||
Future<Box<T>> openBox() async {
|
||||
if (Hive.isBoxOpen(tableName)) {
|
||||
return Hive.box<T>(tableName);
|
||||
Future<IsolatedBox<T>> openBox() async {
|
||||
if (IsolatedHive.isBoxOpen(tableName)) {
|
||||
return IsolatedHive.box<T>(tableName);
|
||||
} else {
|
||||
return Hive.openBox<T>(tableName);
|
||||
return IsolatedHive.openBox<T>(tableName);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Box<T>> openBoxEncryption() async {
|
||||
Future<IsolatedBox<T>> openBoxEncryption() async {
|
||||
final encryptionKey = await _getEncryptionKey();
|
||||
if (Hive.isBoxOpen(tableName)) {
|
||||
return Hive.box<T>(tableName);
|
||||
if (IsolatedHive.isBoxOpen(tableName)) {
|
||||
return IsolatedHive.box<T>(tableName);
|
||||
} else {
|
||||
return Hive.openBox<T>(tableName,
|
||||
return IsolatedHive.openBox<T>(tableName,
|
||||
encryptionCipher:
|
||||
encryptionKey != null ? HiveAesCipher(encryptionKey) : null);
|
||||
}
|
||||
@@ -66,7 +66,8 @@ abstract class HiveCacheClient<T> {
|
||||
Future<List<T>> getAll() {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
return boxItem.values.toList();
|
||||
final items = await boxItem.values;
|
||||
return items.toList();
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
@@ -75,7 +76,8 @@ abstract class HiveCacheClient<T> {
|
||||
Future<List<T>> getListByNestedKey(String nestedKey) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
final listItem = boxItem.toMap()
|
||||
final mapItems = await boxItem.toMap();
|
||||
final listItem = mapItems
|
||||
.where((key, value) => _matchedNestedKey(key, nestedKey))
|
||||
.values
|
||||
.toList();
|
||||
@@ -89,7 +91,8 @@ abstract class HiveCacheClient<T> {
|
||||
Future<List<T>> getValuesByListKey(List<String> listKeys) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
return boxItem.toMap()
|
||||
final mapItems = await boxItem.toMap();
|
||||
return mapItems
|
||||
.where((key, value) => listKeys.contains(key))
|
||||
.values
|
||||
.toList();
|
||||
@@ -154,7 +157,7 @@ abstract class HiveCacheClient<T> {
|
||||
}
|
||||
|
||||
Future<void> deleteBox() {
|
||||
return Hive.deleteBoxFromDisk(tableName);
|
||||
return IsolatedHive.deleteBoxFromDisk(tableName);
|
||||
}
|
||||
|
||||
Future<void> clearAllData() {
|
||||
@@ -171,7 +174,8 @@ abstract class HiveCacheClient<T> {
|
||||
Future<void> clearAllDataContainKey(String nestedKey) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption ? await openBoxEncryption() : await openBox();
|
||||
final listKeys = boxItem.toMap()
|
||||
final mapItems = await boxItem.toMap();
|
||||
final listKeys = mapItems
|
||||
.where((key, value) => _matchedNestedKey(key, nestedKey))
|
||||
.keys;
|
||||
log('$runtimeType::clearAllDataContainKey:listKeys: ${listKeys.length}');
|
||||
@@ -182,8 +186,8 @@ abstract class HiveCacheClient<T> {
|
||||
}
|
||||
|
||||
Future<void> closeBox() async {
|
||||
if (Hive.isBoxOpen(tableName)) {
|
||||
await Hive.box<T>(tableName).close();
|
||||
if (IsolatedHive.isBoxOpen(tableName)) {
|
||||
await IsolatedHive.box<T>(tableName).close();
|
||||
}
|
||||
return Future.value();
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import 'dart:typed_data';
|
||||
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:core/utils/platform_info.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:hive_ce/hive.dart';
|
||||
import 'package:path_provider/path_provider.dart' as path_provider;
|
||||
import 'package:tmail_ui_user/features/base/upgradeable/upgrade_hive_database_steps_v10.dart';
|
||||
import 'package:tmail_ui_user/features/base/upgradeable/upgrade_hive_database_steps_v11.dart';
|
||||
@@ -16,6 +16,7 @@ import 'package:tmail_ui_user/features/base/upgradeable/upgrade_hive_database_st
|
||||
import 'package:tmail_ui_user/features/base/upgradeable/upgrade_hive_database_steps_v7.dart';
|
||||
import 'package:tmail_ui_user/features/caching/caching_manager.dart';
|
||||
import 'package:tmail_ui_user/features/caching/config/cache_version.dart';
|
||||
import 'package:tmail_ui_user/features/caching/config/fcm_isolate_nam_server.dart';
|
||||
import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart';
|
||||
import 'package:tmail_ui_user/features/home/data/model/session_hive_obj.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/local/encryption_key_cache_manager.dart';
|
||||
@@ -55,14 +56,17 @@ class HiveCacheConfig {
|
||||
}
|
||||
|
||||
Future<void> initializeDatabase({String? databasePath}) async {
|
||||
if (databasePath != null) {
|
||||
Hive.init(databasePath);
|
||||
} else {
|
||||
if (PlatformInfo.isMobile) {
|
||||
Directory directory = await path_provider.getApplicationDocumentsDirectory();
|
||||
Hive.init(directory.path);
|
||||
}
|
||||
if (databasePath == null && PlatformInfo.isMobile) {
|
||||
Directory directory = await path_provider.getApplicationDocumentsDirectory();
|
||||
databasePath = directory.path;
|
||||
}
|
||||
|
||||
if (databasePath == null) return;
|
||||
|
||||
await IsolatedHive.init(
|
||||
databasePath,
|
||||
isolateNameServer: const FcmIsolateNameServer(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> onUpgradeDatabase(CachingManager cachingManager) async {
|
||||
@@ -78,6 +82,7 @@ class HiveCacheConfig {
|
||||
await UpgradeHiveDatabaseStepsV14(cachingManager).onUpgrade(oldVersion, newVersion);
|
||||
await UpgradeHiveDatabaseStepsV15(cachingManager).onUpgrade(oldVersion, newVersion);
|
||||
await UpgradeHiveDatabaseStepsV16(cachingManager).onUpgrade(oldVersion, newVersion);
|
||||
await UpgradeHiveDatabaseStepsV16(cachingManager).onUpgrade(oldVersion, newVersion);
|
||||
|
||||
if (oldVersion != newVersion) {
|
||||
await cachingManager.storeCacheVersion(newVersion);
|
||||
@@ -201,12 +206,12 @@ class HiveCacheConfig {
|
||||
}
|
||||
|
||||
void registerCacheAdapter<T>(TypeAdapter<T> typeAdapter, int typeId) {
|
||||
if (!Hive.isAdapterRegistered(typeId)) {
|
||||
Hive.registerAdapter<T>(typeAdapter);
|
||||
if (!IsolatedHive.isAdapterRegistered(typeId)) {
|
||||
IsolatedHive.registerAdapter<T>(typeAdapter);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> closeHive() async {
|
||||
return await Hive.close();
|
||||
return await IsolatedHive.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user