TF-1839 Clean SendingEmail in cache

(cherry picked from commit 2bf35cb26095d7cebb9e722722c209878162805f)
This commit is contained in:
dab246
2023-06-05 11:42:24 +07:00
committed by Dat Vu
parent f121e0c719
commit c70a388732
4 changed files with 41 additions and 7 deletions
+28 -4
View File
@@ -14,6 +14,8 @@ import 'package:tmail_ui_user/features/caching/clients/state_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/subscription_cache_client.dart'; import 'package:tmail_ui_user/features/caching/clients/subscription_cache_client.dart';
import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart'; import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/state_type.dart'; import 'package:tmail_ui_user/features/mailbox/data/model/state_type.dart';
import 'package:tmail_ui_user/features/offline_mode/controller/work_scheduler_controller.dart';
import 'package:tmail_ui_user/features/offline_mode/manager/sending_email_cache_manager.dart';
class CachingManager { class CachingManager {
final MailboxCacheClient _mailboxCacheClient; final MailboxCacheClient _mailboxCacheClient;
@@ -27,6 +29,7 @@ class CachingManager {
final DetailedEmailHiveCacheClient _detailedEmailHiveCacheClient; final DetailedEmailHiveCacheClient _detailedEmailHiveCacheClient;
final OpenedEmailHiveCacheClient _openedEmailHiveCacheClient; final OpenedEmailHiveCacheClient _openedEmailHiveCacheClient;
final FileUtils _fileUtils; final FileUtils _fileUtils;
final SendingEmailCacheManager _sendingEmailCacheManager;
CachingManager( CachingManager(
this._mailboxCacheClient, this._mailboxCacheClient,
@@ -40,6 +43,7 @@ class CachingManager {
this._detailedEmailHiveCacheClient, this._detailedEmailHiveCacheClient,
this._openedEmailHiveCacheClient, this._openedEmailHiveCacheClient,
this._fileUtils, this._fileUtils,
this._sendingEmailCacheManager,
); );
Future<void> clearAll() async { Future<void> clearAll() async {
@@ -51,8 +55,12 @@ class CachingManager {
_fcmSubscriptionCacheClient.clearAllData(), _fcmSubscriptionCacheClient.clearAllData(),
_recentSearchCacheClient.clearAllData(), _recentSearchCacheClient.clearAllData(),
_accountCacheClient.clearAllData(), _accountCacheClient.clearAllData(),
_detailedEmailHiveCacheClient.clearAllData(), if (PlatformInfo.isMobile)
_openedEmailHiveCacheClient.clearAllData(), ...[
_detailedEmailHiveCacheClient.clearAllData(),
_openedEmailHiveCacheClient.clearAllData(),
_clearSendingEmailCache(),
]
], eagerError: true); ], eagerError: true);
} }
@@ -64,8 +72,12 @@ class CachingManager {
_fcmCacheClient.clearAllData(), _fcmCacheClient.clearAllData(),
_fcmSubscriptionCacheClient.clearAllData(), _fcmSubscriptionCacheClient.clearAllData(),
_recentSearchCacheClient.clearAllData(), _recentSearchCacheClient.clearAllData(),
_detailedEmailHiveCacheClient.clearAllData(), if (PlatformInfo.isMobile)
_openedEmailHiveCacheClient.clearAllData(), ...[
_detailedEmailHiveCacheClient.clearAllData(),
_openedEmailHiveCacheClient.clearAllData(),
_clearSendingEmailCache(),
]
], eagerError: true); ], eagerError: true);
} }
@@ -103,4 +115,16 @@ class CachingManager {
_fileUtils.removeFolder(CachingConstants.openedEmailContentFolderName); _fileUtils.removeFolder(CachingConstants.openedEmailContentFolderName);
} }
} }
Future<void> _clearSendingEmailCache() async {
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)),
eagerError: true
);
await _sendingEmailCacheManager.clearAllSendingEmails();
}
}
} }
@@ -256,7 +256,7 @@ class EmailHiveCacheDataSourceImpl extends EmailDataSource {
@override @override
Future<List<SendingEmail>> getAllSendingEmails(AccountId accountId, UserName userName, {bool needToReopen = false}) { Future<List<SendingEmail>> getAllSendingEmails(AccountId accountId, UserName userName, {bool needToReopen = false}) {
return Future.sync(() async { return Future.sync(() async {
final sendingEmailsCache = await _sendingEmailCacheManager.getAllSendingEmails(accountId, userName, needToReopen: needToReopen); final sendingEmailsCache = await _sendingEmailCacheManager.getAllSendingEmailsByTupleKey(accountId, userName, needToReopen: needToReopen);
return sendingEmailsCache.toSendingEmails(); return sendingEmailsCache.toSendingEmails();
}).catchError(_exceptionThrower.throwException); }).catchError(_exceptionThrower.throwException);
} }
@@ -24,7 +24,7 @@ class SendingEmailCacheManager {
return _hiveCacheClient.insertItem(keyCache, sendingEmailHiveCache); return _hiveCacheClient.insertItem(keyCache, sendingEmailHiveCache);
} }
Future<List<SendingEmailHiveCache>> getAllSendingEmails( Future<List<SendingEmailHiveCache>> getAllSendingEmailsByTupleKey(
AccountId accountId, AccountId accountId,
UserName userName, UserName userName,
{bool needToReopen = false} {bool needToReopen = false}
@@ -33,7 +33,7 @@ class SendingEmailCacheManager {
accountId.asString, accountId.asString,
userName.value, userName.value,
needToReopen: needToReopen); needToReopen: needToReopen);
log('SendingEmailCacheManager::getAllSendingEmails():COUNT: ${sendingEmailsCache.length}'); log('SendingEmailCacheManager::getAllSendingEmailsByTupleKey():COUNT: ${sendingEmailsCache.length}');
sendingEmailsCache.sortByLatestTime(); sendingEmailsCache.sortByLatestTime();
return sendingEmailsCache; return sendingEmailsCache;
} }
@@ -47,4 +47,13 @@ class SendingEmailCacheManager {
log('SendingEmailCacheManager::deleteSendingEmail():keyCache: $keyCache'); log('SendingEmailCacheManager::deleteSendingEmail():keyCache: $keyCache');
return _hiveCacheClient.deleteItem(keyCache); return _hiveCacheClient.deleteItem(keyCache);
} }
Future<List<SendingEmailHiveCache>> getAllSendingEmails() async {
final sendingEmailsCache = await _hiveCacheClient.getAll(needToReopen: true);
log('SendingEmailCacheManager::getAllSendingEmails():COUNT: ${sendingEmailsCache.length}');
sendingEmailsCache.sortByLatestTime();
return sendingEmailsCache;
}
Future<void> clearAllSendingEmails() => _hiveCacheClient.clearAllData();
} }
@@ -92,6 +92,7 @@ class LocalBindings extends Bindings {
Get.find<DetailedEmailHiveCacheClient>(), Get.find<DetailedEmailHiveCacheClient>(),
Get.find<OpenedEmailHiveCacheClient>(), Get.find<OpenedEmailHiveCacheClient>(),
Get.find<FileUtils>(), Get.find<FileUtils>(),
Get.find<SendingEmailCacheManager>(),
)); ));
Get.put(SharePreferenceSpamReportDataSource(Get.find<SharedPreferences>())); Get.put(SharePreferenceSpamReportDataSource(Get.find<SharedPreferences>()));
} }