Skip to content

Commit ef1d37f

Browse files
tddang-linagorahoangdat
authored andcommitted
TF-3450 Add text attachment preview
1 parent 3645fe5 commit ef1d37f

File tree

9 files changed

+150
-0
lines changed

9 files changed

+150
-0
lines changed

core/lib/domain/preview/supported_preview_file_types.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ class SupportedPreviewFileTypes {
55
'image/gif',
66
'image/png',];
77

8+
static const textMimeTypes = [
9+
'text/plain',
10+
'text/markdown',
11+
];
12+
813
static const docMimeTypes = [
914
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
1015
'application/vnd.oasis.opendocument.text',

core/lib/presentation/extensions/media_type_extension.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ extension MediaTypeExtension on MediaType {
2121

2222
bool isRtfFile() => SupportedPreviewFileTypes.rtfMimeTypes.contains(mimeType);
2323

24+
bool isTextFile() => SupportedPreviewFileTypes.textMimeTypes.contains(mimeType);
25+
2426
DocumentUti getDocumentUti() => DocumentUti(SupportedPreviewFileTypes.iOSSupportedTypes[mimeType]);
2527

2628
String getIcon(ImagePaths imagePaths, {String? fileName}) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'dart:typed_data';
2+
3+
import 'package:core/presentation/state/failure.dart';
4+
import 'package:core/presentation/state/success.dart';
5+
import 'package:model/email/attachment.dart';
6+
7+
class GettingTextDataFromAttachment extends LoadingState {
8+
GettingTextDataFromAttachment({required this.attachment});
9+
10+
final Attachment attachment;
11+
12+
@override
13+
List<Object> get props => [attachment];
14+
}
15+
16+
class GetTextDataFromAttachmentSuccess extends UIState {
17+
GetTextDataFromAttachmentSuccess({
18+
required this.attachment,
19+
required this.data,
20+
});
21+
22+
final Attachment attachment;
23+
final Uint8List data;
24+
25+
@override
26+
List<Object> get props => [attachment, data];
27+
}
28+
29+
class GetTextDataFromAttachmentFailure extends FeatureFailure {
30+
GetTextDataFromAttachmentFailure({super.exception});
31+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import 'dart:async';
2+
3+
import 'package:core/presentation/state/failure.dart';
4+
import 'package:core/presentation/state/success.dart';
5+
import 'package:core/utils/app_logger.dart';
6+
import 'package:dartz/dartz.dart';
7+
import 'package:dio/dio.dart';
8+
import 'package:jmap_dart_client/jmap/account_id.dart';
9+
import 'package:model/download/download_task_id.dart';
10+
import 'package:model/email/attachment.dart';
11+
import 'package:tmail_ui_user/features/email/domain/state/download_attachment_for_web_state.dart';
12+
import 'package:tmail_ui_user/features/email/domain/state/get_text_data_from_attachment_state.dart';
13+
import 'package:tmail_ui_user/features/email/domain/usecases/download_attachment_for_web_interactor.dart';
14+
15+
class GetTextDataFromAttachmentInteractor {
16+
const GetTextDataFromAttachmentInteractor(this._downloadAttachmentForWebInteractor);
17+
18+
final DownloadAttachmentForWebInteractor _downloadAttachmentForWebInteractor;
19+
20+
Stream<Either<Failure, Success>> execute(
21+
DownloadTaskId taskId,
22+
Attachment attachment,
23+
AccountId accountId,
24+
String baseDownloadUrl,
25+
{CancelToken? cancelToken,}
26+
) async* {
27+
final onProgressStream = StreamController<Either<Failure, Success>>();
28+
try {
29+
yield Right(GettingTextDataFromAttachment(attachment: attachment));
30+
final downloadState = await _downloadAttachmentForWebInteractor.execute(
31+
taskId,
32+
attachment,
33+
accountId,
34+
baseDownloadUrl,
35+
onProgressStream,
36+
cancelToken: cancelToken,
37+
).last;
38+
39+
yield downloadState.fold(
40+
(failure) {
41+
return Left(GetTextDataFromAttachmentFailure(
42+
exception: failure is FeatureFailure ? failure.exception : null,
43+
));
44+
},
45+
(success) {
46+
if (success is! DownloadAttachmentForWebSuccess) {
47+
return Left(GetTextDataFromAttachmentFailure());
48+
}
49+
50+
return Right(GetTextDataFromAttachmentSuccess(
51+
attachment: attachment,
52+
data: success.bytes,
53+
));
54+
},
55+
);
56+
} catch (e) {
57+
logError('GetTextDataFromAttachmentInteractor::execute(): $e');
58+
yield Left(GetTextDataFromAttachmentFailure(exception: e));
59+
}
60+
onProgressStream.close();
61+
}
62+
}

lib/features/email/presentation/bindings/email_bindings.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import 'package:tmail_ui_user/features/email/domain/usecases/export_attachment_i
2424
import 'package:tmail_ui_user/features/email/domain/usecases/get_email_content_interactor.dart';
2525
import 'package:tmail_ui_user/features/email/domain/usecases/get_image_data_from_attachment_interactor.dart';
2626
import 'package:tmail_ui_user/features/email/domain/usecases/get_stored_email_state_interactor.dart';
27+
import 'package:tmail_ui_user/features/email/domain/usecases/get_text_data_from_attachment_interactor.dart';
2728
import 'package:tmail_ui_user/features/email/domain/usecases/mark_as_email_read_interactor.dart';
2829
import 'package:tmail_ui_user/features/email/domain/usecases/mark_as_star_email_interactor.dart';
2930
import 'package:tmail_ui_user/features/email/domain/usecases/move_to_mailbox_interactor.dart';
@@ -86,6 +87,7 @@ class EmailBindings extends BaseBindings {
8687
Get.find<DownloadAllAttachmentsForWebInteractor>(),
8788
Get.find<ExportAllAttachmentsInteractor>(),
8889
Get.find<GetImageDataFromAttachmentInteractor>(),
90+
Get.find<GetTextDataFromAttachmentInteractor>(),
8991
));
9092
}
9193

@@ -187,6 +189,9 @@ class EmailBindings extends BaseBindings {
187189
Get.lazyPut(() => GetImageDataFromAttachmentInteractor(
188190
Get.find<DownloadAttachmentForWebInteractor>(),
189191
));
192+
Get.lazyPut(() => GetTextDataFromAttachmentInteractor(
193+
Get.find<DownloadAttachmentForWebInteractor>(),
194+
));
190195
}
191196

192197
@override

lib/features/email/presentation/controller/single_email_controller.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import 'package:tmail_ui_user/features/email/domain/state/export_attachment_stat
6565
import 'package:tmail_ui_user/features/email/domain/state/get_email_content_state.dart';
6666
import 'package:tmail_ui_user/features/email/domain/state/get_html_content_from_attachment_state.dart';
6767
import 'package:tmail_ui_user/features/email/domain/state/get_image_data_from_attachment_state.dart';
68+
import 'package:tmail_ui_user/features/email/domain/state/get_text_data_from_attachment_state.dart';
6869
import 'package:tmail_ui_user/features/email/domain/state/mark_as_email_read_state.dart';
6970
import 'package:tmail_ui_user/features/email/domain/state/mark_as_email_star_state.dart';
7071
import 'package:tmail_ui_user/features/email/domain/state/move_to_mailbox_state.dart';
@@ -80,6 +81,7 @@ import 'package:tmail_ui_user/features/email/domain/usecases/calendar_event_acce
8081
import 'package:tmail_ui_user/features/email/domain/usecases/download_all_attachments_for_web_interactor.dart';
8182
import 'package:tmail_ui_user/features/email/domain/usecases/export_all_attachments_interactor.dart';
8283
import 'package:tmail_ui_user/features/email/domain/usecases/get_image_data_from_attachment_interactor.dart';
84+
import 'package:tmail_ui_user/features/email/domain/usecases/get_text_data_from_attachment_interactor.dart';
8385
import 'package:tmail_ui_user/features/email/domain/usecases/mark_as_star_email_interactor.dart';
8486
import 'package:tmail_ui_user/features/email/domain/usecases/maybe_calendar_event_interactor.dart';
8587
import 'package:tmail_ui_user/features/email/domain/usecases/calendar_event_reject_interactor.dart';
@@ -143,8 +145,11 @@ import 'package:tmail_ui_user/main/routes/navigation_router.dart';
143145
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
144146
import 'package:tmail_ui_user/main/routes/route_utils.dart';
145147
import 'package:tmail_ui_user/main/utils/app_utils.dart';
148+
import 'package:twake_previewer_flutter/core/constants/supported_charset.dart';
146149
import 'package:twake_previewer_flutter/core/previewer_options/options/top_bar_options.dart';
150+
import 'package:twake_previewer_flutter/core/previewer_options/previewer_options.dart';
147151
import 'package:twake_previewer_flutter/twake_image_previewer/twake_image_previewer.dart';
152+
import 'package:twake_previewer_flutter/twake_plain_text_previewer/twake_plain_text_previewer.dart';
148153

149154
class SingleEmailController extends BaseController with AppLoaderMixin {
150155

@@ -172,6 +177,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
172177
final DownloadAllAttachmentsForWebInteractor _downloadAllAttachmentsForWebInteractor;
173178
final ExportAllAttachmentsInteractor _exportAllAttachmentsInteractor;
174179
final GetImageDataFromAttachmentInteractor _getImageDataFromAttachmentInteractor;
180+
final GetTextDataFromAttachmentInteractor _getTextDataFromAttachmentInteractor;
175181

176182
CreateNewEmailRuleFilterInteractor? _createNewEmailRuleFilterInteractor;
177183
SendReceiptToSenderInteractor? _sendReceiptToSenderInteractor;
@@ -231,6 +237,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
231237
this._downloadAllAttachmentsForWebInteractor,
232238
this._exportAllAttachmentsInteractor,
233239
this._getImageDataFromAttachmentInteractor,
240+
this._getTextDataFromAttachmentInteractor,
234241
);
235242

236243
@override
@@ -319,6 +326,21 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
319326
onClose: popBack,
320327
),
321328
));
329+
} else if (success is GettingTextDataFromAttachment) {
330+
_updateAttachmentsViewState(success.attachment.blobId, Right(success));
331+
} else if (success is GetTextDataFromAttachmentSuccess) {
332+
_updateAttachmentsViewState(success.attachment.blobId, Right(success));
333+
Get.dialog(TwakePlainTextPreviewer(
334+
supportedCharset: SupportedCharset.utf8,
335+
bytes: success.data,
336+
previewerOptions: PreviewerOptions(
337+
width: currentContext == null ? 200 : currentContext!.width * 0.8,
338+
),
339+
topBarOptions: TopBarOptions(
340+
title: success.attachment.generateFileName(),
341+
onClose: popBack,
342+
),
343+
));
322344
}
323345
}
324346

