From bdbc8b4a111a614c3c7f2af7e5641b819676b673 Mon Sep 17 00:00:00 2001 From: dab246 Date: Mon, 24 Oct 2022 10:47:54 +0700 Subject: [PATCH] Optimize `HiveCacheClient` --- .../caching/account_cache_client.dart | 122 +-------------- .../authentication_info_cache_client.dart | 125 +-------------- .../caching/config/hive_cache_client.dart | 144 ++++++++++++++++-- lib/features/caching/email_cache_client.dart | 121 --------------- .../caching/encryption_key_cache_client.dart | 122 +-------------- .../caching/mailbox_cache_client.dart | 118 -------------- .../recent_login_url_cache_client.dart | 118 -------------- .../caching/recent_search_cache_client.dart | 118 -------------- lib/features/caching/state_cache_client.dart | 118 -------------- .../caching/token_oidc_cache_client.dart | 126 +-------------- 10 files changed, 135 insertions(+), 1097 deletions(-) diff --git a/lib/features/caching/account_cache_client.dart b/lib/features/caching/account_cache_client.dart index ee00149fd..302f29190 100644 --- a/lib/features/caching/account_cache_client.dart +++ b/lib/features/caching/account_cache_client.dart @@ -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 { @override - String get tableName => "AccountCache"; - - @override - Future clearAllData() { - return Future.sync(() async { - final boxAccount = await openBox(); - return boxAccount.clear(); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteItem(String key) { - return Future.sync(() async { - final boxAccount = await openBox(); - return boxAccount.delete(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteMultipleItem(List listKey) { - return Future.sync(() async { - final boxAccount = await openBox(); - return boxAccount.deleteAll(listKey); - }).catchError((error) { - throw error; - }); - } - - @override - Future> getAll() { - return Future.sync(() async { - final boxAccount = await openBox(); - return boxAccount.values.toList(); - }).catchError((error) { - throw error; - }); - } - - @override - Future getItem(String key) { - return Future.sync(() async { - final boxAccount = await openBox(); - return boxAccount.get(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future 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 insertMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxAccount = await openBox(); - return boxAccount.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistItem(String key) { - return Future.sync(() async { - final boxAccount = await openBox(); - return boxAccount.containsKey(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistTable() { - return Future.sync(() async { - return Hive.boxExists(tableName); - }).catchError((error) { - throw error; - }); - } - - @override - Future> openBox() async { - if (Hive.isBoxOpen(tableName)) { - return Hive.box(tableName); - } - return Hive.openBox(tableName); - } - - @override - Future updateItem(String key, AccountCache newObject) { - return Future.sync(() async { - final boxAccount = await openBox(); - return boxAccount.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future updateMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxAccount = await openBox(); - return boxAccount.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } + String get tableName => 'AccountCache'; } \ No newline at end of file diff --git a/lib/features/caching/authentication_info_cache_client.dart b/lib/features/caching/authentication_info_cache_client.dart index 8995a4f4d..dd2a89e7e 100644 --- a/lib/features/caching/authentication_info_cache_client.dart +++ b/lib/features/caching/authentication_info_cache_client.dart @@ -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 { @override - String get tableName => "AuthenticationInfoCache"; + String get tableName => 'AuthenticationInfoCache'; @override - Future clearAllData() { - return Future.sync(() async { - final boxAuthenticationInfo = await openBox(); - return boxAuthenticationInfo.clear(); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteItem(String key) { - return Future.sync(() async { - final boxAuthenticationInfo = await openBox(); - return boxAuthenticationInfo.delete(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteMultipleItem(List listKey) { - return Future.sync(() async { - final boxAuthenticationInfo = await openBox(); - return boxAuthenticationInfo.deleteAll(listKey); - }).catchError((error) { - throw error; - }); - } - - @override - Future> getAll() { - return Future.sync(() async { - final boxAuthenticationInfo = await openBox(); - return boxAuthenticationInfo.values.toList(); - }).catchError((error) { - throw error; - }); - } - - @override - Future getItem(String key) { - return Future.sync(() async { - final boxAuthenticationInfo = await openBox(); - return boxAuthenticationInfo.get(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertItem(String key, AuthenticationInfoCache newObject) { - return Future.sync(() async { - final boxAuthenticationInfo = await openBox(); - return boxAuthenticationInfo.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxAuthenticationInfo = await openBox(); - return boxAuthenticationInfo.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistItem(String key) { - return Future.sync(() async { - final boxAuthenticationInfo = await openBox(); - return boxAuthenticationInfo.containsKey(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistTable() { - return Future.sync(() async { - return Hive.boxExists(tableName); - }).catchError((error) { - throw error; - }); - } - - @override - Future> openBox() async { - final encryptionKey = await getEncryptionKey(); - if (Hive.isBoxOpen(tableName)) { - return Hive.box(tableName); - } else { - return Hive.openBox( - tableName, - encryptionCipher: encryptionKey != null - ? HiveAesCipher(encryptionKey) - : null); - } - } - - @override - Future updateItem(String key, AuthenticationInfoCache newObject) { - return Future.sync(() async { - final boxAuthenticationInfo = await openBox(); - return boxAuthenticationInfo.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future updateMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxAuthenticationInfo = await openBox(); - return boxAuthenticationInfo.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } + bool get encryption => true; } \ No newline at end of file diff --git a/lib/features/caching/config/hive_cache_client.dart b/lib/features/caching/config/hive_cache_client.dart index 1cc2fba64..cba583262 100644 --- a/lib/features/caching/config/hive_cache_client.dart +++ b/lib/features/caching/config/hive_cache_client.dart @@ -8,35 +8,149 @@ abstract class HiveCacheClient { String get tableName; - Future> openBox(); + bool get encryption => false; - Future insertItem(String key, T newObject); + Future _getEncryptionKey() => HiveCacheConfig.getEncryptionKey(); - Future insertMultipleItem(Map mapObject); + Future> openBox() async { + if (Hive.isBoxOpen(tableName)) { + return Hive.box(tableName); + } + return Hive.openBox(tableName); + } - Future getItem(String key); + Future> openBoxEncryption() async { + final encryptionKey = await _getEncryptionKey(); + if (Hive.isBoxOpen(tableName)) { + return Hive.box(tableName); + } else { + return Hive.openBox( + tableName, + encryptionCipher: encryptionKey != null + ? HiveAesCipher(encryptionKey) + : null); + } + } - Future> getAll(); + Future 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 updateItem(String key, T newObject); + Future insertMultipleItem(Map mapObject) { + return Future.sync(() async { + final boxItem = encryption + ? await openBoxEncryption() + : await openBox(); + return boxItem.putAll(mapObject); + }).catchError((error) { + throw error; + }); + } - Future updateMultipleItem(Map mapObject); + Future getItem(String key) { + return Future.sync(() async { + final boxItem = encryption + ? await openBoxEncryption() + : await openBox(); + return boxItem.get(key); + }).catchError((error) { + throw error; + }); + } - Future deleteItem(String key); + Future> getAll() { + return Future.sync(() async { + final boxItem = encryption + ? await openBoxEncryption() + : await openBox(); + return boxItem.values.toList(); + }).catchError((error) { + throw error; + }); + } - Future deleteMultipleItem(List listKey); + Future 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 isExistItem(String key); + Future updateMultipleItem(Map mapObject) { + return Future.sync(() async { + final boxItem = encryption + ? await openBoxEncryption() + : await openBox(); + return boxItem.putAll(mapObject); + }).catchError((error) { + throw error; + }); + } - Future isExistTable(); + Future deleteItem(String key) { + return Future.sync(() async { + final boxItem = encryption + ? await openBoxEncryption() + : await openBox(); + return boxItem.delete(key); + }).catchError((error) { + throw error; + }); + } + + Future deleteMultipleItem(List listKey) { + return Future.sync(() async { + final boxItem = encryption + ? await openBoxEncryption() + : await openBox(); + return boxItem.deleteAll(listKey); + }).catchError((error) { + throw error; + }); + } + + Future isExistItem(String key) { + return Future.sync(() async { + final boxItem = encryption + ? await openBoxEncryption() + : await openBox(); + return boxItem.containsKey(key); + }).catchError((error) { + throw error; + }); + } + + Future isExistTable() { + return Future.sync(() async { + return Hive.boxExists(tableName); + }).catchError((error) { + throw error; + }); + } Future deleteBox() { return Hive.deleteBoxFromDisk(tableName); } - Future clearAllData(); - - Future getEncryptionKey() { - return HiveCacheConfig.getEncryptionKey(); + Future clearAllData() { + return Future.sync(() async { + final boxItem = encryption + ? await openBoxEncryption() + : await openBox(); + return boxItem.clear(); + }).catchError((error) { + throw error; + }); } } \ No newline at end of file diff --git a/lib/features/caching/email_cache_client.dart b/lib/features/caching/email_cache_client.dart index 70acc70be..40df7e3ff 100644 --- a/lib/features/caching/email_cache_client.dart +++ b/lib/features/caching/email_cache_client.dart @@ -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 { @override String get tableName => 'EmailCache'; - @override - Future> openBox() async { - if (Hive.isBoxOpen(tableName)) { - return Hive.box(tableName); - } - return Hive.openBox(tableName); - } - - @override - Future isExistItem(String key) { - return Future.sync(() async { - final boxEmail = await openBox(); - return boxEmail.containsKey(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteItem(String key) { - return Future.sync(() async { - final boxEmail = await openBox(); - return boxEmail.delete(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future getItem(String key) { - return Future.sync(() async { - final boxEmail = await openBox(); - return boxEmail.get(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future> getAll() { - return Future.sync(() async { - final boxEmail = await openBox(); - return boxEmail.values.toList(); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertItem(String key, EmailCache newObject) { - return Future.sync(() async { - final boxEmail = await openBox(); - return boxEmail.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxEmail = await openBox(); - return boxEmail.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future updateItem(String key, EmailCache newObject) { - return Future.sync(() async { - final boxEmail = await openBox(); - return boxEmail.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistTable() { - return Future.sync(() async { - return Hive.boxExists(tableName); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteMultipleItem(List 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 updateMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxEmail = await openBox(); - return boxEmail.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - Future> getListEmailCacheByMailboxId(MailboxId mailboxId) { return Future.sync(() async { final boxEmail = await openBox(); @@ -131,14 +20,4 @@ class EmailCacheClient extends HiveCacheClient { throw error; }); } - - @override - Future clearAllData() { - return Future.sync(() async { - final boxEmail = await openBox(); - return boxEmail.clear(); - }).catchError((error) { - throw error; - }); - } } \ No newline at end of file diff --git a/lib/features/caching/encryption_key_cache_client.dart b/lib/features/caching/encryption_key_cache_client.dart index 05db8aff6..bae8813bf 100644 --- a/lib/features/caching/encryption_key_cache_client.dart +++ b/lib/features/caching/encryption_key_cache_client.dart @@ -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 { @override - String get tableName => "EncryptionKeyCache"; - - @override - Future clearAllData() { - return Future.sync(() async { - final boxEncryptionKey = await openBox(); - return boxEncryptionKey.clear(); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteItem(String key) { - return Future.sync(() async { - final boxEncryptionKey = await openBox(); - return boxEncryptionKey.delete(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteMultipleItem(List listKey) { - return Future.sync(() async { - final boxEncryptionKey = await openBox(); - return boxEncryptionKey.deleteAll(listKey); - }).catchError((error) { - throw error; - }); - } - - @override - Future> getAll() { - return Future.sync(() async { - final boxEncryptionKey = await openBox(); - return boxEncryptionKey.values.toList(); - }).catchError((error) { - throw error; - }); - } - - @override - Future getItem(String key) { - return Future.sync(() async { - final boxEncryptionKey = await openBox(); - return boxEncryptionKey.get(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertItem(String key, EncryptionKeyCache newObject) { - return Future.sync(() async { - final boxEncryptionKey = await openBox(); - return boxEncryptionKey.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxEncryptionKey = await openBox(); - return boxEncryptionKey.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistItem(String key) { - return Future.sync(() async { - final boxEncryptionKey = await openBox(); - return boxEncryptionKey.containsKey(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistTable() { - return Future.sync(() async { - return Hive.boxExists(tableName); - }).catchError((error) { - throw error; - }); - } - - @override - Future> openBox() async { - if (Hive.isBoxOpen(tableName)) { - return Hive.box(tableName); - } else { - return Hive.openBox(tableName); - } - } - - @override - Future updateItem(String key, EncryptionKeyCache newObject) { - return Future.sync(() async { - final boxEncryptionKey = await openBox(); - return boxEncryptionKey.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future updateMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxEncryptionKey = await openBox(); - return boxEncryptionKey.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - + String get tableName => 'EncryptionKeyCache'; } \ No newline at end of file diff --git a/lib/features/caching/mailbox_cache_client.dart b/lib/features/caching/mailbox_cache_client.dart index d6f1ad024..014907561 100644 --- a/lib/features/caching/mailbox_cache_client.dart +++ b/lib/features/caching/mailbox_cache_client.dart @@ -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 { @override String get tableName => 'MailboxCache'; - - @override - Future> openBox() async { - if (Hive.isBoxOpen(tableName)) { - return Hive.box(tableName); - } - return Hive.openBox(tableName); - } - - @override - Future isExistItem(String key) { - return Future.sync(() async { - final boxMailbox = await openBox(); - return boxMailbox.containsKey(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteItem(String key) { - return Future.sync(() async { - final boxMailbox = await openBox(); - return boxMailbox.delete(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future getItem(String key) { - return Future.sync(() async { - final boxMailbox = await openBox(); - return boxMailbox.get(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future> getAll() { - return Future.sync(() async { - final boxMailbox = await openBox(); - return boxMailbox.values.toList(); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertItem(String key, MailboxCache newObject) { - return Future.sync(() async { - final boxMailbox = await openBox(); - return boxMailbox.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxMailbox = await openBox(); - return boxMailbox.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future updateItem(String key, MailboxCache newObject) { - return Future.sync(() async { - final boxMailbox = await openBox(); - return boxMailbox.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistTable() { - return Future.sync(() async { - return Hive.boxExists(tableName); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteMultipleItem(List listKey) { - return Future.sync(() async { - final boxMailbox = await openBox(); - return boxMailbox.deleteAll(listKey); - }).catchError((error) { - throw error; - }); - } - - @override - Future updateMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxMailbox = await openBox(); - return boxMailbox.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future clearAllData() { - return Future.sync(() async { - final boxMailbox = await openBox(); - return boxMailbox.clear(); - }).catchError((error) { - throw error; - }); - } } \ No newline at end of file diff --git a/lib/features/caching/recent_login_url_cache_client.dart b/lib/features/caching/recent_login_url_cache_client.dart index 3366667de..434353acf 100644 --- a/lib/features/caching/recent_login_url_cache_client.dart +++ b/lib/features/caching/recent_login_url_cache_client.dart @@ -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 { @override String get tableName => 'RecentLoginUrlCache'; - - @override - Future clearAllData() { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.clear(); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteItem(String key) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.delete(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteMultipleItem(List listKey) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.deleteAll(listKey); - }).catchError((error) { - throw error; - }); - } - - @override - Future> getAll() { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.values.toList(); - }).catchError((error) { - throw error; - }); - } - - @override - Future getItem(String key) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.get(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertItem(String key, RecentLoginUrlCache newObject) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistItem(String key) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.containsKey(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistTable() { - return Future.sync(() async { - return Hive.boxExists(tableName); - }).catchError((error) { - throw error; - }); - } - - @override - Future> openBox() async { - if (Hive.isBoxOpen(tableName)) { - return Hive.box(tableName); - } - return Hive.openBox(tableName); - } - - @override - Future updateItem(String key, RecentLoginUrlCache newObject) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future updateMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } } \ No newline at end of file diff --git a/lib/features/caching/recent_search_cache_client.dart b/lib/features/caching/recent_search_cache_client.dart index b5fb7187d..58c3387ba 100644 --- a/lib/features/caching/recent_search_cache_client.dart +++ b/lib/features/caching/recent_search_cache_client.dart @@ -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 { @override String get tableName => 'RecentSearchCache'; - - @override - Future clearAllData() { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.clear(); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteItem(String key) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.delete(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteMultipleItem(List listKey) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.deleteAll(listKey); - }).catchError((error) { - throw error; - }); - } - - @override - Future> getAll() { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.values.toList(); - }).catchError((error) { - throw error; - }); - } - - @override - Future getItem(String key) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.get(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertItem(String key, RecentSearchCache newObject) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistItem(String key) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.containsKey(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistTable() { - return Future.sync(() async { - return Hive.boxExists(tableName); - }).catchError((error) { - throw error; - }); - } - - @override - Future> openBox() async { - if (Hive.isBoxOpen(tableName)) { - return Hive.box(tableName); - } - return Hive.openBox(tableName); - } - - @override - Future updateItem(String key, RecentSearchCache newObject) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future updateMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxRecent = await openBox(); - return boxRecent.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } } \ No newline at end of file diff --git a/lib/features/caching/state_cache_client.dart b/lib/features/caching/state_cache_client.dart index bd9b755e3..ee1126add 100644 --- a/lib/features/caching/state_cache_client.dart +++ b/lib/features/caching/state_cache_client.dart @@ -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 { @override String get tableName => 'StateCache'; - - @override - Future> openBox() async { - if (Hive.isBoxOpen(tableName)) { - return Hive.box(tableName); - } - return Hive.openBox(tableName); - } - - @override - Future isExistItem(String key) { - return Future.sync(() async { - final boxState = await openBox(); - return boxState.containsKey(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteItem(String key) { - return Future.sync(() async { - final boxState = await openBox(); - return boxState.delete(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future getItem(String key) { - return Future.sync(() async { - final boxState = await openBox(); - return boxState.get(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future> getAll() { - return Future.sync(() async { - final boxState = await openBox(); - return boxState.values.toList(); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertItem(String key, StateCache newObject) { - return Future.sync(() async { - final boxState = await openBox(); - return boxState.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxState = await openBox(); - return boxState.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future updateItem(String key, StateCache newObject) { - return Future.sync(() async { - final boxState = await openBox(); - return boxState.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistTable() { - return Future.sync(() async { - return await Hive.boxExists(tableName); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteMultipleItem(List listKey) { - return Future.sync(() async { - final boxState = await openBox(); - return boxState.deleteAll(listKey); - }).catchError((error) { - throw error; - }); - } - - @override - Future updateMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxState = await openBox(); - return boxState.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future clearAllData() { - return Future.sync(() async { - final boxState = await openBox(); - return boxState.clear(); - }).catchError((error) { - throw error; - }); - } } \ No newline at end of file diff --git a/lib/features/caching/token_oidc_cache_client.dart b/lib/features/caching/token_oidc_cache_client.dart index 813136558..bded2ee12 100644 --- a/lib/features/caching/token_oidc_cache_client.dart +++ b/lib/features/caching/token_oidc_cache_client.dart @@ -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 { @override - String get tableName => "TokenOidcCache"; + String get tableName => 'TokenOidcCache'; @override - Future clearAllData() { - return Future.sync(() async { - final boxToken = await openBox(); - return boxToken.clear(); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteItem(String key) { - return Future.sync(() async { - final boxToken = await openBox(); - return boxToken.delete(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future deleteMultipleItem(List listKey) { - return Future.sync(() async { - final boxToken = await openBox(); - return boxToken.deleteAll(listKey); - }).catchError((error) { - throw error; - }); - } - - @override - Future> getAll() { - return Future.sync(() async { - final boxToken = await openBox(); - return boxToken.values.toList(); - }).catchError((error) { - throw error; - }); - } - - @override - Future getItem(String key) { - return Future.sync(() async { - final boxToken = await openBox(); - return boxToken.get(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertItem(String key, TokenOidcCache newObject) { - return Future.sync(() async { - final boxToken = await openBox(); - return boxToken.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future insertMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxToken = await openBox(); - return boxToken.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistItem(String key) { - return Future.sync(() async { - final boxToken = await openBox(); - return boxToken.containsKey(key); - }).catchError((error) { - throw error; - }); - } - - @override - Future isExistTable() { - return Future.sync(() async { - return Hive.boxExists(tableName); - }).catchError((error) { - throw error; - }); - } - - @override - Future> openBox() async { - final encryptionKey = await getEncryptionKey(); - if (Hive.isBoxOpen(tableName)) { - return Hive.box(tableName); - } else { - return Hive.openBox( - tableName, - encryptionCipher: encryptionKey != null - ? HiveAesCipher(encryptionKey) - : null); - } - } - - @override - Future updateItem(String key, TokenOidcCache newObject) { - return Future.sync(() async { - final boxToken = await openBox(); - return boxToken.put(key, newObject); - }).catchError((error) { - throw error; - }); - } - - @override - Future updateMultipleItem(Map mapObject) { - return Future.sync(() async { - final boxToken = await openBox(); - return boxToken.putAll(mapObject); - }).catchError((error) { - throw error; - }); - } - + bool get encryption => true; } \ No newline at end of file