From 1bf7ee4f2078dd4555b83b6ded5003c17f037135 Mon Sep 17 00:00:00 2001 From: dab246 Date: Wed, 7 Feb 2024 01:24:15 +0700 Subject: [PATCH] TF-2564 Change logic empty folder (Spam/Trash) to avoid cache still has data Signed-off-by: dab246 --- .../data/datasource/thread_datasource.dart | 1 - .../local_thread_datasource_impl.dart | 1 - .../thread_datasource_impl.dart | 6 ++-- .../data/local/email_cache_manager.dart | 26 +++++++------- .../data/network/thread_isolate_worker.dart | 33 ++++++----------- .../repository/thread_repository_impl.dart | 36 ++++++++++--------- 6 files changed, 44 insertions(+), 59 deletions(-) diff --git a/lib/features/thread/data/datasource/thread_datasource.dart b/lib/features/thread/data/datasource/thread_datasource.dart index aa602acc3..6c324c855 100644 --- a/lib/features/thread/data/datasource/thread_datasource.dart +++ b/lib/features/thread/data/datasource/thread_datasource.dart @@ -55,7 +55,6 @@ abstract class ThreadDataSource { Session session, AccountId accountId, MailboxId mailboxId, - Future Function(List? newDestroyed) updateDestroyedEmailCache, ); Future getEmailById(Session session, AccountId accountId, EmailId emailId, {Properties? properties}); diff --git a/lib/features/thread/data/datasource_impl/local_thread_datasource_impl.dart b/lib/features/thread/data/datasource_impl/local_thread_datasource_impl.dart index 21d6f9987..b18202dab 100644 --- a/lib/features/thread/data/datasource_impl/local_thread_datasource_impl.dart +++ b/lib/features/thread/data/datasource_impl/local_thread_datasource_impl.dart @@ -96,7 +96,6 @@ class LocalThreadDataSourceImpl extends ThreadDataSource { Session session, AccountId accountId, MailboxId mailboxId, - Future Function(List? newDestroyed) updateDestroyedEmailCache ) { throw UnimplementedError(); } diff --git a/lib/features/thread/data/datasource_impl/thread_datasource_impl.dart b/lib/features/thread/data/datasource_impl/thread_datasource_impl.dart index f7933b068..1f1974c2a 100644 --- a/lib/features/thread/data/datasource_impl/thread_datasource_impl.dart +++ b/lib/features/thread/data/datasource_impl/thread_datasource_impl.dart @@ -90,15 +90,13 @@ class ThreadDataSourceImpl extends ThreadDataSource { Future> emptyMailboxFolder( Session session, AccountId accountId, - MailboxId mailboxId, - Future Function(List? newDestroyed) updateDestroyedEmailCache + MailboxId mailboxId ) { return Future.sync(() async { return await _threadIsolateWorker.emptyMailboxFolder( session, accountId, - mailboxId, - updateDestroyedEmailCache, + mailboxId ); }).catchError(_exceptionThrower.throwException); } diff --git a/lib/features/thread/data/local/email_cache_manager.dart b/lib/features/thread/data/local/email_cache_manager.dart index 43ee5bb25..e67466a9f 100644 --- a/lib/features/thread/data/local/email_cache_manager.dart +++ b/lib/features/thread/data/local/email_cache_manager.dart @@ -65,21 +65,21 @@ class EmailCacheManager { List? created, List? destroyed }) async { - final emailCacheExist = await _emailCacheClient.isExistTable(); - if (emailCacheExist) { - final updatedCacheEmails = updated?.toMapCache(accountId, userName) ?? {}; - final createdCacheEmails = created?.toMapCache(accountId, userName) ?? {}; - final destroyedCacheEmails = destroyed?.toCacheKeyList(accountId, userName) ?? []; - - await Future.wait([ - _emailCacheClient.updateMultipleItem(updatedCacheEmails), - _emailCacheClient.insertMultipleItem(createdCacheEmails), - _emailCacheClient.deleteMultipleItem(destroyedCacheEmails) - ]); - } else { - final createdCacheEmails = created?.toMapCache(accountId, userName) ?? {}; + if (created?.isNotEmpty == true) { + final createdCacheEmails = created!.toMapCache(accountId, userName); await _emailCacheClient.insertMultipleItem(createdCacheEmails); } + + if (updated?.isNotEmpty == true) { + final updatedCacheEmails = updated!.toMapCache(accountId, userName); + await _emailCacheClient.updateMultipleItem(updatedCacheEmails); + } + + final emailCacheExist = await _emailCacheClient.isExistTable(); + if (destroyed?.isNotEmpty == true && emailCacheExist) { + final destroyedCacheEmails = destroyed!.toCacheKeyList(accountId, userName); + await _emailCacheClient.deleteMultipleItem(destroyedCacheEmails); + } } Future clean(EmailCleanupRule cleanupRule) async { diff --git a/lib/features/thread/data/network/thread_isolate_worker.dart b/lib/features/thread/data/network/thread_isolate_worker.dart index 24872ac02..0157acfda 100644 --- a/lib/features/thread/data/network/thread_isolate_worker.dart +++ b/lib/features/thread/data/network/thread_isolate_worker.dart @@ -33,10 +33,9 @@ class ThreadIsolateWorker { Session session, AccountId accountId, MailboxId mailboxId, - Future Function(List? newDestroyed) updateDestroyedEmailCache, ) async { if (PlatformInfo.isWeb) { - return _emptyMailboxFolderOnWeb(session, accountId, mailboxId, updateDestroyedEmailCache); + return _emptyMailboxFolderOnWeb(session, accountId, mailboxId); } else { final rootIsolateToken = RootIsolateToken.instance; if (rootIsolateToken == null) { @@ -52,13 +51,7 @@ class ThreadIsolateWorker { mailboxId, rootIsolateToken ), - fun1: _emptyMailboxFolderAction, - notification: (value) { - if (value is List) { - updateDestroyedEmailCache.call(value); - log('ThreadIsolateWorker::emptyMailboxFolder(): onUpdateProgress: PERCENT ${value.length}'); - } - } + fun1: _emptyMailboxFolderAction ); if (result.isEmpty) { @@ -102,14 +95,11 @@ class ThreadIsolateWorker { if (newEmailList.isNotEmpty) { lastEmail = newEmailList.last; hasEmails = true; - final listEmailIdDeleted = await args.emailAPI.deleteMultipleEmailsPermanently(args.session, args.accountId, newEmailList.listEmailIds); - - if (listEmailIdDeleted.isNotEmpty && listEmailIdDeleted.length == newEmailList.listEmailIds.length) { - sendPort.send(listEmailIdDeleted); - } + final listEmailIdDeleted = await args.emailAPI.deleteMultipleEmailsPermanently( + args.session, + args.accountId, + newEmailList.listEmailIds); emailListCompleted.addAll(listEmailIdDeleted); - - sendPort.send(emailListCompleted); } else { hasEmails = false; } @@ -125,7 +115,6 @@ class ThreadIsolateWorker { Session session, AccountId accountId, MailboxId mailboxId, - Future Function(List newDestroyed) updateDestroyedEmailCache, ) async { List emailListCompleted = List.empty(growable: true); try { @@ -152,13 +141,11 @@ class ThreadIsolateWorker { if (newEmailList.isNotEmpty) { lastEmail = newEmailList.last; hasEmails = true; - final listEmailIdDeleted = await _emailAPI.deleteMultipleEmailsPermanently(session, accountId, newEmailList.listEmailIds); - - if (listEmailIdDeleted.isNotEmpty && listEmailIdDeleted.length == newEmailList.listEmailIds.length) { - await updateDestroyedEmailCache(listEmailIdDeleted); - } + final listEmailIdDeleted = await _emailAPI.deleteMultipleEmailsPermanently( + session, + accountId, + newEmailList.listEmailIds); emailListCompleted.addAll(listEmailIdDeleted); - } else { hasEmails = false; } diff --git a/lib/features/thread/data/repository/thread_repository_impl.dart b/lib/features/thread/data/repository/thread_repository_impl.dart index a3721f83e..6a29faf5b 100644 --- a/lib/features/thread/data/repository/thread_repository_impl.dart +++ b/lib/features/thread/data/repository/thread_repository_impl.dart @@ -309,14 +309,17 @@ class ThreadRepositoryImpl extends ThreadRepository { @override Future> emptyTrashFolder(Session session, AccountId accountId, MailboxId trashMailboxId) async { - return mapDataSource[DataSourceType.network]!.emptyMailboxFolder( + final listEmailIdDeleted = await mapDataSource[DataSourceType.network]!.emptyMailboxFolder( session, accountId, - trashMailboxId, - (listEmailIdDeleted) async { - await _updateEmailCache(accountId, session.username, newDestroyed: listEmailIdDeleted); - }, - ); + trashMailboxId); + + await _updateEmailCache( + accountId, + session.username, + newDestroyed: listEmailIdDeleted); + + return listEmailIdDeleted; } Future _synchronizeCacheWithChanges( @@ -388,18 +391,17 @@ class ThreadRepositoryImpl extends ThreadRepository { } @override - Future> emptySpamFolder(Session session, AccountId accountId, MailboxId spamMailboxId) { - return mapDataSource[DataSourceType.network]!.emptyMailboxFolder( + Future> emptySpamFolder(Session session, AccountId accountId, MailboxId spamMailboxId) async { + final listEmailIdDeleted = await mapDataSource[DataSourceType.network]!.emptyMailboxFolder( session, accountId, - spamMailboxId, - (listEmailIdDeleted) async { - await _updateEmailCache( - accountId, - session.username, - newDestroyed: listEmailIdDeleted - ); - }, - ); + spamMailboxId); + + await _updateEmailCache( + accountId, + session.username, + newDestroyed: listEmailIdDeleted); + + return listEmailIdDeleted; } } \ No newline at end of file