TF-3450 Refactor image and text preview

This commit is contained in:
DatDang
2025-02-26 11:19:25 +07:00
committed by Dat H. Pham
parent ef1d37f7ac
commit aabfe6a3a5
14 changed files with 94 additions and 285 deletions
@@ -3,6 +3,7 @@ class SupportedPreviewFileTypes {
'image/bmp', 'image/bmp',
'image/jpeg', 'image/jpeg',
'image/gif', 'image/gif',
'image/webp',
'image/png',]; 'image/png',];
static const textMimeTypes = [ static const textMimeTypes = [
@@ -10,6 +11,10 @@ class SupportedPreviewFileTypes {
'text/markdown', 'text/markdown',
]; ];
static const jsonMimeTypes = [
'application/json',
];
static const docMimeTypes = [ static const docMimeTypes = [
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.oasis.opendocument.text', 'application/vnd.oasis.opendocument.text',
@@ -23,6 +23,8 @@ extension MediaTypeExtension on MediaType {
bool isTextFile() => SupportedPreviewFileTypes.textMimeTypes.contains(mimeType); bool isTextFile() => SupportedPreviewFileTypes.textMimeTypes.contains(mimeType);
bool isJsonFile() => SupportedPreviewFileTypes.jsonMimeTypes.contains(mimeType);
DocumentUti getDocumentUti() => DocumentUti(SupportedPreviewFileTypes.iOSSupportedTypes[mimeType]); DocumentUti getDocumentUti() => DocumentUti(SupportedPreviewFileTypes.iOSSupportedTypes[mimeType]);
String getIcon(ImagePaths imagePaths, {String? fileName}) { String getIcon(ImagePaths imagePaths, {String? fileName}) {
@@ -12,11 +12,12 @@ class StartDownloadAttachmentForWeb extends UIState {
final DownloadTaskId taskId; final DownloadTaskId taskId;
final Attachment attachment; final Attachment attachment;
final CancelToken? cancelToken; final CancelToken? cancelToken;
final bool previewerSupported;
StartDownloadAttachmentForWeb(this.taskId, this.attachment, [this.cancelToken]); StartDownloadAttachmentForWeb(this.taskId, this.attachment, [this.cancelToken, this.previewerSupported = false]);
@override @override
List<Object?> get props => [taskId, attachment, cancelToken]; List<Object?> get props => [taskId, attachment, cancelToken, previewerSupported];
} }
class DownloadingAttachmentForWeb extends UIState { class DownloadingAttachmentForWeb extends UIState {
@@ -50,11 +51,12 @@ class DownloadAttachmentForWebSuccess extends UIState {
final DownloadTaskId taskId; final DownloadTaskId taskId;
final Attachment attachment; final Attachment attachment;
final Uint8List bytes; final Uint8List bytes;
final bool previewerSupported;
DownloadAttachmentForWebSuccess(this.taskId, this.attachment, this.bytes); DownloadAttachmentForWebSuccess(this.taskId, this.attachment, this.bytes, this.previewerSupported);
@override @override
List<Object> get props => [taskId, attachment, bytes]; List<Object> get props => [taskId, attachment, bytes, previewerSupported];
} }
class DownloadAttachmentForWebFailure extends FeatureFailure { class DownloadAttachmentForWebFailure extends FeatureFailure {
@@ -1,31 +0,0 @@
import 'dart:typed_data';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:model/email/attachment.dart';
class GettingImageDataFromAttachment extends LoadingState {
GettingImageDataFromAttachment({required this.attachment});
final Attachment attachment;
@override
List<Object> get props => [attachment];
}
class GetImageDataFromAttachmentSuccess extends UIState {
GetImageDataFromAttachmentSuccess({
required this.attachment,
required this.data,
});
final Attachment attachment;
final Uint8List data;
@override
List<Object> get props => [attachment, data];
}
class GetImageDataFromAttachmentFailure extends FeatureFailure {
GetImageDataFromAttachmentFailure({super.exception});
}
@@ -1,31 +0,0 @@
import 'dart:typed_data';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:model/email/attachment.dart';
class GettingTextDataFromAttachment extends LoadingState {
GettingTextDataFromAttachment({required this.attachment});
final Attachment attachment;
@override
List<Object> get props => [attachment];
}
class GetTextDataFromAttachmentSuccess extends UIState {
GetTextDataFromAttachmentSuccess({
required this.attachment,
required this.data,
});
final Attachment attachment;
final Uint8List data;
@override
List<Object> get props => [attachment, data];
}
class GetTextDataFromAttachmentFailure extends FeatureFailure {
GetTextDataFromAttachmentFailure({super.exception});
}
@@ -35,11 +35,12 @@ class DownloadAttachmentForWebInteractor {
AccountId accountId, AccountId accountId,
String baseDownloadUrl, String baseDownloadUrl,
StreamController<Either<Failure, Success>> onReceiveController, StreamController<Either<Failure, Success>> onReceiveController,
{CancelToken? cancelToken} {CancelToken? cancelToken,
) async* { bool previewerSupported = false,
}) async* {
try { try {
yield Right<Failure, Success>(StartDownloadAttachmentForWeb(taskId, attachment, cancelToken)); yield Right<Failure, Success>(StartDownloadAttachmentForWeb(taskId, attachment, cancelToken, previewerSupported));
onReceiveController.add(Right(StartDownloadAttachmentForWeb(taskId, attachment, cancelToken))); onReceiveController.add(Right(StartDownloadAttachmentForWeb(taskId, attachment, cancelToken, previewerSupported)));
final currentAccount = await _accountRepository.getCurrentAccount(); final currentAccount = await _accountRepository.getCurrentAccount();
AccountRequest? accountRequest; AccountRequest? accountRequest;
@@ -69,7 +70,8 @@ class DownloadAttachmentForWebInteractor {
DownloadAttachmentForWebSuccess( DownloadAttachmentForWebSuccess(
taskId, taskId,
attachment, attachment,
bytesDownloaded bytesDownloaded,
previewerSupported,
) )
); );
} catch (exception) { } catch (exception) {
@@ -1,62 +0,0 @@
import 'dart:async';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
import 'package:dartz/dartz.dart';
import 'package:dio/dio.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:model/download/download_task_id.dart';
import 'package:model/email/attachment.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_image_data_from_attachment_state.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/download_attachment_for_web_interactor.dart';
class GetImageDataFromAttachmentInteractor {
const GetImageDataFromAttachmentInteractor(this._downloadAttachmentForWebInteractor);
final DownloadAttachmentForWebInteractor _downloadAttachmentForWebInteractor;
Stream<Either<Failure, Success>> execute(
DownloadTaskId taskId,
Attachment attachment,
AccountId accountId,
String baseDownloadUrl,
{CancelToken? cancelToken,}
) async* {
final onProgressStream = StreamController<Either<Failure, Success>>();
try {
yield Right(GettingImageDataFromAttachment(attachment: attachment));
final downloadState = await _downloadAttachmentForWebInteractor.execute(
taskId,
attachment,
accountId,
baseDownloadUrl,
onProgressStream,
cancelToken: cancelToken,
).last;
yield downloadState.fold(
(failure) {
return Left(GetImageDataFromAttachmentFailure(
exception: failure is FeatureFailure ? failure.exception : null,
));
},
(success) {
if (success is! DownloadAttachmentForWebSuccess) {
return Left(GetImageDataFromAttachmentFailure());
}
return Right(GetImageDataFromAttachmentSuccess(
attachment: attachment,
data: success.bytes,
));
},
);
} catch (e) {
logError('GetImageDataFromAttachmentInteractor::execute(): $e');
yield Left(GetImageDataFromAttachmentFailure(exception: e));
}
onProgressStream.close();
}
}
@@ -1,62 +0,0 @@
import 'dart:async';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
import 'package:dartz/dartz.dart';
import 'package:dio/dio.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:model/download/download_task_id.dart';
import 'package:model/email/attachment.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_text_data_from_attachment_state.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/download_attachment_for_web_interactor.dart';
class GetTextDataFromAttachmentInteractor {
const GetTextDataFromAttachmentInteractor(this._downloadAttachmentForWebInteractor);
final DownloadAttachmentForWebInteractor _downloadAttachmentForWebInteractor;
Stream<Either<Failure, Success>> execute(
DownloadTaskId taskId,
Attachment attachment,
AccountId accountId,
String baseDownloadUrl,
{CancelToken? cancelToken,}
) async* {
final onProgressStream = StreamController<Either<Failure, Success>>();
try {
yield Right(GettingTextDataFromAttachment(attachment: attachment));
final downloadState = await _downloadAttachmentForWebInteractor.execute(
taskId,
attachment,
accountId,
baseDownloadUrl,
onProgressStream,
cancelToken: cancelToken,
).last;
yield downloadState.fold(
(failure) {
return Left(GetTextDataFromAttachmentFailure(
exception: failure is FeatureFailure ? failure.exception : null,
));
},
(success) {
if (success is! DownloadAttachmentForWebSuccess) {
return Left(GetTextDataFromAttachmentFailure());
}
return Right(GetTextDataFromAttachmentSuccess(
attachment: attachment,
data: success.bytes,
));
},
);
} catch (e) {
logError('GetTextDataFromAttachmentInteractor::execute(): $e');
yield Left(GetTextDataFromAttachmentFailure(exception: e));
}
onProgressStream.close();
}
}
@@ -22,9 +22,7 @@ import 'package:tmail_ui_user/features/email/domain/usecases/download_attachment
import 'package:tmail_ui_user/features/email/domain/usecases/export_all_attachments_interactor.dart'; import 'package:tmail_ui_user/features/email/domain/usecases/export_all_attachments_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/export_attachment_interactor.dart'; import 'package:tmail_ui_user/features/email/domain/usecases/export_attachment_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_email_content_interactor.dart'; import 'package:tmail_ui_user/features/email/domain/usecases/get_email_content_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_image_data_from_attachment_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_stored_email_state_interactor.dart'; import 'package:tmail_ui_user/features/email/domain/usecases/get_stored_email_state_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_text_data_from_attachment_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_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/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/move_to_mailbox_interactor.dart';
@@ -86,8 +84,6 @@ class EmailBindings extends BaseBindings {
Get.find<GetHtmlContentFromAttachmentInteractor>(), Get.find<GetHtmlContentFromAttachmentInteractor>(),
Get.find<DownloadAllAttachmentsForWebInteractor>(), Get.find<DownloadAllAttachmentsForWebInteractor>(),
Get.find<ExportAllAttachmentsInteractor>(), Get.find<ExportAllAttachmentsInteractor>(),
Get.find<GetImageDataFromAttachmentInteractor>(),
Get.find<GetTextDataFromAttachmentInteractor>(),
)); ));
} }
@@ -186,12 +182,6 @@ class EmailBindings extends BaseBindings {
Get.find<AccountRepository>(), Get.find<AccountRepository>(),
Get.find<AuthenticationOIDCRepository>(), Get.find<AuthenticationOIDCRepository>(),
Get.find<CredentialRepository>())); Get.find<CredentialRepository>()));
Get.lazyPut(() => GetImageDataFromAttachmentInteractor(
Get.find<DownloadAttachmentForWebInteractor>(),
));
Get.lazyPut(() => GetTextDataFromAttachmentInteractor(
Get.find<DownloadAttachmentForWebInteractor>(),
));
} }
@override @override
@@ -17,6 +17,7 @@ import 'package:flutter_file_dialog/flutter_file_dialog.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:get/get_navigation/src/dialog/dialog_route.dart';
import 'package:http_parser/http_parser.dart'; import 'package:http_parser/http_parser.dart';
import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart'; import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
@@ -64,8 +65,6 @@ import 'package:tmail_ui_user/features/email/domain/state/export_all_attachments
import 'package:tmail_ui_user/features/email/domain/state/export_attachment_state.dart'; import 'package:tmail_ui_user/features/email/domain/state/export_attachment_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/get_email_content_state.dart'; import 'package:tmail_ui_user/features/email/domain/state/get_email_content_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/get_html_content_from_attachment_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/get_image_data_from_attachment_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/get_text_data_from_attachment_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/mark_as_email_read_state.dart'; import 'package:tmail_ui_user/features/email/domain/state/mark_as_email_read_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/mark_as_email_star_state.dart'; import 'package:tmail_ui_user/features/email/domain/state/mark_as_email_star_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/move_to_mailbox_state.dart'; import 'package:tmail_ui_user/features/email/domain/state/move_to_mailbox_state.dart';
@@ -80,8 +79,6 @@ import 'package:tmail_ui_user/features/email/domain/state/unsubscribe_email_stat
import 'package:tmail_ui_user/features/email/domain/usecases/calendar_event_accept_interactor.dart'; import 'package:tmail_ui_user/features/email/domain/usecases/calendar_event_accept_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_all_attachments_for_web_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/export_all_attachments_interactor.dart'; import 'package:tmail_ui_user/features/email/domain/usecases/export_all_attachments_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_image_data_from_attachment_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_text_data_from_attachment_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/mark_as_star_email_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/maybe_calendar_event_interactor.dart'; import 'package:tmail_ui_user/features/email/domain/usecases/maybe_calendar_event_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/calendar_event_reject_interactor.dart'; import 'package:tmail_ui_user/features/email/domain/usecases/calendar_event_reject_interactor.dart';
@@ -146,6 +143,7 @@ import 'package:tmail_ui_user/main/routes/route_navigation.dart';
import 'package:tmail_ui_user/main/routes/route_utils.dart'; import 'package:tmail_ui_user/main/routes/route_utils.dart';
import 'package:tmail_ui_user/main/utils/app_utils.dart'; import 'package:tmail_ui_user/main/utils/app_utils.dart';
import 'package:twake_previewer_flutter/core/constants/supported_charset.dart'; import 'package:twake_previewer_flutter/core/constants/supported_charset.dart';
import 'package:twake_previewer_flutter/core/previewer_options/options/previewer_state.dart';
import 'package:twake_previewer_flutter/core/previewer_options/options/top_bar_options.dart'; import 'package:twake_previewer_flutter/core/previewer_options/options/top_bar_options.dart';
import 'package:twake_previewer_flutter/core/previewer_options/previewer_options.dart'; import 'package:twake_previewer_flutter/core/previewer_options/previewer_options.dart';
import 'package:twake_previewer_flutter/twake_image_previewer/twake_image_previewer.dart'; import 'package:twake_previewer_flutter/twake_image_previewer/twake_image_previewer.dart';
@@ -176,8 +174,6 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
final GetHtmlContentFromAttachmentInteractor _getHtmlContentFromAttachmentInteractor; final GetHtmlContentFromAttachmentInteractor _getHtmlContentFromAttachmentInteractor;
final DownloadAllAttachmentsForWebInteractor _downloadAllAttachmentsForWebInteractor; final DownloadAllAttachmentsForWebInteractor _downloadAllAttachmentsForWebInteractor;
final ExportAllAttachmentsInteractor _exportAllAttachmentsInteractor; final ExportAllAttachmentsInteractor _exportAllAttachmentsInteractor;
final GetImageDataFromAttachmentInteractor _getImageDataFromAttachmentInteractor;
final GetTextDataFromAttachmentInteractor _getTextDataFromAttachmentInteractor;
CreateNewEmailRuleFilterInteractor? _createNewEmailRuleFilterInteractor; CreateNewEmailRuleFilterInteractor? _createNewEmailRuleFilterInteractor;
SendReceiptToSenderInteractor? _sendReceiptToSenderInteractor; SendReceiptToSenderInteractor? _sendReceiptToSenderInteractor;
@@ -236,8 +232,6 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
this._getHtmlContentFromAttachmentInteractor, this._getHtmlContentFromAttachmentInteractor,
this._downloadAllAttachmentsForWebInteractor, this._downloadAllAttachmentsForWebInteractor,
this._exportAllAttachmentsInteractor, this._exportAllAttachmentsInteractor,
this._getImageDataFromAttachmentInteractor,
this._getTextDataFromAttachmentInteractor,
); );
@override @override
@@ -315,32 +309,6 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
)); ));
} else if (success is GettingHtmlContentFromAttachment) { } else if (success is GettingHtmlContentFromAttachment) {
_updateAttachmentsViewState(success.attachment.blobId, Right(success)); _updateAttachmentsViewState(success.attachment.blobId, Right(success));
} else if (success is GettingImageDataFromAttachment) {
_updateAttachmentsViewState(success.attachment.blobId, Right(success));
} else if (success is GetImageDataFromAttachmentSuccess) {
_updateAttachmentsViewState(success.attachment.blobId, Right(success));
Get.dialog(TwakeImagePreviewer(
bytes: success.data,
topBarOptions: TopBarOptions(
title: success.attachment.generateFileName(),
onClose: popBack,
),
));
} else if (success is GettingTextDataFromAttachment) {
_updateAttachmentsViewState(success.attachment.blobId, Right(success));
} else if (success is GetTextDataFromAttachmentSuccess) {
_updateAttachmentsViewState(success.attachment.blobId, Right(success));
Get.dialog(TwakePlainTextPreviewer(
supportedCharset: SupportedCharset.utf8,
bytes: success.data,
previewerOptions: PreviewerOptions(
width: currentContext == null ? 200 : currentContext!.width * 0.8,
),
topBarOptions: TopBarOptions(
title: success.attachment.generateFileName(),
onClose: popBack,
),
));
} }
} }
@@ -483,7 +451,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
state.fold( state.fold(
(failure) => null, (failure) => null,
(success) { (success) {
if (success is StartDownloadAttachmentForWeb) { if (success is StartDownloadAttachmentForWeb && !success.previewerSupported) {
emailSupervisorController.mailboxDashBoardController.addDownloadTask( emailSupervisorController.mailboxDashBoardController.addDownloadTask(
DownloadTaskState( DownloadTaskState(
taskId: success.taskId, taskId: success.taskId,
@@ -492,7 +460,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
), ),
); );
if (currentOverlayContext != null && currentContext != null) { if (currentOverlayContext != null && currentContext != null && !success.previewerSupported) {
appToast.showToastMessage( appToast.showToastMessage(
currentOverlayContext!, currentOverlayContext!,
AppLocalizations.of(currentContext!).your_download_has_started, AppLocalizations.of(currentContext!).your_download_has_started,
@@ -1034,7 +1002,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
await FlutterFileDialog.saveFile(params: params); await FlutterFileDialog.saveFile(params: params);
} }
void downloadAttachmentForWeb(Attachment attachment) { void downloadAttachmentForWeb(Attachment attachment, {bool previewerSupported = false}) {
if (accountId != null && session != null) { if (accountId != null && session != null) {
final generateTaskId = DownloadTaskId(uuid.v4()); final generateTaskId = DownloadTaskId(uuid.v4());
try { try {
@@ -1049,6 +1017,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
baseDownloadUrl, baseDownloadUrl,
_downloadProgressStateController, _downloadProgressStateController,
cancelToken: cancelToken, cancelToken: cancelToken,
previewerSupported: previewerSupported,
)); ));
} catch (e) { } catch (e) {
logError('SingleEmailController::downloadAttachmentForWeb(): $e'); logError('SingleEmailController::downloadAttachmentForWeb(): $e');
@@ -1130,9 +1099,57 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
mailboxDashBoardController.deleteDownloadTask(success.taskId); mailboxDashBoardController.deleteDownloadTask(success.taskId);
_downloadManager.createAnchorElementDownloadFileWeb( if (!success.previewerSupported) {
_downloadManager.createAnchorElementDownloadFileWeb(
success.bytes, success.bytes,
success.attachment.generateFileName()); success.attachment.generateFileName());
return;
}
if (success.attachment.isImage) {
_updateAttachmentsViewState(success.attachment.blobId, Right(success));
Navigator.of(currentContext!).push(GetDialogRoute(
pageBuilder: (context, _, __) => PointerInterceptor(child: TwakeImagePreviewer(
bytes: success.bytes,
zoomable: true,
previewerOptions: const PreviewerOptions(
previewerState: PreviewerState.success,
),
topBarOptions: TopBarOptions(
title: success.attachment.generateFileName(),
onClose: () => Navigator.maybePop(context),
onDownload: currentContext == null
? null
: () => handleDownloadAttachmentAction(
currentContext!,
success.attachment),
),
)),
barrierDismissible: false,
));
} else if (success.attachment.isText || success.attachment.isJson) {
_updateAttachmentsViewState(success.attachment.blobId, Right(success));
Navigator.of(currentContext!).push(GetDialogRoute(
pageBuilder: (context, _, __) => PointerInterceptor(child: TwakePlainTextPreviewer(
supportedCharset: SupportedCharset.utf8,
bytes: success.bytes,
previewerOptions: PreviewerOptions(
previewerState: PreviewerState.success,
width: currentContext == null ? 200 : currentContext!.width * 0.8,
),
topBarOptions: TopBarOptions(
title: success.attachment.generateFileName(),
onClose: () => Navigator.maybePop(context),
onDownload: currentContext == null
? null
: () => handleDownloadAttachmentAction(
currentContext!,
success.attachment),
),
)),
barrierDismissible: false,
));
}
} }
void _downloadPDFFile(Uint8List bytes, String fileName) { void _downloadPDFFile(Uint8List bytes, String fileName) {
@@ -2193,9 +2210,13 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
); );
} }
void handleDownloadAttachmentAction(BuildContext context, Attachment attachment) { void handleDownloadAttachmentAction(
BuildContext context,
Attachment attachment,
{bool previewerSupported = false}
) {
if (PlatformInfo.isWeb) { if (PlatformInfo.isWeb) {
downloadAttachmentForWeb(attachment); downloadAttachmentForWeb(attachment, previewerSupported: previewerSupported);
} else if (PlatformInfo.isMobile) { } else if (PlatformInfo.isMobile) {
exportAttachment(context, attachment); exportAttachment(context, attachment);
} else { } else {
@@ -2241,37 +2262,19 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
customTextTransformers: [const StandardizeHtmlSanitizingTransformers()], customTextTransformers: [const StandardizeHtmlSanitizingTransformers()],
), ),
)); ));
} else if (attachment.isImage) {
final attachmentEvaluation = evaluateAttachment(attachment);
if (!attachmentEvaluation.canDownloadAttachment) {
consumeState(Stream.value(Left(GetImageDataFromAttachmentFailure())));
return;
}
consumeState(_getImageDataFromAttachmentInteractor.execute(
DownloadTaskId(uuid.v4()),
attachment,
attachmentEvaluation.accountId!,
attachmentEvaluation.downloadUrl!,
));
} else if (attachment.isText) {
final attachmentEvaluation = evaluateAttachment(attachment);
if (!attachmentEvaluation.canDownloadAttachment) {
consumeState(Stream.value(Left(GetTextDataFromAttachmentFailure())));
return;
}
consumeState(_getTextDataFromAttachmentInteractor.execute(
DownloadTaskId(uuid.v4()),
attachment,
attachmentEvaluation.accountId!,
attachmentEvaluation.downloadUrl!,
));
} else { } else {
handleDownloadAttachmentAction(context, attachment); handleDownloadAttachmentAction(
context,
attachment,
previewerSupported: attachmentPreviewSupported(attachment),
);
} }
} }
bool attachmentPreviewSupported(Attachment attachment) {
return attachment.isImage || attachment.isText || attachment.isJson;
}
({ ({
bool canDownloadAttachment, bool canDownloadAttachment,
AccountId? accountId, AccountId? accountId,
@@ -46,4 +46,7 @@ extension AttachmentExtension on Attachment {
bool get isText => (type?.isTextFile() ?? false) bool get isText => (type?.isTextFile() ?? false)
|| name?.endsWith('.txt') == true || name?.endsWith('.txt') == true
|| name?.endsWith('.md') == true; || name?.endsWith('.md') == true;
bool get isJson => (type?.isJsonFile() ?? false)
|| name?.endsWith('.json') == true;
} }
@@ -18,8 +18,6 @@ import 'package:jmap_dart_client/jmap/mail/email/email_address.dart';
import 'package:model/email/attachment.dart'; import 'package:model/email/attachment.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/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/get_html_content_from_attachment_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/get_image_data_from_attachment_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/get_text_data_from_attachment_state.dart';
import 'package:tmail_ui_user/features/email/presentation/model/email_unsubscribe.dart'; import 'package:tmail_ui_user/features/email/presentation/model/email_unsubscribe.dart';
import 'package:tmail_ui_user/features/email/presentation/styles/attachment/attachment_item_widget_style.dart'; import 'package:tmail_ui_user/features/email/presentation/styles/attachment/attachment_item_widget_style.dart';
import 'package:tmail_ui_user/features/email/presentation/styles/email_attachments_styles.dart'; import 'package:tmail_ui_user/features/email/presentation/styles/email_attachments_styles.dart';
@@ -78,8 +76,6 @@ class EmailUtils {
(success) { (success) {
return success is DownloadAttachmentForWebSuccess return success is DownloadAttachmentForWebSuccess
|| success is GetHtmlContentFromAttachmentSuccess || success is GetHtmlContentFromAttachmentSuccess
|| success is GetImageDataFromAttachmentSuccess
|| success is GetTextDataFromAttachmentSuccess
|| success is IdleDownloadAttachmentForWeb; || success is IdleDownloadAttachmentForWeb;
}) ?? false; }) ?? false;
} }
@@ -154,8 +154,8 @@ class EmailViewAppBarWidget extends StatelessWidget {
return PopScope( return PopScope(
canPop: false, canPop: false,
onPopInvokedWithResult: (_, __) { onPopInvokedWithResult: (_, __) {
if (!PlatformInfo.isAndroid) return;
onBackAction(); onBackAction();
return;
}, },
child: child, child: child,
); );
@@ -31,8 +31,6 @@ import 'package:tmail_ui_user/features/email/domain/usecases/calendar_event_acce
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_all_attachments_for_web_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/export_all_attachments_interactor.dart'; import 'package:tmail_ui_user/features/email/domain/usecases/export_all_attachments_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_html_content_from_attachment_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_image_data_from_attachment_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_text_data_from_attachment_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/maybe_calendar_event_interactor.dart'; import 'package:tmail_ui_user/features/email/domain/usecases/maybe_calendar_event_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/calendar_event_reject_interactor.dart'; import 'package:tmail_ui_user/features/email/domain/usecases/calendar_event_reject_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/download_attachment_for_web_interactor.dart';
@@ -118,8 +116,6 @@ const fallbackGenerators = {
MockSpec<DownloadAllAttachmentsForWebInteractor>(), MockSpec<DownloadAllAttachmentsForWebInteractor>(),
MockSpec<ExportAllAttachmentsInteractor>(), MockSpec<ExportAllAttachmentsInteractor>(),
MockSpec<FileUploader>(), MockSpec<FileUploader>(),
MockSpec<GetImageDataFromAttachmentInteractor>(),
MockSpec<GetTextDataFromAttachmentInteractor>(),
]) ])
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
@@ -161,8 +157,6 @@ void main() {
final downloadAllAttachmentsForWebInteractor = MockDownloadAllAttachmentsForWebInteractor(); final downloadAllAttachmentsForWebInteractor = MockDownloadAllAttachmentsForWebInteractor();
final exportAllAttachmentsInteractor = MockExportAllAttachmentsInteractor(); final exportAllAttachmentsInteractor = MockExportAllAttachmentsInteractor();
final getImageDataFromAttachmentInteractor = MockGetImageDataFromAttachmentInteractor();
final getTextDataFromAttachmentInteractor = MockGetTextDataFromAttachmentInteractor();
late SingleEmailController singleEmailController; late SingleEmailController singleEmailController;
@@ -223,8 +217,6 @@ void main() {
getHtmlContentFromAttachmentInteractor, getHtmlContentFromAttachmentInteractor,
downloadAllAttachmentsForWebInteractor, downloadAllAttachmentsForWebInteractor,
exportAllAttachmentsInteractor, exportAllAttachmentsInteractor,
getImageDataFromAttachmentInteractor,
getTextDataFromAttachmentInteractor,
); );
}); });