@@ -2232,6 +2254,19 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
22322254
attachmentEvaluation.accountId!,
22332255
attachmentEvaluation.downloadUrl!,
22342256
));
2257+
} else if (attachment.isText) {
2258+
final attachmentEvaluation = evaluateAttachment(attachment);
2259+
if (!attachmentEvaluation.canDownloadAttachment) {
2260+
consumeState(Stream.value(Left(GetTextDataFromAttachmentFailure())));
2261+
return;
2262+
}
2263+
2264+
consumeState(_getTextDataFromAttachmentInteractor.execute(
2265+
DownloadTaskId(uuid.v4()),
2266+
attachment,
2267+
attachmentEvaluation.accountId!,
2268+
attachmentEvaluation.downloadUrl!,
2269+
));
22352270
} else {
22362271
handleDownloadAttachmentAction(context, attachment);
22372272
}

lib/features/email/presentation/extensions/attachment_extension.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ extension AttachmentExtension on Attachment {
4242
bool get isHTMLFile => type?.isHTMLFile(fileName: name) ?? false;
4343

4444
bool get isImage => type?.isImageFile() ?? false;
45+
46+
bool get isText => (type?.isTextFile() ?? false)
47+
|| name?.endsWith('.txt') == true
48+
|| name?.endsWith('.md') == true;
4549
}

