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
@@ -8,13 +8,13 @@ import 'package:tmail_ui_user/features/offline_mode/model/attachment_hive_cache.
extension AttachmentExtension on AttachmentHiveCache {
Attachment toAttachment() {
return Attachment(
partId: PartId(partId!),
blobId: Id(blobId!),
size: UnsignedInt(size!),
name: name,
type: MediaType.parse(type!),
cid: cid,
disposition: disposition?.toContentDisposition(),
partId: partId != null ? PartId(partId!) : null,
blobId: blobId != null ? Id(blobId!) : null,
size: size != null ? UnsignedInt(size!) : null,
name: name,
type: type != null ? MediaType.parse(type!) : null,
cid: cid,
disposition: disposition?.toContentDisposition(),
);
}
}
@@ -17,7 +17,9 @@ extension DetailedEmailExtension on DetailedEmail {
);
}
String get folderPath => CachingConstants.emailContentFolderName;
String get newEmailFolderPath => CachingConstants.newEmailContentFolderName;
String get openedEmailFolderPath => CachingConstants.openedEmailContentFolderNamee;
DetailedEmail fromEmailContentPath(String path) {
return DetailedEmail(
@@ -96,5 +96,5 @@ abstract class EmailRepository {
Future<void> storeOpenedEmail(Session session, AccountId accountId, DetailedEmail detailedEmail);
Future<DetailedEmail> getOpenedEmail(Session session, AccountId accountId, EmailId emailId);
Future<DetailedEmail?> getOpenedEmail(Session session, AccountId accountId, EmailId emailId);
}
@@ -26,6 +26,22 @@ class GetEmailContentSuccess extends UIState {
];
}
class GetEmailContentFromCacheSuccess extends UIState {
final String emailContentString;
final List<Attachment> attachments;
GetEmailContentFromCacheSuccess(
this.emailContentString,
this.attachments,
);
@override
List<Object?> get props => [
emailContentString,
attachments,
];
}
class GetEmailContentFailure extends FeatureFailure {
final dynamic exception;
@@ -1,22 +0,0 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:tmail_ui_user/features/email/domain/model/detailed_email.dart';
class GetOpenedEmailLoading extends UIState {}
class GetOpenedEmailSuccess extends UIState {
final DetailedEmail detailedEmail;
GetOpenedEmailSuccess(this.detailedEmail);
@override
List<Object?> get props => [detailedEmail];
}
class GetOpenedEmailFailure extends FeatureFailure {
GetOpenedEmailFailure(dynamic exception) : super(exception: exception);
@override
List<Object?> get props => [exception];
}
@@ -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));
}
}
}