TF-4050 refactor: move attachment preview & download logic to download controller

This commit is contained in:
dab246
2025-10-23 12:14:39 +07:00
committed by Dat H. Pham
parent 1398a30ed6
commit 92286b48e1
33 changed files with 1055 additions and 651 deletions
@@ -1,3 +1,6 @@
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/core/id.dart';
import 'package:model/email/attachment.dart';
import 'package:tmail_ui_user/features/base/action/ui_action.dart';
@@ -12,13 +15,13 @@ class DownloadUIAction extends UIAction {
}
class UpdateAttachmentsViewStateAction extends DownloadUIAction {
UpdateAttachmentsViewStateAction(this.blobId, this.success);
UpdateAttachmentsViewStateAction(this.blobId, this.viewState);
final Id? blobId;
final dynamic success;
final Either<Failure, Success> viewState;
@override
List<Object?> get props => [blobId, success];
List<Object?> get props => [blobId, viewState];
}
class DownloadAttachmentsQuicklyAction extends DownloadUIAction {
@@ -29,3 +32,12 @@ class DownloadAttachmentsQuicklyAction extends DownloadUIAction {
@override
List<Object?> get props => [attachment];
}
class OpenMailtoLinkFromPreviewAttachmentAction extends DownloadUIAction {
OpenMailtoLinkFromPreviewAttachmentAction(this.uri);
final Uri? uri;
@override
List<Object?> get props => [uri];
}
@@ -1,8 +1,25 @@
import 'package:core/core.dart';
import 'package:get/get.dart';
import 'package:tmail_ui_user/features/base/interactors_bindings.dart';
import 'package:tmail_ui_user/features/caching/utils/local_storage_manager.dart';
import 'package:tmail_ui_user/features/caching/utils/session_storage_manager.dart';
import 'package:tmail_ui_user/features/email/data/datasource/email_datasource.dart';
import 'package:tmail_ui_user/features/email/data/datasource/html_datasource.dart';
import 'package:tmail_ui_user/features/email/data/datasource_impl/email_datasource_impl.dart';
import 'package:tmail_ui_user/features/email/data/datasource_impl/email_local_storage_datasource_impl.dart';
import 'package:tmail_ui_user/features/email/data/datasource_impl/email_session_storage_datasource_impl.dart';
import 'package:tmail_ui_user/features/email/data/datasource_impl/html_datasource_impl.dart';
import 'package:tmail_ui_user/features/email/data/local/html_analyzer.dart';
import 'package:tmail_ui_user/features/email/data/network/email_api.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/download_all_attachments_for_web_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/download_attachment_for_web_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_html_content_from_attachment_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_preview_email_eml_content_shared_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_preview_eml_content_in_memory_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/move_preview_eml_content_from_persistent_to_memory_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/parse_email_by_blob_id_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/preview_email_from_eml_file_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/remove_preview_email_eml_content_shared_interactor.dart';
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
import 'package:tmail_ui_user/features/login/domain/repository/authentication_oidc_repository.dart';
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
@@ -10,6 +27,7 @@ import 'package:tmail_ui_user/features/mailbox_dashboard/data/datasource/downloa
import 'package:tmail_ui_user/features/mailbox_dashboard/data/datasource_impl/download_datasource_impl.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/data/repository/download_repository_impl.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/repository/download_repository.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
class DownloadInteractorBindings extends InteractorsBindings {
@@ -21,6 +39,31 @@ class DownloadInteractorBindings extends InteractorsBindings {
Get.find<RemoteExceptionThrower>(),
),
);
Get.lazyPut(
() => EmailDataSourceImpl(
Get.find<EmailAPI>(),
Get.find<RemoteExceptionThrower>(),
),
);
Get.lazyPut(
() => HtmlDataSourceImpl(
Get.find<HtmlAnalyzer>(),
Get.find<CacheExceptionThrower>(),
),
);
Get.lazyPut(
() => EmailSessionStorageDatasourceImpl(
Get.find<SessionStorageManager>(),
Get.find<CacheExceptionThrower>(),
),
);
Get.lazyPut(
() => EmailLocalStorageDataSourceImpl(
Get.find<LocalStorageManager>(),
Get.find<PreviewEmlFileUtils>(),
Get.find<CacheExceptionThrower>(),
),
);
}
@override
@@ -41,6 +84,38 @@ class DownloadInteractorBindings extends InteractorsBindings {
Get.find<CredentialRepository>(),
),
);
Get.lazyPut(
() => ParseEmailByBlobIdInteractor(Get.find<DownloadRepository>()),
);
Get.lazyPut(
() => PreviewEmailFromEmlFileInteractor(Get.find<DownloadRepository>()),
);
Get.lazyPut(
() => GetHtmlContentFromAttachmentInteractor(
Get.find<DownloadAttachmentForWebInteractor>(),
),
);
Get.lazyPut(
() => MovePreviewEmlContentFromPersistentToMemoryInteractor(
Get.find<DownloadRepository>(),
),
);
Get.lazyPut(
() => RemovePreviewEmailEmlContentSharedInteractor(
Get.find<DownloadRepository>(),
),
);
Get.lazyPut(
() => GetPreviewEmailEMLContentSharedInteractor(
Get.find<DownloadRepository>(),
),
);
Get.lazyPut(
() => GetPreviewEmlContentInMemoryInteractor(
Get.find<DownloadRepository>(),
),
);
}
@override
@@ -50,11 +125,23 @@ class DownloadInteractorBindings extends InteractorsBindings {
@override
void bindingsRepositoryImpl() {
Get.lazyPut(() => DownloadRepositoryImpl(Get.find<DownloadDatasource>()));
Get.lazyPut(
() => DownloadRepositoryImpl(
Get.find<DownloadDatasource>(),
{
DataSourceType.session: Get.find<EmailSessionStorageDatasourceImpl>(),
DataSourceType.local: Get.find<EmailLocalStorageDataSourceImpl>(),
DataSourceType.network: Get.find<EmailDataSource>(),
},
Get.find<HtmlDataSource>(),
),
);
}
@override
void bindingsDataSource() {
Get.lazyPut<DownloadDatasource>(() => Get.find<DownloadDatasourceImpl>());
Get.lazyPut<EmailDataSource>(() => Get.find<EmailDataSourceImpl>());
Get.lazyPut<HtmlDataSource>(() => Get.find<HtmlDataSourceImpl>());
}
}
@@ -34,10 +34,13 @@ import 'package:tmail_ui_user/features/email/domain/usecases/delete_email_perman
import 'package:tmail_ui_user/features/email/domain/usecases/delete_multiple_emails_permanently_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/download_all_attachments_for_web_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/download_attachment_for_web_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_html_content_from_attachment_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_restored_deleted_message_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/mark_as_email_read_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/mark_as_star_email_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/move_to_mailbox_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/parse_email_by_blob_id_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/preview_email_from_eml_file_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/restore_deleted_message_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/unsubscribe_email_interactor.dart';
import 'package:tmail_ui_user/features/email/presentation/bindings/email_bindings.dart';
@@ -175,6 +178,9 @@ class MailboxDashBoardBindings extends BaseBindings {
Get.find<DownloadAttachmentForWebInteractor>(),
Get.find<DownloadAllAttachmentsForWebInteractor>(),
Get.find<DownloadManager>(),
Get.find<ParseEmailByBlobIdInteractor>(),
Get.find<PreviewEmailFromEmlFileInteractor>(),
Get.find<GetHtmlContentFromAttachmentInteractor>(),
));
Get.put(SearchController(
Get.find<QuickSearchEmailInteractor>(),
@@ -16,6 +16,7 @@ import 'package:get/get.dart';
import 'package:get/get_navigation/src/dialog/dialog_route.dart';
import 'package:http_parser/http_parser.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:model/download/download_task_id.dart';
import 'package:model/email/attachment.dart';
@@ -28,9 +29,18 @@ import 'package:tmail_ui_user/features/base/base_controller.dart';
import 'package:tmail_ui_user/features/email/domain/exceptions/email_exceptions.dart';
import 'package:tmail_ui_user/features/email/domain/state/download_all_attachments_for_web_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/download_attachment_for_web_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/get_html_content_from_attachment_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/parse_email_by_blob_id_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/preview_email_from_eml_file_state.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/download_all_attachments_for_web_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/download_attachment_for_web_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_html_content_from_attachment_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/parse_email_by_blob_id_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/preview_email_from_eml_file_interactor.dart';
import 'package:tmail_ui_user/features/email/presentation/extensions/attachment_extension.dart';
import 'package:tmail_ui_user/features/email/presentation/mixin/preview_attachment_mixin.dart';
import 'package:tmail_ui_user/features/email/presentation/utils/email_utils.dart';
import 'package:tmail_ui_user/features/email/presentation/widgets/html_attachment_previewer.dart';
import 'package:tmail_ui_user/features/home/data/exceptions/session_exceptions.dart';
import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/action/download_ui_action.dart';
@@ -47,16 +57,23 @@ import 'package:uuid/uuid.dart';
typedef UpdateDownloadTaskStateCallback = DownloadTaskState Function(DownloadTaskState currentState);
class DownloadController extends BaseController {
class DownloadController extends BaseController with PreviewAttachmentMixin {
final DownloadAttachmentForWebInteractor _downloadAttachmentForWebInteractor;
final DownloadAllAttachmentsForWebInteractor
_downloadAllAttachmentsForWebInteractor;
final DownloadManager _downloadManager;
final ParseEmailByBlobIdInteractor parseEmailByBlobIdInteractor;
final PreviewEmailFromEmlFileInteractor previewEmailFromEmlFileInteractor;
final GetHtmlContentFromAttachmentInteractor
getHtmlContentFromAttachmentInteractor;
DownloadController(
this._downloadAttachmentForWebInteractor,
this._downloadAllAttachmentsForWebInteractor,
this._downloadManager,
this.parseEmailByBlobIdInteractor,
this.previewEmailFromEmlFileInteractor,
this.getHtmlContentFromAttachmentInteractor,
);
final listDownloadTaskState = RxList<DownloadTaskState>();
@@ -335,7 +352,7 @@ class DownloadController extends BaseController {
) {
_pushDownloadUIAction(UpdateAttachmentsViewStateAction(
success.attachment.blobId,
success,
Right<Failure, Success>(success),
));
deleteDownloadTask(success.taskId);
@@ -446,7 +463,10 @@ class DownloadController extends BaseController {
if (failure.attachment != null) {
_pushDownloadUIAction(
UpdateAttachmentsViewStateAction(failure.attachment?.blobId, failure),
UpdateAttachmentsViewStateAction(
failure.attachment?.blobId,
Left<Failure, Success>(failure),
),
);
}
@@ -464,7 +484,6 @@ class DownloadController extends BaseController {
appToast.showToastErrorMessage(currentOverlayContext!, message);
}
@override
void handleSuccessViewState(Success success) {
if (success is DownloadAttachmentForWebSuccess) {
@@ -472,15 +491,77 @@ class DownloadController extends BaseController {
} else if (success is StartDownloadAttachmentForWeb) {
_pushDownloadUIAction(UpdateAttachmentsViewStateAction(
success.attachment.blobId,
success,
Right<Failure, Success>(success),
));
} else if (success is DownloadingAttachmentForWeb) {
_pushDownloadUIAction(UpdateAttachmentsViewStateAction(
success.attachment.blobId,
success,
Right<Failure, Success>(success),
));
} else if (success is DownloadAllAttachmentsForWebSuccess) {
deleteDownloadTask(success.taskId);
} else if (success is ParseEmailByBlobIdSuccess) {
handleParseEmailByBlobIdSuccess(
context: currentContext,
accountId: success.accountId,
session: success.session,
ownEmailAddress: success.ownEmailAddress,
blobId: success.blobId,
email: success.email,
controller: this,
previewInteractor: previewEmailFromEmlFileInteractor,
);
} else if (success is PreviewEmailFromEmlFileSuccess) {
handlePreviewEmailFromEMLFileSuccess(
emlPreviewer: success.emlPreviewer,
context: currentContext,
imagePaths: imagePaths,
onMailtoAction: _openMailtoLink,
onDownloadAction: (uri) async {
if (uri == null) return;
final attachment = EmailUtils.parsingAttachmentByUri(uri);
if (attachment == null) return;
_downloadAttachmentQuickly(attachment);
},
onPreviewAction: (uri) async {
if (currentContext != null && uri?.path.isNotEmpty == true) {
previewEMLFileAction(
appLocalizations: AppLocalizations.of(currentContext!),
accountId: success.accountId,
session: success.session,
ownEmailAddress: success.ownEmailAddress,
blobId: Id(uri!.path),
controller: this,
parseEmailByBlobIdInteractor: parseEmailByBlobIdInteractor,
);
}
},
);
} else if (success is GetHtmlContentFromAttachmentSuccess) {
_pushDownloadUIAction(
UpdateAttachmentsViewStateAction(
success.attachment.blobId,
Right<Failure, Success>(success),
),
);
Get.dialog(HtmlAttachmentPreviewer(
title: success.htmlAttachmentTitle,
htmlContent: success.sanitizedHtmlContent,
mailToClicked: _openMailtoLink,
downloadAttachmentClicked: () =>
_downloadAttachmentQuickly(success.attachment),
responsiveUtils: responsiveUtils,
));
} else if (success is GettingHtmlContentFromAttachment) {
_pushDownloadUIAction(
UpdateAttachmentsViewStateAction(
success.attachment.blobId,
Right<Failure, Success>(success),
),
);
} else {
super.handleSuccessViewState(success);
}
@@ -492,11 +573,40 @@ class DownloadController extends BaseController {
_downloadAllAttachmentsForWebFailure(failure);
} else if (failure is DownloadAttachmentForWebFailure) {
_downloadAttachmentForWebFailureAction(failure);
} else if (failure is ParseEmailByBlobIdFailure) {
handleParseEmailByBlobIdFailure(failure);
} else if (failure is PreviewEmailFromEmlFileFailure) {
handlePreviewEmailFromEMLFileFailure(failure);
} else if (failure is GetHtmlContentFromAttachmentFailure) {
_handleGetHtmlContentFromAttachmentFailure(failure);
} else {
super.handleFailureViewState(failure);
}
}
void _handleGetHtmlContentFromAttachmentFailure(
GetHtmlContentFromAttachmentFailure failure,
) {
_pushDownloadUIAction(
UpdateAttachmentsViewStateAction(
failure.attachment.blobId,
Left<Failure, Success>(failure),
),
);
if (currentOverlayContext != null && currentContext != null) {
appToast.showToastErrorMessage(
currentOverlayContext!,
AppLocalizations.of(currentContext!)
.thisHtmlAttachmentCannotBePreviewed,
);
}
}
Future<void> _openMailtoLink(Uri? uri) async {
_pushDownloadUIAction(OpenMailtoLinkFromPreviewAttachmentAction(uri));
}
@override
void onClose() {
_downloadProgressStateSubscription?.cancel();
@@ -123,6 +123,7 @@ import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/handle_create_new_rule_filter.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/handle_download_extension.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/handle_preferences_setting_extension.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/handle_preview_attachment_extension.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/handle_reactive_obx_variable_extension.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/handle_save_email_as_draft_extension.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/handle_store_email_sort_order_extension.dart';
@@ -767,6 +768,9 @@ class MailboxDashBoardController extends ReloadableController
if (action is DownloadAttachmentsQuicklyAction) {
downloadAttachmentForWeb(attachment: action.attachment);
downloadController.clearDownloadUIAction();
} else if (action is OpenMailtoLinkFromPreviewAttachmentAction) {
openMailToLink(action.uri);
downloadController.clearDownloadUIAction();
}
},
);
@@ -0,0 +1,84 @@
import 'package:core/presentation/resources/image_paths.dart';
import 'package:core/presentation/views/html_viewer/html_content_viewer_widget.dart';
import 'package:core/utils/app_logger.dart';
import 'package:flutter/cupertino.dart';
import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:model/email/attachment.dart';
import 'package:tmail_ui_user/features/email/presentation/mixin/preview_attachment_mixin.dart';
import 'package:tmail_ui_user/features/email/presentation/model/composer_arguments.dart';
import 'package:tmail_ui_user/features/email/presentation/model/eml_previewer.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/open_and_close_composer_extension.dart';
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
import 'package:tmail_ui_user/main/routes/route_utils.dart';
extension HandlePreviewAttachmentExtension on MailboxDashBoardController {
void previewAttachmentAction({
required BuildContext context,
required Attachment attachment,
required OnDownloadAttachmentAction onDownloadAttachment,
}) =>
downloadController.previewAttachmentAction(
context: context,
attachment: attachment,
accountId: accountId.value,
session: sessionCurrent,
ownEmailAddress: ownEmailAddress.value,
controller: downloadController,
parseEmailInteractor: downloadController.parseEmailByBlobIdInteractor,
getHtmlInteractor:
downloadController.getHtmlContentFromAttachmentInteractor,
onDownloadAttachment: onDownloadAttachment,
);
void previewEMLFileAction({
required AppLocalizations appLocalizations,
required Id? blobId,
}) =>
downloadController.previewEMLFileAction(
appLocalizations: appLocalizations,
accountId: accountId.value,
session: sessionCurrent,
ownEmailAddress: ownEmailAddress.value,
blobId: blobId,
controller: downloadController,
parseEmailByBlobIdInteractor:
downloadController.parseEmailByBlobIdInteractor,
);
void showDialogToPreviewEMLAttachment({
required BuildContext context,
required EMLPreviewer emlPreviewer,
required ImagePaths imagePaths,
required OnMailtoDelegateAction onMailtoAction,
required OnDownloadAttachmentDelegateAction onDownloadAction,
required OnPreviewEMLDelegateAction onPreviewAction,
}) =>
downloadController.showDialogToPreviewEMLAttachment(
context: context,
emlPreviewer: emlPreviewer,
imagePaths: imagePaths,
onMailtoAction: onMailtoAction,
onDownloadAction: onDownloadAction,
onPreviewAction: onPreviewAction,
);
Future<void> openMailToLink(Uri? uri) async {
if (uri == null) return;
final navigationRouter = RouteUtils.generateNavigationRouterFromMailtoLink(
uri.toString(),
);
log('$runtimeType::openMailToLink(): ${uri.toString()}');
if (!RouteUtils.canOpenComposerFromNavigationRouter(navigationRouter)) {
return;
}
openComposer(ComposerArguments.fromMailtoUri(
listEmailAddress: navigationRouter.listEmailAddress,
cc: navigationRouter.cc,
bcc: navigationRouter.bcc,
subject: navigationRouter.subject,
body: navigationRouter.body,
));
}
}