Optimize HiveCacheClient

This commit is contained in:
dab246
2022-10-24 10:47:54 +07:00
committed by Dat H. Pham
parent ae89802845
commit bdbc8b4a11
10 changed files with 135 additions and 1097 deletions
+1 -121
View File
@@ -1,128 +1,8 @@
import 'package:core/utils/app_logger.dart';
import 'package:hive/hive.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
import 'package:tmail_ui_user/features/login/data/model/account_cache.dart';
class AccountCacheClient extends HiveCacheClient<AccountCache> {
@override
String get tableName => "AccountCache";
@override
Future<void> clearAllData() {
return Future.sync(() async {
final boxAccount = await openBox();
return boxAccount.clear();
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxAccount = await openBox();
return boxAccount.delete(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteMultipleItem(List<String> listKey) {
return Future.sync(() async {
final boxAccount = await openBox();
return boxAccount.deleteAll(listKey);
}).catchError((error) {
throw error;
});
}
@override
Future<List<AccountCache>> getAll() {
return Future.sync(() async {
final boxAccount = await openBox();
return boxAccount.values.toList();
}).catchError((error) {
throw error;
});
}
@override
Future<AccountCache?> getItem(String key) {
return Future.sync(() async {
final boxAccount = await openBox();
return boxAccount.get(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertItem(String key, AccountCache newObject) {
log('AccountCacheClient::insertItem(): $key: $newObject');
return Future.sync(() async {
final boxAccount = await openBox();
return boxAccount.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertMultipleItem(Map<String, AccountCache> mapObject) {
return Future.sync(() async {
final boxAccount = await openBox();
return boxAccount.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxAccount = await openBox();
return boxAccount.containsKey(key);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistTable() {
return Future.sync(() async {
return Hive.boxExists(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<Box<AccountCache>> openBox() async {
if (Hive.isBoxOpen(tableName)) {
return Hive.box<AccountCache>(tableName);
}
return Hive.openBox<AccountCache>(tableName);
}
@override
Future<void> updateItem(String key, AccountCache newObject) {
return Future.sync(() async {
final boxAccount = await openBox();
return boxAccount.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateMultipleItem(Map<String, AccountCache> mapObject) {
return Future.sync(() async {
final boxAccount = await openBox();
return boxAccount.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
String get tableName => 'AccountCache';
}
@@ -1,132 +1,11 @@
import 'package:hive/hive.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
import 'package:tmail_ui_user/features/login/data/model/authentication_info_cache.dart';
class AuthenticationInfoCacheClient extends HiveCacheClient<AuthenticationInfoCache> {
@override
String get tableName => "AuthenticationInfoCache";
String get tableName => 'AuthenticationInfoCache';
@override
Future<void> clearAllData() {
return Future.sync(() async {
final boxAuthenticationInfo = await openBox();
return boxAuthenticationInfo.clear();
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxAuthenticationInfo = await openBox();
return boxAuthenticationInfo.delete(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteMultipleItem(List<String> listKey) {
return Future.sync(() async {
final boxAuthenticationInfo = await openBox();
return boxAuthenticationInfo.deleteAll(listKey);
}).catchError((error) {
throw error;
});
}
@override
Future<List<AuthenticationInfoCache>> getAll() {
return Future.sync(() async {
final boxAuthenticationInfo = await openBox();
return boxAuthenticationInfo.values.toList();
}).catchError((error) {
throw error;
});
}
@override
Future<AuthenticationInfoCache?> getItem(String key) {
return Future.sync(() async {
final boxAuthenticationInfo = await openBox();
return boxAuthenticationInfo.get(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertItem(String key, AuthenticationInfoCache newObject) {
return Future.sync(() async {
final boxAuthenticationInfo = await openBox();
return boxAuthenticationInfo.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertMultipleItem(Map<String, AuthenticationInfoCache> mapObject) {
return Future.sync(() async {
final boxAuthenticationInfo = await openBox();
return boxAuthenticationInfo.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxAuthenticationInfo = await openBox();
return boxAuthenticationInfo.containsKey(key);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistTable() {
return Future.sync(() async {
return Hive.boxExists(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<Box<AuthenticationInfoCache>> openBox() async {
final encryptionKey = await getEncryptionKey();
if (Hive.isBoxOpen(tableName)) {
return Hive.box<AuthenticationInfoCache>(tableName);
} else {
return Hive.openBox<AuthenticationInfoCache>(
tableName,
encryptionCipher: encryptionKey != null
? HiveAesCipher(encryptionKey)
: null);
}
}
@override
Future<void> updateItem(String key, AuthenticationInfoCache newObject) {
return Future.sync(() async {
final boxAuthenticationInfo = await openBox();
return boxAuthenticationInfo.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateMultipleItem(Map<String, AuthenticationInfoCache> mapObject) {
return Future.sync(() async {
final boxAuthenticationInfo = await openBox();
return boxAuthenticationInfo.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
bool get encryption => true;
}
@@ -8,35 +8,149 @@ abstract class HiveCacheClient<T> {
String get tableName;
Future<Box<T>> openBox();
bool get encryption => false;
Future<void> insertItem(String key, T newObject);
Future<Uint8List?> _getEncryptionKey() => HiveCacheConfig.getEncryptionKey();
Future<void> insertMultipleItem(Map<String, T> mapObject);
Future<Box<T>> openBox() async {
if (Hive.isBoxOpen(tableName)) {
return Hive.box<T>(tableName);
}
return Hive.openBox<T>(tableName);
}
Future<T?> getItem(String key);
Future<Box<T>> openBoxEncryption() async {
final encryptionKey = await _getEncryptionKey();
if (Hive.isBoxOpen(tableName)) {
return Hive.box<T>(tableName);
} else {
return Hive.openBox<T>(
tableName,
encryptionCipher: encryptionKey != null
? HiveAesCipher(encryptionKey)
: null);
}
}
Future<List<T>> getAll();
Future<void> insertItem(String key, T newObject) {
return Future.sync(() async {
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
return boxItem.put(key, newObject);
}).catchError((error) {
throw error;
});
}
Future<void> updateItem(String key, T newObject);
Future<void> insertMultipleItem(Map<String, T> mapObject) {
return Future.sync(() async {
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
return boxItem.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
Future<void> updateMultipleItem(Map<String, T> mapObject);
Future<T?> getItem(String key) {
return Future.sync(() async {
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
return boxItem.get(key);
}).catchError((error) {
throw error;
});
}
Future<void> deleteItem(String key);
Future<List<T>> getAll() {
return Future.sync(() async {
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
return boxItem.values.toList();
}).catchError((error) {
throw error;
});
}
Future<void> deleteMultipleItem(List<String> listKey);
Future<void> updateItem(String key, T newObject) {
return Future.sync(() async {
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
return boxItem.put(key, newObject);
}).catchError((error) {
throw error;
});
}
Future<bool> isExistItem(String key);
Future<void> updateMultipleItem(Map<String, T> mapObject) {
return Future.sync(() async {
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
return boxItem.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
Future<bool> isExistTable();
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
return boxItem.delete(key);
}).catchError((error) {
throw error;
});
}
Future<void> deleteMultipleItem(List<String> listKey) {
return Future.sync(() async {
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
return boxItem.deleteAll(listKey);
}).catchError((error) {
throw error;
});
}
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
return boxItem.containsKey(key);
}).catchError((error) {
throw error;
});
}
Future<bool> isExistTable() {
return Future.sync(() async {
return Hive.boxExists(tableName);
}).catchError((error) {
throw error;
});
}
Future<void> deleteBox() {
return Hive.deleteBoxFromDisk(tableName);
}
Future<void> clearAllData();
Future<Uint8List?> getEncryptionKey() {
return HiveCacheConfig.getEncryptionKey();
Future<void> clearAllData() {
return Future.sync(() async {
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
return boxItem.clear();
}).catchError((error) {
throw error;
});
}
}
@@ -1,6 +1,4 @@
import 'package:core/core.dart';
import 'package:hive/hive.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
import 'package:tmail_ui_user/features/thread/data/model/email_cache.dart';
@@ -10,115 +8,6 @@ class EmailCacheClient extends HiveCacheClient<EmailCache> {
@override
String get tableName => 'EmailCache';
@override
Future<Box<EmailCache>> openBox() async {
if (Hive.isBoxOpen(tableName)) {
return Hive.box<EmailCache>(tableName);
}
return Hive.openBox<EmailCache>(tableName);
}
@override
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.containsKey(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.delete(key);
}).catchError((error) {
throw error;
});
}
@override
Future<EmailCache?> getItem(String key) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.get(key);
}).catchError((error) {
throw error;
});
}
@override
Future<List<EmailCache>> getAll() {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.values.toList();
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertItem(String key, EmailCache newObject) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertMultipleItem(Map<String, EmailCache> mapObject) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateItem(String key, EmailCache newObject) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistTable() {
return Future.sync(() async {
return Hive.boxExists(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteMultipleItem(List<String> listKey) {
return Future.sync(() async {
log('EmailCacheClient::deleteMultipleItem(): listKey: ${listKey.length}');
final boxEmail = await openBox();
return boxEmail.deleteAll(listKey);
}).catchError((error) {
log('EmailCacheClient::deleteMultipleItem(): error: $error');
throw error;
});
}
@override
Future<void> updateMultipleItem(Map<String, EmailCache> mapObject) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
Future<List<EmailCache>> getListEmailCacheByMailboxId(MailboxId mailboxId) {
return Future.sync(() async {
final boxEmail = await openBox();
@@ -131,14 +20,4 @@ class EmailCacheClient extends HiveCacheClient<EmailCache> {
throw error;
});
}
@override
Future<void> clearAllData() {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.clear();
}).catchError((error) {
throw error;
});
}
}
@@ -1,128 +1,8 @@
import 'package:hive/hive.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
import 'package:tmail_ui_user/features/login/data/model/encryption_key_cache.dart';
class EncryptionKeyCacheClient extends HiveCacheClient<EncryptionKeyCache> {
@override
String get tableName => "EncryptionKeyCache";
@override
Future<void> clearAllData() {
return Future.sync(() async {
final boxEncryptionKey = await openBox();
return boxEncryptionKey.clear();
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxEncryptionKey = await openBox();
return boxEncryptionKey.delete(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteMultipleItem(List<String> listKey) {
return Future.sync(() async {
final boxEncryptionKey = await openBox();
return boxEncryptionKey.deleteAll(listKey);
}).catchError((error) {
throw error;
});
}
@override
Future<List<EncryptionKeyCache>> getAll() {
return Future.sync(() async {
final boxEncryptionKey = await openBox();
return boxEncryptionKey.values.toList();
}).catchError((error) {
throw error;
});
}
@override
Future<EncryptionKeyCache?> getItem(String key) {
return Future.sync(() async {
final boxEncryptionKey = await openBox();
return boxEncryptionKey.get(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertItem(String key, EncryptionKeyCache newObject) {
return Future.sync(() async {
final boxEncryptionKey = await openBox();
return boxEncryptionKey.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertMultipleItem(Map<String, EncryptionKeyCache> mapObject) {
return Future.sync(() async {
final boxEncryptionKey = await openBox();
return boxEncryptionKey.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxEncryptionKey = await openBox();
return boxEncryptionKey.containsKey(key);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistTable() {
return Future.sync(() async {
return Hive.boxExists(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<Box<EncryptionKeyCache>> openBox() async {
if (Hive.isBoxOpen(tableName)) {
return Hive.box<EncryptionKeyCache>(tableName);
} else {
return Hive.openBox<EncryptionKeyCache>(tableName);
}
}
@override
Future<void> updateItem(String key, EncryptionKeyCache newObject) {
return Future.sync(() async {
final boxEncryptionKey = await openBox();
return boxEncryptionKey.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateMultipleItem(Map<String, EncryptionKeyCache> mapObject) {
return Future.sync(() async {
final boxEncryptionKey = await openBox();
return boxEncryptionKey.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
String get tableName => 'EncryptionKeyCache';
}
@@ -1,5 +1,4 @@
import 'package:hive/hive.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_cache.dart';
@@ -7,121 +6,4 @@ class MailboxCacheClient extends HiveCacheClient<MailboxCache> {
@override
String get tableName => 'MailboxCache';
@override
Future<Box<MailboxCache>> openBox() async {
if (Hive.isBoxOpen(tableName)) {
return Hive.box<MailboxCache>(tableName);
}
return Hive.openBox<MailboxCache>(tableName);
}
@override
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxMailbox = await openBox();
return boxMailbox.containsKey(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxMailbox = await openBox();
return boxMailbox.delete(key);
}).catchError((error) {
throw error;
});
}
@override
Future<MailboxCache?> getItem(String key) {
return Future.sync(() async {
final boxMailbox = await openBox();
return boxMailbox.get(key);
}).catchError((error) {
throw error;
});
}
@override
Future<List<MailboxCache>> getAll() {
return Future.sync(() async {
final boxMailbox = await openBox();
return boxMailbox.values.toList();
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertItem(String key, MailboxCache newObject) {
return Future.sync(() async {
final boxMailbox = await openBox();
return boxMailbox.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertMultipleItem(Map<String, MailboxCache> mapObject) {
return Future.sync(() async {
final boxMailbox = await openBox();
return boxMailbox.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateItem(String key, MailboxCache newObject) {
return Future.sync(() async {
final boxMailbox = await openBox();
return boxMailbox.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistTable() {
return Future.sync(() async {
return Hive.boxExists(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteMultipleItem(List<String> listKey) {
return Future.sync(() async {
final boxMailbox = await openBox();
return boxMailbox.deleteAll(listKey);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateMultipleItem(Map<String, MailboxCache> mapObject) {
return Future.sync(() async {
final boxMailbox = await openBox();
return boxMailbox.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> clearAllData() {
return Future.sync(() async {
final boxMailbox = await openBox();
return boxMailbox.clear();
}).catchError((error) {
throw error;
});
}
}
@@ -1,4 +1,3 @@
import 'package:hive/hive.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
import 'package:tmail_ui_user/features/login/data/model/recent_login_url_cache.dart';
@@ -6,121 +5,4 @@ class RecentLoginUrlCacheClient extends HiveCacheClient<RecentLoginUrlCache> {
@override
String get tableName => 'RecentLoginUrlCache';
@override
Future<void> clearAllData() {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.clear();
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.delete(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteMultipleItem(List<String> listKey) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.deleteAll(listKey);
}).catchError((error) {
throw error;
});
}
@override
Future<List<RecentLoginUrlCache>> getAll() {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.values.toList();
}).catchError((error) {
throw error;
});
}
@override
Future<RecentLoginUrlCache?> getItem(String key) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.get(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertItem(String key, RecentLoginUrlCache newObject) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertMultipleItem(Map<String, RecentLoginUrlCache> mapObject) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.containsKey(key);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistTable() {
return Future.sync(() async {
return Hive.boxExists(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<Box<RecentLoginUrlCache>> openBox() async {
if (Hive.isBoxOpen(tableName)) {
return Hive.box<RecentLoginUrlCache>(tableName);
}
return Hive.openBox<RecentLoginUrlCache>(tableName);
}
@override
Future<void> updateItem(String key, RecentLoginUrlCache newObject) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateMultipleItem(Map<String, RecentLoginUrlCache> mapObject) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
}
@@ -1,5 +1,4 @@
import 'package:hive/hive.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/data/model/recent_search_cache.dart';
@@ -7,121 +6,4 @@ class RecentSearchCacheClient extends HiveCacheClient<RecentSearchCache> {
@override
String get tableName => 'RecentSearchCache';
@override
Future<void> clearAllData() {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.clear();
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.delete(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteMultipleItem(List<String> listKey) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.deleteAll(listKey);
}).catchError((error) {
throw error;
});
}
@override
Future<List<RecentSearchCache>> getAll() {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.values.toList();
}).catchError((error) {
throw error;
});
}
@override
Future<RecentSearchCache?> getItem(String key) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.get(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertItem(String key, RecentSearchCache newObject) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertMultipleItem(Map<String, RecentSearchCache> mapObject) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.containsKey(key);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistTable() {
return Future.sync(() async {
return Hive.boxExists(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<Box<RecentSearchCache>> openBox() async {
if (Hive.isBoxOpen(tableName)) {
return Hive.box<RecentSearchCache>(tableName);
}
return Hive.openBox<RecentSearchCache>(tableName);
}
@override
Future<void> updateItem(String key, RecentSearchCache newObject) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateMultipleItem(Map<String, RecentSearchCache> mapObject) {
return Future.sync(() async {
final boxRecent = await openBox();
return boxRecent.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
}
@@ -1,5 +1,4 @@
import 'package:hive/hive.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/state_cache.dart';
@@ -7,121 +6,4 @@ class StateCacheClient extends HiveCacheClient<StateCache> {
@override
String get tableName => 'StateCache';
@override
Future<Box<StateCache>> openBox() async {
if (Hive.isBoxOpen(tableName)) {
return Hive.box<StateCache>(tableName);
}
return Hive.openBox<StateCache>(tableName);
}
@override
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxState = await openBox();
return boxState.containsKey(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxState = await openBox();
return boxState.delete(key);
}).catchError((error) {
throw error;
});
}
@override
Future<StateCache?> getItem(String key) {
return Future.sync(() async {
final boxState = await openBox();
return boxState.get(key);
}).catchError((error) {
throw error;
});
}
@override
Future<List<StateCache>> getAll() {
return Future.sync(() async {
final boxState = await openBox();
return boxState.values.toList();
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertItem(String key, StateCache newObject) {
return Future.sync(() async {
final boxState = await openBox();
return boxState.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertMultipleItem(Map<String, StateCache> mapObject) {
return Future.sync(() async {
final boxState = await openBox();
return boxState.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateItem(String key, StateCache newObject) {
return Future.sync(() async {
final boxState = await openBox();
return boxState.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistTable() {
return Future.sync(() async {
return await Hive.boxExists(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteMultipleItem(List<String> listKey) {
return Future.sync(() async {
final boxState = await openBox();
return boxState.deleteAll(listKey);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateMultipleItem(Map<String, StateCache> mapObject) {
return Future.sync(() async {
final boxState = await openBox();
return boxState.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> clearAllData() {
return Future.sync(() async {
final boxState = await openBox();
return boxState.clear();
}).catchError((error) {
throw error;
});
}
}
@@ -1,133 +1,11 @@
import 'package:hive/hive.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
import 'package:tmail_ui_user/features/login/data/model/token_oidc_cache.dart';
class TokenOidcCacheClient extends HiveCacheClient<TokenOidcCache> {
@override
String get tableName => "TokenOidcCache";
String get tableName => 'TokenOidcCache';
@override
Future<void> clearAllData() {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.clear();
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.delete(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteMultipleItem(List<String> listKey) {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.deleteAll(listKey);
}).catchError((error) {
throw error;
});
}
@override
Future<List<TokenOidcCache>> getAll() {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.values.toList();
}).catchError((error) {
throw error;
});
}
@override
Future<TokenOidcCache?> getItem(String key) {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.get(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertItem(String key, TokenOidcCache newObject) {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertMultipleItem(Map<String, TokenOidcCache> mapObject) {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.containsKey(key);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistTable() {
return Future.sync(() async {
return Hive.boxExists(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<Box<TokenOidcCache>> openBox() async {
final encryptionKey = await getEncryptionKey();
if (Hive.isBoxOpen(tableName)) {
return Hive.box<TokenOidcCache>(tableName);
} else {
return Hive.openBox<TokenOidcCache>(
tableName,
encryptionCipher: encryptionKey != null
? HiveAesCipher(encryptionKey)
: null);
}
}
@override
Future<void> updateItem(String key, TokenOidcCache newObject) {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateMultipleItem(Map<String, TokenOidcCache> mapObject) {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
bool get encryption => true;
}