diff --git a/lib/features/caching/caching_manager.dart b/lib/features/caching/caching_manager.dart index bf1825a90..908c391b6 100644 --- a/lib/features/caching/caching_manager.dart +++ b/lib/features/caching/caching_manager.dart @@ -116,11 +116,11 @@ class CachingManager { } Future _clearSendingEmailCache() async { - final listSendingEmails = await _sendingEmailCacheManager.getAllSendingEmails(needToReopen: true); + final listSendingEmails = await _sendingEmailCacheManager.getAllSendingEmails(); final sendingIds = listSendingEmails.map((sendingEmail) => sendingEmail.sendingId).toSet().toList(); if (sendingIds.isNotEmpty) { await Future.wait( - sendingIds.map((sendingId) => WorkSchedulerController().cancelByUniqueId(sendingId)), + sendingIds.map(WorkSchedulerController().cancelByUniqueId), eagerError: true ); await _sendingEmailCacheManager.clearAllSendingEmails(); diff --git a/lib/features/caching/config/hive_cache_client.dart b/lib/features/caching/config/hive_cache_client.dart index 050991cb2..89ca70241 100644 --- a/lib/features/caching/config/hive_cache_client.dart +++ b/lib/features/caching/config/hive_cache_client.dart @@ -71,25 +71,17 @@ abstract class HiveCacheClient { }); } - Future> getAll({bool needToReopen = false}) { + Future> getAll() { return Future.sync(() async { - if (needToReopen) { - await closeBox(); - } - final boxItem = encryption - ? await openBoxEncryption() - : await openBox(); + final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.values.toList(); }).catchError((error) { throw error; }); } - Future> getListByTupleKey(String accountId, String userName, {bool needToReopen = false}) { + Future> getListByTupleKey(String accountId, String userName) { return Future.sync(() async { - if (needToReopen) { - await closeBox(); - } final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.toMap() .where((key, value) => _matchedKey(key, accountId, userName)) @@ -100,20 +92,27 @@ abstract class HiveCacheClient { }); } + Future> getValuesByListKey(List listKeys) { + return Future.sync(() async { + final boxItem = encryption ? await openBoxEncryption() : await openBox(); + return boxItem.toMap() + .where((key, value) => listKeys.contains(key)) + .values + .toList(); + }).catchError((error) { + throw error; + }); + } + bool _matchedKey(String key, String accountId, String userName) { final keyDecoded = CacheUtils.decodeKey(key); final tupleKey = TupleKey.fromString(keyDecoded); return tupleKey.parts.length >= 3 && tupleKey.parts[1] == accountId && tupleKey.parts[2] == userName; } - Future updateItem(String key, T newObject, {bool needToReopen = false}) { + Future updateItem(String key, T newObject) { return Future.sync(() async { - if (needToReopen) { - await closeBox(); - } - final boxItem = encryption - ? await openBoxEncryption() - : await openBox(); + final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.put(key, newObject); }).catchError((error) { throw error; @@ -122,24 +121,16 @@ abstract class HiveCacheClient { Future updateMultipleItem(Map mapObject) { return Future.sync(() async { - final boxItem = encryption - ? await openBoxEncryption() - : await openBox(); + final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.putAll(mapObject); }).catchError((error) { throw error; }); } - Future deleteItem(String key, {bool needToReopen = false}) { + Future deleteItem(String key) { return Future.sync(() async { - if (needToReopen) { - await closeBox(); - } - - final boxItem = encryption - ? await openBoxEncryption() - : await openBox(); + final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.delete(key); }).catchError((error) { throw error;