From 109ea54204bbb6d65bdac4ae34615d438c77ce23 Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Tue, 23 May 2023 18:05:04 +0700 Subject: [PATCH] TF-1869: Duplicate check for OpenedEmail and DetailedEmail when get contents (cherry picked from commit 674312c3dd515b5699c84d3572bdc7bed9e7415d) --- .../caching/utils/caching_constants.dart | 2 +- .../data/datasource/email_datasource.dart | 4 ++ .../email_datasource_impl.dart | 10 +++++ .../email_hive_cache_datasource_impl.dart | 44 +++++++++++++++++-- .../extensions/detailed_email_extension.dart | 2 +- .../data/local/email_cache_manager.dart | 5 +++ 6 files changed, 61 insertions(+), 6 deletions(-) diff --git a/lib/features/caching/utils/caching_constants.dart b/lib/features/caching/utils/caching_constants.dart index 2dcbe0bf5..08c55038c 100644 --- a/lib/features/caching/utils/caching_constants.dart +++ b/lib/features/caching/utils/caching_constants.dart @@ -21,7 +21,7 @@ class CachingConstants { static const String fcmCacheBoxName = 'fcm_cache_box'; static const String detailedEmailCacheBoxName = 'detailed_email_cache_box'; static const String newEmailContentFolderName = 'new_email'; - static const String openedEmailContentFolderNamee = 'opened_email'; + static const String openedEmailContentFolderName = 'opened_email'; static const String openedEmailCacheBoxName = 'opened_email_cache_box'; static const int maxNumberNewEmailsForOffline = 10; diff --git a/lib/features/email/data/datasource/email_datasource.dart b/lib/features/email/data/datasource/email_datasource.dart index c9ebff048..19703368d 100644 --- a/lib/features/email/data/datasource/email_datasource.dart +++ b/lib/features/email/data/datasource/email_datasource.dart @@ -80,7 +80,11 @@ abstract class EmailDataSource { Future storeEmail(Session session, AccountId accountId, Email email); + Future getEmailFromCache(Session session, AccountId accountId, EmailId emailId); + Future storeOpenedEmail(Session session, AccountId accountId, DetailedEmail detailedEmail); Future getOpenedEmail(Session session, AccountId accountId, EmailId emailId); + + Future getDetailedEmail(Session session, AccountId accountId, EmailId emailId); } \ No newline at end of file diff --git a/lib/features/email/data/datasource_impl/email_datasource_impl.dart b/lib/features/email/data/datasource_impl/email_datasource_impl.dart index bb2cc8fb3..a48c07ac8 100644 --- a/lib/features/email/data/datasource_impl/email_datasource_impl.dart +++ b/lib/features/email/data/datasource_impl/email_datasource_impl.dart @@ -176,4 +176,14 @@ class EmailDataSourceImpl extends EmailDataSource { Future getOpenedEmail(Session session, AccountId accountId, EmailId emailId) { throw UnimplementedError(); } + + @override + Future getDetailedEmail(Session session, AccountId accountId, EmailId emailId) { + throw UnimplementedError(); + } + + @override + Future getEmailFromCache(Session session, AccountId accountId, EmailId emailId) { + throw UnimplementedError(); + } } \ No newline at end of file diff --git a/lib/features/email/data/datasource_impl/email_hive_cache_datasource_impl.dart b/lib/features/email/data/datasource_impl/email_hive_cache_datasource_impl.dart index c94a05b3d..5c322d633 100644 --- a/lib/features/email/data/datasource_impl/email_hive_cache_datasource_impl.dart +++ b/lib/features/email/data/datasource_impl/email_hive_cache_datasource_impl.dart @@ -32,6 +32,7 @@ import 'package:tmail_ui_user/features/offline_mode/manager/detailed_email_cache import 'package:tmail_ui_user/features/offline_mode/manager/opened_email_cache_manager.dart'; import 'package:tmail_ui_user/features/offline_mode/manager/opened_email_cache_worker_queue.dart'; import 'package:tmail_ui_user/features/offline_mode/worker/hive_task.dart'; +import 'package:tmail_ui_user/features/thread/data/extensions/email_cache_extension.dart'; import 'package:tmail_ui_user/features/thread/data/extensions/email_extension.dart'; import 'package:tmail_ui_user/features/thread/data/local/email_cache_manager.dart'; import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart'; @@ -192,14 +193,49 @@ class EmailHiveCacheDataSourceImpl extends EmailDataSource { @override Future getOpenedEmail(Session session, AccountId accountId, EmailId emailId) { return Future.sync(() async { - final detailedEmailHiveCache = await _openedEmailCacheManager.getDetailEmailExistedInCache(accountId, session.username, emailId); + final openedEmailHiveCache = await _openedEmailCacheManager.getOpenedEmailExistedInCache(accountId, session.username, emailId); + + if (openedEmailHiveCache == null) { + return null; + } + + log('EmailHiveCacheDataSourceImpl::getOpenedEmail():folderPath: ${openedEmailHiveCache.emailContentPath}'); final emailContent = await _fileUtils.getContentFromFile( - nameFile: emailId.asString, - folderPath: CachingConstants.openedEmailContentFolderNamee + nameFile: emailId.asString, + folderPath: CachingConstants.openedEmailContentFolderName ); - return detailedEmailHiveCache?.toDetailedEmailWithContent(emailContent); + return openedEmailHiveCache.toDetailedEmailWithContent(emailContent); + }).catchError(_exceptionThrower.throwException); + } + + @override + Future getDetailedEmail(Session session, AccountId accountId, EmailId emailId) { + return Future.sync(() async { + final detailedEmailHiveCache = await _detailedEmailCacheManager.getDetailEmailExistedInCache(accountId, session.username, emailId); + + if (detailedEmailHiveCache == null) { + return null; + } + log('EmailHiveCacheDataSourceImpl::getDetailedEmail():folderPath: ${detailedEmailHiveCache.emailContentPath}'); + + final emailContent = await _fileUtils.getContentFromFile( + nameFile: emailId.asString, + folderPath: CachingConstants.newEmailContentFolderName + ); + + return detailedEmailHiveCache.toDetailedEmailWithContent(emailContent); + }).catchError(_exceptionThrower.throwException); + } + + @override + Future getEmailFromCache(Session session, AccountId accountId, EmailId emailId) { + return Future.sync(() async { + log('EmailHiveCacheDataSourceImpl::getEmailFromCache():emailId: ${emailId.asString}'); + final email = await _emailCacheManager.getEmailFromCache(accountId, session.username, emailId); + log('EmailHiveCacheDataSourceImpl::getEmailFromCache():emailId: $email'); + return email?.toEmail(); }).catchError(_exceptionThrower.throwException); } } \ No newline at end of file diff --git a/lib/features/email/domain/extensions/detailed_email_extension.dart b/lib/features/email/domain/extensions/detailed_email_extension.dart index 207fd5e5c..47bd2b2be 100644 --- a/lib/features/email/domain/extensions/detailed_email_extension.dart +++ b/lib/features/email/domain/extensions/detailed_email_extension.dart @@ -19,7 +19,7 @@ extension DetailedEmailExtension on DetailedEmail { String get newEmailFolderPath => CachingConstants.newEmailContentFolderName; - String get openedEmailFolderPath => CachingConstants.openedEmailContentFolderNamee; + String get openedEmailFolderPath => CachingConstants.openedEmailContentFolderName; DetailedEmail fromEmailContentPath(String path) { return DetailedEmail( diff --git a/lib/features/thread/data/local/email_cache_manager.dart b/lib/features/thread/data/local/email_cache_manager.dart index d6fd3647d..f1242e990 100644 --- a/lib/features/thread/data/local/email_cache_manager.dart +++ b/lib/features/thread/data/local/email_cache_manager.dart @@ -96,4 +96,9 @@ class EmailCacheManager { final keyCache = TupleKey(emailCache.id, accountId.asString, userName.value).encodeKey; return _emailCacheClient.insertItem(keyCache, emailCache); } + + Future getEmailFromCache(AccountId accountId, UserName userName, EmailId emailId) { + final keyCache = TupleKey(emailId.asString, accountId.asString, userName.value).encodeKey; + return _emailCacheClient.getItem(keyCache); + } } \ No newline at end of file