TF-1809: Handle sync data from cache and network when get email contents

(cherry picked from commit 896059cd5b29c62c2cf34621fe35d7b23a54acf4)
This commit is contained in:
HuyNguyen
2023-05-18 11:56:24 +07:00
committed by Dat Vu
parent b17e4e4728
commit 48f5a8b51f
19 changed files with 205 additions and 135 deletions
@@ -1,3 +1,5 @@
import 'dart:io';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
@@ -28,37 +30,85 @@ class GetEmailContentInteractor {
) async* {
try {
yield Right<Failure, Success>(GetEmailContentLoading());
final email = await emailRepository.getEmailContent(session, accountId, emailId);
if (email.emailContentList.isNotEmpty) {
final newEmailContents = await emailRepository.transformEmailContent(
email.emailContentList,
email.allAttachments.listAttachmentsDisplayedInContent,
baseDownloadUrl,
accountId,
draftsEmail: draftsEmail
);
final newEmailContentsDisplayed = BuildUtils.isWeb && !composeEmail
? await emailRepository.addTooltipWhenHoverOnLink(newEmailContents)
: newEmailContents;
yield Right<Failure, Success>(GetEmailContentSuccess(
newEmailContents,
newEmailContentsDisplayed,
email.allAttachments,
email
));
} else if (email.allAttachments.isNotEmpty) {
yield Right<Failure, Success>(GetEmailContentSuccess([], [], email.allAttachments, email));
} else if (email.headers?.isNotEmpty == true) {
yield Right<Failure, Success>(GetEmailContentSuccess([], [], [], email));
if (!BuildUtils.isWeb) {
yield* _tryToGetOpenedEmailCache(session, accountId, emailId, baseDownloadUrl, composeEmail: composeEmail, draftsEmail: draftsEmail);
} else {
yield Left(GetEmailContentFailure(null));
yield* _getContentEmailFromServer(session, accountId, emailId, baseDownloadUrl, composeEmail: composeEmail, draftsEmail: draftsEmail);
}
} catch (e) {
log('GetEmailContentInteractor::execute(): exception = $e');
yield Left(GetEmailContentFailure(e));
}
}
Stream<Either<Failure, Success>> _getContentEmailFromServer(
Session session,
AccountId accountId,
EmailId emailId,
String? baseDownloadUrl,
{
bool composeEmail = false,
bool draftsEmail = false
}
) async* {
final email = await emailRepository.getEmailContent(session, accountId, emailId);
if (email.emailContentList.isNotEmpty) {
final newEmailContents = await emailRepository.transformEmailContent(
email.emailContentList,
email.allAttachments.listAttachmentsDisplayedInContent,
baseDownloadUrl,
accountId,
draftsEmail: draftsEmail
);
final newEmailContentsDisplayed = BuildUtils.isWeb && !composeEmail
? await emailRepository.addTooltipWhenHoverOnLink(newEmailContents)
: newEmailContents;
yield Right<Failure, Success>(GetEmailContentSuccess(
newEmailContents,
newEmailContentsDisplayed,
email.allAttachments,
email
));
} else if (email.allAttachments.isNotEmpty) {
yield Right<Failure, Success>(GetEmailContentSuccess([], [], email.allAttachments, email));
} else if (email.headers?.isNotEmpty == true) {
yield Right<Failure, Success>(GetEmailContentSuccess([], [], [], email));
} else {
yield Left(GetEmailContentFailure(null));
}
}
Stream<Either<Failure, Success>> _tryToGetOpenedEmailCache(
Session session,
AccountId accountId,
EmailId emailId,
String? baseDownloadUrl,
{
bool composeEmail = false,
bool draftsEmail = false
}
) async* {
log('GetEmailContentInteractor::_getOpenedEmailCache():');
try {
final detailedEmail = await emailRepository.getOpenedEmail(session, accountId, emailId);
if (detailedEmail != null) {
yield Right<Failure, Success>(GetEmailContentFromCacheSuccess(
detailedEmail.htmlEmailContent ?? "",
detailedEmail.attachments ?? [],
));
} else {
yield* _getContentEmailFromServer(session, accountId, emailId, baseDownloadUrl, composeEmail: composeEmail, draftsEmail: draftsEmail);
}
} catch (e) {
if (e is PathNotFoundException) {
yield* _getContentEmailFromServer(session, accountId, emailId, baseDownloadUrl, composeEmail: composeEmail, draftsEmail: draftsEmail);
} else {
yield Left<Failure, Success>(GetEmailContentFailure(e));
}
}
}
}
@@ -1,25 +0,0 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
import 'package:tmail_ui_user/features/email/domain/state/get_opened_email_from_cache_state.dart';
class GetOpenedEmailFromCacheInteractor {
final EmailRepository _emailRepository;
GetOpenedEmailFromCacheInteractor(this._emailRepository);
Stream<Either<Failure, Success>> execute(Session session, AccountId accountId, EmailId emailId) async* {
try {
yield Right<Failure, Success>(GetOpenedEmailLoading());
final detailedEmail = await _emailRepository.getOpenedEmail(session, accountId, emailId);
yield Right<Failure, Success>(GetOpenedEmailSuccess(detailedEmail));
} catch (e) {
yield Left<Failure, Success>(GetOpenedEmailFailure(e));
}
}
}