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
@@ -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/utils/cache_utils.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_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);
}
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;
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/success.dart';
import 'package:core/utils/app_logger.dart';
import 'package:core/utils/platform_info.dart';
import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
@@ -30,11 +29,12 @@ class GetEmailByIdInteractor {
try {
yield Right<Failure, Success>(GetEmailByIdLoading());
if (PlatformInfo.isMobile) {
yield* _tryToGetEmailFromCache(session, accountId, emailId, properties: properties);
yield* _getStoredEmail(session, accountId, emailId, properties: properties);
} else {
yield* _getEmailByIdFromServer(session, accountId, emailId, properties: properties);
}
} catch (e) {
logError('GetEmailByIdInteractor::execute():EXCEPTION: $e');
yield Left<Failure, Success>(GetEmailByIdFailure(e));
}
}
@@ -51,12 +51,13 @@ class GetEmailByIdInteractor {
final email = await _threadRepository.getEmailById(session, accountId, emailId, properties: properties);
yield Right<Failure, Success>(GetEmailByIdSuccess(email));
} catch (e) {
logError('GetEmailByIdInteractor::_getEmailByIdFromServer():EXCEPTION: $e');
yield Left<Failure, Success>(GetEmailByIdFailure(e));
}
}
Stream<Either<Failure, Success>> _tryToGetEmailFromCache(
Stream<Either<Failure, Success>> _getStoredEmail(
Session session,
AccountId accountId,
EmailId emailId,
@@ -65,20 +66,11 @@ class GetEmailByIdInteractor {
}
) async* {
try {
final email = await _emailRepository.getEmailStored(session, accountId, emailId);
if (email != null) {
yield Right<Failure, Success>(GetEmailByIdSuccess(email.toPresentationEmail()));
} else {
yield* _getEmailByIdFromServer(session, accountId, emailId, properties: properties);
}
final email = await _emailRepository.getStoredEmail(session, accountId, emailId);
yield Right<Failure, Success>(GetEmailByIdSuccess(email.toPresentationEmail()));
} catch (e) {
if (e is PathNotFoundException) {
yield* _getEmailByIdFromServer(session, accountId, emailId, properties: properties);
} else {
yield Left<Failure, Success>(GetEmailByIdFailure(e));
}
logError('GetEmailByIdInteractor::_tryToGetEmailFromCache():EXCEPTION: $e');
yield* _getEmailByIdFromServer(session, accountId, emailId, properties: properties);
}
}
}