TF-1923 Fix logic get stored email when offline

(cherry picked from commit fa74abdf2663e80e445236449c5aec3e5c9267e5)
This commit is contained in:
dab246
2023-06-20 22:28:01 +07:00
committed by Dat H. Pham
parent 99a6a64899
commit 002a543865
8 changed files with 28 additions and 29 deletions
@@ -82,7 +82,7 @@ abstract class EmailDataSource {
Future<void> storeEmail(Session session, AccountId accountId, Email email); Future<void> storeEmail(Session session, AccountId accountId, Email email);
Future<Email?> getEmailStored(Session session, AccountId accountId, EmailId emailId); Future<Email> getStoredEmail(Session session, AccountId accountId, EmailId emailId);
Future<void> storeOpenedEmail(Session session, AccountId accountId, DetailedEmail detailedEmail); Future<void> storeOpenedEmail(Session session, AccountId accountId, DetailedEmail detailedEmail);
@@ -185,7 +185,7 @@ class EmailDataSourceImpl extends EmailDataSource {
} }
@override @override
Future<Email?> getEmailStored(Session session, AccountId accountId, EmailId emailId) { Future<Email> getStoredEmail(Session session, AccountId accountId, EmailId emailId) {
throw UnimplementedError(); throw UnimplementedError();
} }
@@ -229,11 +229,10 @@ class EmailHiveCacheDataSourceImpl extends EmailDataSource {
} }
@override @override
Future<Email?> getEmailStored(Session session, AccountId accountId, EmailId emailId) { Future<Email> getStoredEmail(Session session, AccountId accountId, EmailId emailId) {
return Future.sync(() async { return Future.sync(() async {
final email = await _emailCacheManager.getEmailFromCache(accountId, session.username, emailId); final email = await _emailCacheManager.getStoredEmail(accountId, session.username, emailId);
log('EmailHiveCacheDataSourceImpl::getEmailFromCache():emailId: $email'); return email.toEmail();
return email?.toEmail();
}).catchError(_exceptionThrower.throwException); }).catchError(_exceptionThrower.throwException);
} }
@@ -210,8 +210,8 @@ class EmailRepositoryImpl extends EmailRepository {
} }
@override @override
Future<Email?> getEmailStored(Session session, AccountId accountId, EmailId emailId) { Future<Email> getStoredEmail(Session session, AccountId accountId, EmailId emailId) {
return emailDataSource[DataSourceType.hiveCache]!.getEmailStored(session, accountId, emailId); return emailDataSource[DataSourceType.hiveCache]!.getStoredEmail(session, accountId, emailId);
} }
@override @override
@@ -1,4 +1,6 @@
class NotFoundStoredOpenedEmailException implements Exception {} class NotFoundStoredOpenedEmailException implements Exception {}
class NotFoundStoredNewEmailException implements Exception {} class NotFoundStoredNewEmailException implements Exception {}
class NotFoundStoredEmailException implements Exception {}
@@ -94,7 +94,7 @@ abstract class EmailRepository {
Future<void> storeEmail(Session session, AccountId accountId, Email email); Future<void> storeEmail(Session session, AccountId accountId, Email email);
Future<Email?> getEmailStored(Session session, AccountId accountId, EmailId emailId); Future<Email> getStoredEmail(Session session, AccountId accountId, EmailId emailId);
Future<void> storeOpenedEmail(Session session, AccountId accountId, DetailedEmail detailedEmail); Future<void> storeOpenedEmail(Session session, AccountId accountId, DetailedEmail detailedEmail);
@@ -7,6 +7,7 @@ import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/caching/clients/email_cache_client.dart'; import 'package:tmail_ui_user/features/caching/clients/email_cache_client.dart';
import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart'; import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/email_cleanup_rule.dart'; import 'package:tmail_ui_user/features/cleanup/domain/model/email_cleanup_rule.dart';
import 'package:tmail_ui_user/features/email/domain/exceptions/email_cache_exceptions.dart';
import 'package:tmail_ui_user/features/thread/data/extensions/email_cache_extension.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/extensions/email_extension.dart';
import 'package:tmail_ui_user/features/thread/data/extensions/list_email_cache_extension.dart'; import 'package:tmail_ui_user/features/thread/data/extensions/list_email_cache_extension.dart';
@@ -97,8 +98,13 @@ class EmailCacheManager {
return _emailCacheClient.insertItem(keyCache, emailCache); return _emailCacheClient.insertItem(keyCache, emailCache);
} }
Future<EmailCache?> getEmailFromCache(AccountId accountId, UserName userName, EmailId emailId) { Future<EmailCache> getStoredEmail(AccountId accountId, UserName userName, EmailId emailId) async {
final keyCache = TupleKey(emailId.asString, accountId.asString, userName.value).encodeKey; final keyCache = TupleKey(emailId.asString, accountId.asString, userName.value).encodeKey;
return _emailCacheClient.getItem(keyCache, needToReopen: true); final emailCache = await _emailCacheClient.getItem(keyCache, needToReopen: true);
if (emailCache != null) {
return emailCache;
} else {
throw NotFoundStoredEmailException();
}
} }
} }
@@ -1,7 +1,6 @@
import 'dart:io';
import 'package:core/presentation/state/failure.dart'; import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart'; import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
import 'package:core/utils/platform_info.dart'; import 'package:core/utils/platform_info.dart';
import 'package:dartz/dartz.dart'; import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/account_id.dart';
@@ -30,11 +29,12 @@ class GetEmailByIdInteractor {
try { try {
yield Right<Failure, Success>(GetEmailByIdLoading()); yield Right<Failure, Success>(GetEmailByIdLoading());
if (PlatformInfo.isMobile) { if (PlatformInfo.isMobile) {
yield* _tryToGetEmailFromCache(session, accountId, emailId, properties: properties); yield* _getStoredEmail(session, accountId, emailId, properties: properties);
} else { } else {
yield* _getEmailByIdFromServer(session, accountId, emailId, properties: properties); yield* _getEmailByIdFromServer(session, accountId, emailId, properties: properties);
} }
} catch (e) { } catch (e) {
logError('GetEmailByIdInteractor::execute():EXCEPTION: $e');
yield Left<Failure, Success>(GetEmailByIdFailure(e)); yield Left<Failure, Success>(GetEmailByIdFailure(e));
} }
} }
@@ -51,12 +51,13 @@ class GetEmailByIdInteractor {
final email = await _threadRepository.getEmailById(session, accountId, emailId, properties: properties); final email = await _threadRepository.getEmailById(session, accountId, emailId, properties: properties);
yield Right<Failure, Success>(GetEmailByIdSuccess(email)); yield Right<Failure, Success>(GetEmailByIdSuccess(email));
} catch (e) { } catch (e) {
logError('GetEmailByIdInteractor::_getEmailByIdFromServer():EXCEPTION: $e');
yield Left<Failure, Success>(GetEmailByIdFailure(e)); yield Left<Failure, Success>(GetEmailByIdFailure(e));
} }
} }
Stream<Either<Failure, Success>> _tryToGetEmailFromCache( Stream<Either<Failure, Success>> _getStoredEmail(
Session session, Session session,
AccountId accountId, AccountId accountId,
EmailId emailId, EmailId emailId,
@@ -65,20 +66,11 @@ class GetEmailByIdInteractor {
} }
) async* { ) async* {
try { try {
final email = await _emailRepository.getStoredEmail(session, accountId, emailId);
final email = await _emailRepository.getEmailStored(session, accountId, emailId); yield Right<Failure, Success>(GetEmailByIdSuccess(email.toPresentationEmail()));
if (email != null) {
yield Right<Failure, Success>(GetEmailByIdSuccess(email.toPresentationEmail()));
} else {
yield* _getEmailByIdFromServer(session, accountId, emailId, properties: properties);
}
} catch (e) { } catch (e) {
if (e is PathNotFoundException) { logError('GetEmailByIdInteractor::_tryToGetEmailFromCache():EXCEPTION: $e');
yield* _getEmailByIdFromServer(session, accountId, emailId, properties: properties); yield* _getEmailByIdFromServer(session, accountId, emailId, properties: properties);
} else {
yield Left<Failure, Success>(GetEmailByIdFailure(e));
}
} }
} }
} }