lib/features/email/presentation/utils/email_utils.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import 'package:model/email/attachment.dart';
1919
import 'package:tmail_ui_user/features/email/domain/state/download_attachment_for_web_state.dart';
2020
import 'package:tmail_ui_user/features/email/domain/state/get_html_content_from_attachment_state.dart';
2121
import 'package:tmail_ui_user/features/email/domain/state/get_image_data_from_attachment_state.dart';
22+
import 'package:tmail_ui_user/features/email/domain/state/get_text_data_from_attachment_state.dart';
2223
import 'package:tmail_ui_user/features/email/presentation/model/email_unsubscribe.dart';
2324
import 'package:tmail_ui_user/features/email/presentation/styles/attachment/attachment_item_widget_style.dart';
2425
import 'package:tmail_ui_user/features/email/presentation/styles/email_attachments_styles.dart';
@@ -78,6 +79,7 @@ class EmailUtils {
7879
return success is DownloadAttachmentForWebSuccess
7980
|| success is GetHtmlContentFromAttachmentSuccess
8081
|| success is GetImageDataFromAttachmentSuccess
82+
|| success is GetTextDataFromAttachmentSuccess
8183
|| success is IdleDownloadAttachmentForWeb;
8284
}) ?? false;
8385
}

test/features/email/presentation/controller/single_email_controller_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import 'package:tmail_ui_user/features/email/domain/usecases/download_all_attach
3232
import 'package:tmail_ui_user/features/email/domain/usecases/export_all_attachments_interactor.dart';
3333
import 'package:tmail_ui_user/features/email/domain/usecases/get_html_content_from_attachment_interactor.dart';
3434
import 'package:tmail_ui_user/features/email/domain/usecases/get_image_data_from_attachment_interactor.dart';
35+
import 'package:tmail_ui_user/features/email/domain/usecases/get_text_data_from_attachment_interactor.dart';
3536
import 'package:tmail_ui_user/features/email/domain/usecases/maybe_calendar_event_interactor.dart';
3637
import 'package:tmail_ui_user/features/email/domain/usecases/calendar_event_reject_interactor.dart';
3738
import 'package:tmail_ui_user/features/email/domain/usecases/download_attachment_for_web_interactor.dart';
@@ -118,6 +119,7 @@ const fallbackGenerators = {
118119
MockSpec<ExportAllAttachmentsInteractor>(),
119120
MockSpec<FileUploader>(),
120121
MockSpec<GetImageDataFromAttachmentInteractor>(),
122+
MockSpec<GetTextDataFromAttachmentInteractor>(),
121123
])
122124
void main() {
123125
TestWidgetsFlutterBinding.ensureInitialized();
@@ -160,6 +162,7 @@ void main() {
160162
final downloadAllAttachmentsForWebInteractor = MockDownloadAllAttachmentsForWebInteractor();
161163
final exportAllAttachmentsInteractor = MockExportAllAttachmentsInteractor();
162164
final getImageDataFromAttachmentInteractor = MockGetImageDataFromAttachmentInteractor();
165+
final getTextDataFromAttachmentInteractor = MockGetTextDataFromAttachmentInteractor();
163166

164167
late SingleEmailController singleEmailController;
165168

@@ -221,6 +224,7 @@ void main() {
221224
downloadAllAttachmentsForWebInteractor,
222225
exportAllAttachmentsInteractor,
223226
getImageDataFromAttachmentInteractor,
227+
getTextDataFromAttachmentInteractor,
224228
);
225229
});
226230

0 commit comments

Comments
 (0)