TF-1923 Fix new email offline mode not working

(cherry picked from commit c1c48e93d50cb0ebbccaf11c3f4a975af640b63a)
This commit is contained in:
dab246
2023-06-20 22:20:23 +07:00
committed by Dat H. Pham
parent b1a0cab02b
commit 6d96a1109e
35 changed files with 333 additions and 261 deletions
@@ -0,0 +1,4 @@
class NotFoundStoredOpenedEmailException implements Exception {}
class NotFoundStoredNewEmailException implements Exception {}
@@ -1,4 +1,6 @@
class CannotDeleteOldEmailException implements Exception {}
class NotFoundEmailException implements Exception {}
class NotFoundEmailException implements Exception {}
class NotFoundEmailContentException implements Exception {}
@@ -5,6 +5,7 @@ import 'package:tmail_ui_user/features/email/domain/extensions/list_attachments_
import 'package:tmail_ui_user/features/email/domain/extensions/list_email_header_extension.dart';
import 'package:tmail_ui_user/features/email/domain/model/detailed_email.dart';
import 'package:tmail_ui_user/features/offline_mode/model/detailed_email_hive_cache.dart';
import 'package:tmail_ui_user/features/thread/data/extensions/map_keywords_extension.dart';
extension DetailedEmailExtension on DetailedEmail {
DetailedEmailHiveCache toHiveCache() {
@@ -12,12 +13,13 @@ extension DetailedEmailExtension on DetailedEmail {
emailId: emailId.asString,
timeSaved: DateTime.now(),
attachments: attachments?.toHiveCache(),
headers: headers?.toHiveCache(),
headers: headers?.toList().toHiveCache(),
keywords: keywords?.toMapString(),
emailContentPath: emailContentPath
);
}
String get newEmailFolderPath => CachingConstants.incomingEmailedContentFolderName;
String get newEmailFolderPath => CachingConstants.newEmailsContentFolderName;
String get openedEmailFolderPath => CachingConstants.openedEmailContentFolderName;
@@ -26,6 +28,7 @@ extension DetailedEmailExtension on DetailedEmail {
emailId: emailId,
attachments: attachments,
headers: headers,
keywords: keywords,
htmlEmailContent: htmlEmailContent,
emailContentPath: path,
);
@@ -1,5 +1,6 @@
import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/email/keyword_identifier.dart';
import 'package:tmail_ui_user/features/email/domain/extensions/list_acttachments_hive_cache_extension.dart';
import 'package:tmail_ui_user/features/email/domain/extensions/list_email_header_hive_cache_extension.dart';
import 'package:tmail_ui_user/features/email/domain/model/detailed_email.dart';
@@ -10,7 +11,10 @@ extension DetailedEmailHiveCacheExtension on DetailedEmailHiveCache {
return DetailedEmail(
emailId: EmailId(Id(emailId)),
attachments: attachments?.toAttachment(),
headers: headers?.toListEmailHeader(),
headers: headers?.toSetEmailHeader(),
keywords: keywords != null
? Map.fromIterables(keywords!.keys.map((value) => KeyWordIdentifier(value)), keywords!.values)
: null,
htmlEmailContent: emailContent,
);
}
@@ -8,7 +8,8 @@ extension EmailExtension on Email {
return DetailedEmail(
emailId: id!,
attachments: allAttachments,
headers: headers?.toList(),
headers: headers,
keywords: keywords,
htmlEmailContent: htmlEmailContent
);
}
@@ -3,5 +3,5 @@ import 'package:tmail_ui_user/features/email/domain/extensions/email_header_hive
import 'package:tmail_ui_user/features/offline_mode/model/email_header_hive_cache.dart';
extension ListEmailHeaderExtension on List<EmailHeaderHiveCache> {
List<EmailHeader> toListEmailHeader() => map((emailHeaderHiveCache) => emailHeaderHiveCache.toEmailHeader()).toList();
Set<EmailHeader> toSetEmailHeader() => map((emailHeaderHiveCache) => emailHeaderHiveCache.toEmailHeader()).toSet();
}
@@ -1,12 +1,14 @@
import 'package:equatable/equatable.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/email/email_header.dart';
import 'package:jmap_dart_client/jmap/mail/email/keyword_identifier.dart';
import 'package:model/email/attachment.dart';
class DetailedEmail with EquatableMixin {
final EmailId emailId;
final List<Attachment>? attachments;
final List<EmailHeader>? headers;
final Set<EmailHeader>? headers;
final Map<KeyWordIdentifier, bool>? keywords;
final String? htmlEmailContent;
final String? emailContentPath;
@@ -14,6 +16,7 @@ class DetailedEmail with EquatableMixin {
required this.emailId,
this.attachments,
this.headers,
this.keywords,
this.htmlEmailContent,
this.emailContentPath
});
@@ -23,6 +26,7 @@ class DetailedEmail with EquatableMixin {
emailId,
attachments,
headers,
keywords,
htmlEmailContent,
emailContentPath
];
@@ -88,15 +88,17 @@ abstract class EmailRepository {
Future<jmap.State?> getEmailState(Session session, AccountId accountId);
Future<void> storeDetailedEmailToCache(Session session, AccountId accountId, DetailedEmail detailedEmail);
Future<void> storeDetailedNewEmail(Session session, AccountId accountId, DetailedEmail detailedEmail);
Future<Email> getDetailedEmailById(Session session, AccountId accountId, EmailId emailId);
Future<void> storeEmailToCache(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<void> storeOpenedEmail(Session session, AccountId accountId, DetailedEmail detailedEmail);
Future<DetailedEmail?> getOpenedEmail(Session session, AccountId accountId, EmailId emailId);
Future<DetailedEmail> getStoredOpenedEmail(Session session, AccountId accountId, EmailId emailId);
Future<DetailedEmail> getStoredNewEmail(Session session, AccountId accountId, EmailId emailId);
}
@@ -1,6 +1,8 @@
import 'package:core/core.dart';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:model/model.dart';
import 'package:model/email/attachment.dart';
import 'package:model/email/email_content.dart';
class GetEmailContentLoading extends LoadingState {}
@@ -10,12 +12,12 @@ class GetEmailContentSuccess extends UIState {
final List<Attachment> attachments;
final Email? emailCurrent;
GetEmailContentSuccess(
this.emailContents,
this.emailContentsDisplayed,
this.attachments,
this.emailCurrent
);
GetEmailContentSuccess({
required this.emailContents,
required this.emailContentsDisplayed,
required this.attachments,
required this.emailCurrent
});
@override
List<Object?> get props => [
@@ -29,23 +31,25 @@ class GetEmailContentSuccess extends UIState {
class GetEmailContentFromCacheSuccess extends UIState {
final String emailContentString;
final List<Attachment> attachments;
final Email? emailCurrent;
GetEmailContentFromCacheSuccess(
this.emailContentString,
this.attachments,
);
GetEmailContentFromCacheSuccess({
required this.emailContentString,
required this.attachments,
this.emailCurrent
});
@override
List<Object?> get props => [
emailContentString,
attachments,
emailCurrent,
];
}
class GetEmailContentFailure extends FeatureFailure {
final dynamic exception;
GetEmailContentFailure(this.exception);
GetEmailContentFailure(dynamic exception) : super(exception: exception);
@override
List<Object?> get props => [exception];
@@ -1,13 +0,0 @@
import 'package:core/core.dart';
class StoreDetailedEmailToCacheLoading extends UIState {}
class StoreDetailedEmailToCacheSuccess extends UIState {}
class StoreDetailedEmailToCacheFailure extends FeatureFailure {
StoreDetailedEmailToCacheFailure(dynamic exception) : super(exception: exception);
@override
List<Object?> get props => [exception];
}
@@ -0,0 +1,13 @@
import 'package:core/core.dart';
class StoreNewEmailLoading extends UIState {}
class StoreNewEmailSuccess extends UIState {}
class StoreNewEmailFailure extends FeatureFailure {
StoreNewEmailFailure(dynamic exception) : super(exception: exception);
@override
List<Object?> get props => [exception];
}
@@ -1,4 +1,3 @@
import 'dart:io';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
@@ -31,13 +30,13 @@ class GetEmailContentInteractor {
yield Right<Failure, Success>(GetEmailContentLoading());
if (PlatformInfo.isMobile) {
yield* _tryToGetOpenedEmailCache(session, accountId, emailId, baseDownloadUrl, composeEmail: composeEmail, draftsEmail: draftsEmail);
yield* _getStoredOpenedEmail(session, accountId, emailId, baseDownloadUrl, composeEmail: composeEmail, draftsEmail: draftsEmail);
} else {
yield* _getContentEmailFromServer(session, accountId, emailId, baseDownloadUrl, composeEmail: composeEmail, draftsEmail: draftsEmail);
}
} catch (e) {
log('GetEmailContentInteractor::execute(): exception = $e');
yield Left(GetEmailContentFailure(e));
yield Left<Failure, Success>(GetEmailContentFailure(e));
}
}
@@ -51,63 +50,105 @@ class GetEmailContentInteractor {
bool draftsEmail = false
}
) async* {
final email = await emailRepository.getEmailContent(session, accountId, emailId);
try {
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
);
if (email.emailContentList.isNotEmpty) {
final newEmailContents = await emailRepository.transformEmailContent(
email.emailContentList,
email.allAttachments.listAttachmentsDisplayedInContent,
baseDownloadUrl,
accountId,
draftsEmail: draftsEmail
);
final newEmailContentsDisplayed = PlatformInfo.isWeb && !composeEmail
? await emailRepository.addTooltipWhenHoverOnLink(newEmailContents)
: newEmailContents;
final newEmailContentsDisplayed = PlatformInfo.isWeb && !composeEmail
? await emailRepository.addTooltipWhenHoverOnLink(newEmailContents)
: newEmailContents;
yield Right<Failure, Success>(GetEmailContentSuccess(
newEmailContents.asHtmlString,
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));
yield Right<Failure, Success>(GetEmailContentSuccess(
emailContents: newEmailContents.asHtmlString,
emailContentsDisplayed: newEmailContentsDisplayed,
attachments: email.allAttachments,
emailCurrent: email
));
} else {
yield Right<Failure, Success>(GetEmailContentSuccess(
emailContents: '',
emailContentsDisplayed: [],
attachments: email.allAttachments,
emailCurrent: email
));
}
} catch (e) {
logError('GetEmailContentInteractor::_getContentEmailFromServer():EXCEPTION: $e');
yield Left<Failure, Success>(GetEmailContentFailure(e));
}
}
Stream<Either<Failure, Success>> _tryToGetOpenedEmailCache(
Stream<Either<Failure, Success>> _getStoredOpenedEmail(
Session session,
AccountId accountId,
EmailId emailId,
String? baseDownloadUrl,
{
bool composeEmail = false,
bool draftsEmail = false
}
{
bool composeEmail = false,
bool draftsEmail = false
}
) async* {
try {
final detailedEmail = await emailRepository.getOpenedEmail(session, accountId, emailId);
if (detailedEmail != null) {
log('GetEmailContentInteractor::_tryToGetOpenedEmailCache(): $detailedEmail');
yield Right<Failure, Success>(GetEmailContentFromCacheSuccess(
detailedEmail.htmlEmailContent ?? "",
detailedEmail.attachments ?? [],
));
} else {
yield* _getContentEmailFromServer(session, accountId, emailId, baseDownloadUrl, composeEmail: composeEmail, draftsEmail: draftsEmail);
}
final detailedEmail = await emailRepository.getStoredOpenedEmail(session, accountId, emailId);
yield Right<Failure, Success>(GetEmailContentFromCacheSuccess(
emailContentString: detailedEmail.htmlEmailContent ?? "",
attachments: detailedEmail.attachments ?? [],
emailCurrent: Email(
id: emailId,
headers: detailedEmail.headers,
keywords: detailedEmail.keywords
)
));
} catch (e) {
if (e is PathNotFoundException) {
yield* _getContentEmailFromServer(session, accountId, emailId, baseDownloadUrl, composeEmail: composeEmail, draftsEmail: draftsEmail);
} else {
yield Left<Failure, Success>(GetEmailContentFailure(e));
}
logError('GetEmailContentInteractor::_getStoredOpenedEmail():EXCEPTION: $e');
yield* _getStoredNewEmail(
session,
accountId,
emailId,
baseDownloadUrl,
composeEmail: composeEmail,
draftsEmail: draftsEmail);
}
}
Stream<Either<Failure, Success>> _getStoredNewEmail(
Session session,
AccountId accountId,
EmailId emailId,
String? baseDownloadUrl,
{
bool composeEmail = false,
bool draftsEmail = false
}
) async* {
try {
final detailedEmail = await emailRepository.getStoredNewEmail(session, accountId, emailId);
yield Right<Failure, Success>(GetEmailContentFromCacheSuccess(
emailContentString: detailedEmail.htmlEmailContent ?? "",
attachments: detailedEmail.attachments ?? [],
emailCurrent: Email(
id: emailId,
headers: detailedEmail.headers,
keywords: detailedEmail.keywords
)
));
} catch (e) {
logError('GetEmailContentInteractor::_getStoredNewEmail():EXCEPTION: $e');
yield* _getContentEmailFromServer(
session,
accountId,
emailId,
baseDownloadUrl,
composeEmail: composeEmail,
draftsEmail: draftsEmail);
}
}
}
@@ -6,12 +6,12 @@ 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/model/detailed_email.dart';
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
import 'package:tmail_ui_user/features/email/domain/state/store_detailed_email_to_cache_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/store_new_email_state.dart';
class StoreDetailedEmailToCacheInteractor {
class StoreNewEmailInteractor {
final EmailRepository _emailRepository;
StoreDetailedEmailToCacheInteractor(this._emailRepository);
StoreNewEmailInteractor(this._emailRepository);
Stream<Either<Failure, Success>> execute(
Session session,
@@ -20,14 +20,14 @@ class StoreDetailedEmailToCacheInteractor {
DetailedEmail detailedEmail
) async* {
try {
yield Right<Failure, Success>(StoreDetailedEmailToCacheLoading());
yield Right<Failure, Success>(StoreNewEmailLoading());
await Future.wait([
_emailRepository.storeEmailToCache(session, accountId, email),
_emailRepository.storeDetailedEmailToCache(session, accountId, detailedEmail),
_emailRepository.storeEmail(session, accountId, email),
_emailRepository.storeDetailedNewEmail(session, accountId, detailedEmail),
]);
yield Right<Failure, Success>(StoreDetailedEmailToCacheSuccess());
yield Right<Failure, Success>(StoreNewEmailSuccess());
} catch (e) {
yield Left<Failure, Success>(StoreDetailedEmailToCacheFailure(e));
yield Left<Failure, Success>(StoreNewEmailFailure(e));
}
}
}
@@ -5,12 +5,12 @@ import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:tmail_ui_user/features/email/domain/model/detailed_email.dart';
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
import 'package:tmail_ui_user/features/email/domain/state/store_opened_email_to_cache_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/store_opened_email_state.dart';
class StoreOpenedEmailToCacheInteractor {
class StoreOpenedEmailInteractor {
final EmailRepository _emailRepository;
StoreOpenedEmailToCacheInteractor(this._emailRepository);
StoreOpenedEmailInteractor(this._emailRepository);
Stream<Either<Failure, Success>> execute(
Session session,