Skip to content

Commit 1c97c8a

Browse files
dab246hoangdat
authored andcommitted
TF-3601 Fix preview EML file with long content in iOS
Signed-off-by: dab246 <[email protected]>
1 parent d01618c commit 1c97c8a

File tree

3 files changed

+203
-15
lines changed

3 files changed

+203
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import 'dart:async';
2+
3+
import 'package:core/data/constants/constant.dart';
4+
import 'package:core/presentation/views/html_viewer/html_content_viewer_widget.dart';
5+
import 'package:core/utils/html/html_interaction.dart';
6+
import 'package:core/utils/html/html_utils.dart';
7+
import 'package:flutter/cupertino.dart';
8+
import 'package:flutter/foundation.dart';
9+
import 'package:flutter/gestures.dart';
10+
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
11+
import 'package:url_launcher/url_launcher.dart' as launcher;
12+
import 'package:url_launcher/url_launcher_string.dart';
13+
14+
class IosHtmlContentViewerWidget extends StatefulWidget {
15+
16+
final String contentHtml;
17+
final TextDirection? direction;
18+
final bool useDefaultFont;
19+
final OnMailtoDelegateAction? onMailtoDelegateAction;
20+
final OnPreviewEMLDelegateAction? onPreviewEMLDelegateAction;
21+
final OnDownloadAttachmentDelegateAction? onDownloadAttachmentDelegateAction;
22+
23+
const IosHtmlContentViewerWidget({
24+
Key? key,
25+
required this.contentHtml,
26+
this.direction,
27+
this.useDefaultFont = false,
28+
this.onMailtoDelegateAction,
29+
this.onPreviewEMLDelegateAction,
30+
this.onDownloadAttachmentDelegateAction,
31+
}) : super(key: key);
32+
33+
@override
34+
State<StatefulWidget> createState() => _IosHtmlContentViewerWidgetState();
35+
}
36+
37+
class _IosHtmlContentViewerWidgetState extends State<IosHtmlContentViewerWidget> {
38+
39+
@override
40+
Widget build(BuildContext context) {
41+
return InAppWebView(
42+
initialSettings: InAppWebViewSettings(transparentBackground: true),
43+
onWebViewCreated: _onWebViewCreated,
44+
shouldOverrideUrlLoading: _shouldOverrideUrlLoading,
45+
gestureRecognizers: {
46+
Factory<LongPressGestureRecognizer>(() => LongPressGestureRecognizer()),
47+
},
48+
);
49+
}
50+
51+
Future<void> _onWebViewCreated(InAppWebViewController controller) async {
52+
await controller.loadData(data: HtmlUtils.generateHtmlDocument(
53+
content: widget.contentHtml,
54+
direction: widget.direction,
55+
javaScripts: HtmlInteraction.scriptsHandleLazyLoadingBackgroundImage,
56+
useDefaultFont: widget.useDefaultFont,
57+
));
58+
}
59+
60+
Future<NavigationActionPolicy?> _shouldOverrideUrlLoading(
61+
InAppWebViewController controller,
62+
NavigationAction navigationAction
63+
) async {
64+
final url = navigationAction.request.url?.toString();
65+
66+
if (url == null) {
67+
return NavigationActionPolicy.CANCEL;
68+
}
69+
70+
if (navigationAction.isForMainFrame && url == 'about:blank') {
71+
return NavigationActionPolicy.ALLOW;
72+
}
73+
74+
final requestUri = Uri.parse(url);
75+
if (widget.onMailtoDelegateAction != null &&
76+
requestUri.isScheme(Constant.mailtoScheme)) {
77+
await widget.onMailtoDelegateAction?.call(requestUri);
78+
return NavigationActionPolicy.CANCEL;
79+
}
80+
81+
if (widget.onPreviewEMLDelegateAction != null &&
82+
requestUri.isScheme(Constant.emlPreviewerScheme)) {
83+
await widget.onPreviewEMLDelegateAction?.call(requestUri);
84+
return NavigationActionPolicy.CANCEL;
85+
}
86+
87+
if (widget.onDownloadAttachmentDelegateAction != null &&
88+
requestUri.isScheme(Constant.attachmentScheme)) {
89+
await widget.onDownloadAttachmentDelegateAction?.call(requestUri);
90+
return NavigationActionPolicy.CANCEL;
91+
}
92+
93+
if (await launcher.canLaunchUrl(Uri.parse(url))) {
94+
await launcher.launchUrl(
95+
Uri.parse(url),
96+
mode: LaunchMode.externalApplication
97+
);
98+
}
99+
100+
return NavigationActionPolicy.CANCEL;
101+
}
102+
}

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

+26-3
Original file line numberDiff line numberDiff line change
@@ -2428,9 +2428,17 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
24282428
return;
24292429
}
24302430

2431-
showModalSheetToPreviewEMLAttachment(
2432-
currentContext!,
2433-
success.emlPreviewer);
2431+
if (PlatformInfo.isAndroid) {
2432+
showModalSheetToPreviewEMLAttachment(
2433+
currentContext!,
2434+
success.emlPreviewer,
2435+
);
2436+
} if (PlatformInfo.isIOS) {
2437+
showDialogToPreviewEMLAttachment(
2438+
currentContext!,
2439+
success.emlPreviewer,
2440+
);
2441+
}
24342442
}
24352443
}
24362444

@@ -2454,6 +2462,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
24542462
initialChildSize: 1.0,
24552463
builder: (context, ___) => EmailPreviewerDialogView(
24562464
emlPreviewer: emlPreviewer,
2465+
imagePaths: imagePaths,
24572466
onMailtoDelegateAction: openMailToLink,
24582467
onPreviewEMLDelegateAction: (uri) => _openEMLPreviewer(context, uri),
24592468
onDownloadAttachmentDelegateAction: (uri) =>
@@ -2464,6 +2473,20 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
24642473
);
24652474
}
24662475

2476+
void showDialogToPreviewEMLAttachment(BuildContext context, EMLPreviewer emlPreviewer) {
2477+
Get.dialog(
2478+
EmailPreviewerDialogView(
2479+
emlPreviewer: emlPreviewer,
2480+
imagePaths: imagePaths,
2481+
onMailtoDelegateAction: openMailToLink,
2482+
onPreviewEMLDelegateAction: (uri) => _openEMLPreviewer(context, uri),
2483+
onDownloadAttachmentDelegateAction: (uri) =>
2484+
_downloadAttachmentInEMLPreview(context, uri),
2485+
),
2486+
barrierColor: AppColor.colorDefaultCupertinoActionSheet,
2487+
);
2488+
}
2489+
24672490
Future<void> _openEMLPreviewer(BuildContext context, Uri? uri) async {
24682491
log('SingleEmailController::_openEMLPreviewer:uri = $uri');
24692492
if (uri == null) return;
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,102 @@
11

2+
import 'package:core/presentation/extensions/color_extension.dart';
3+
import 'package:core/presentation/resources/image_paths.dart';
4+
import 'package:core/presentation/views/button/tmail_button_widget.dart';
25
import 'package:core/presentation/views/html_viewer/html_content_viewer_widget.dart';
6+
import 'package:core/presentation/views/html_viewer/ios_html_content_viewer_widget.dart';
7+
import 'package:core/utils/platform_info.dart';
38
import 'package:flutter/material.dart';
49
import 'package:get/get.dart';
510
import 'package:tmail_ui_user/features/email/presentation/model/eml_previewer.dart';
11+
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
612
import 'package:tmail_ui_user/main/utils/app_utils.dart';
713

814
class EmailPreviewerDialogView extends StatelessWidget {
915

1016
final EMLPreviewer emlPreviewer;
17+
final ImagePaths imagePaths;
1118
final OnMailtoDelegateAction onMailtoDelegateAction;
1219
final OnPreviewEMLDelegateAction onPreviewEMLDelegateAction;
1320
final OnDownloadAttachmentDelegateAction onDownloadAttachmentDelegateAction;
1421

1522
const EmailPreviewerDialogView({
1623
super.key,
1724
required this.emlPreviewer,
25+
required this.imagePaths,
1826
required this.onMailtoDelegateAction,
1927
required this.onPreviewEMLDelegateAction,
2028
required this.onDownloadAttachmentDelegateAction,
2129
});
2230

2331
@override
2432
Widget build(BuildContext context) {
25-
return Scaffold(
26-
backgroundColor: Colors.white,
27-
body: SingleChildScrollView(
28-
child: HtmlContentViewer(
29-
contentHtml: emlPreviewer.content,
30-
initialWidth: context.width,
31-
direction: AppUtils.getCurrentDirection(context),
32-
onMailtoDelegateAction: onMailtoDelegateAction,
33-
onPreviewEMLDelegateAction: onPreviewEMLDelegateAction,
34-
onDownloadAttachmentDelegateAction: onDownloadAttachmentDelegateAction,
33+
if (PlatformInfo.isIOS) {
34+
return Dialog(
35+
shape: const RoundedRectangleBorder(
36+
borderRadius: BorderRadius.only(
37+
topRight: Radius.circular(16),
38+
topLeft: Radius.circular(16),
39+
),
3540
),
36-
),
37-
);
41+
insetPadding: EdgeInsets.zero,
42+
alignment: Alignment.center,
43+
backgroundColor: Colors.white,
44+
child: Container(
45+
decoration: const BoxDecoration(
46+
borderRadius: BorderRadius.only(
47+
topRight: Radius.circular(16),
48+
topLeft: Radius.circular(16),
49+
),
50+
),
51+
width: double.infinity,
52+
height: double.infinity,
53+
clipBehavior: Clip.antiAlias,
54+
child: Column(
55+
children: [
56+
SizedBox(
57+
height: 52,
58+
child: Row(
59+
children: [
60+
const Spacer(),
61+
TMailButtonWidget.fromIcon(
62+
icon: imagePaths.icComposerClose,
63+
backgroundColor: Colors.transparent,
64+
margin: const EdgeInsetsDirectional.only(end: 12),
65+
onTapActionCallback: popBack,
66+
)
67+
],
68+
),
69+
),
70+
const Divider(color: AppColor.colorDivider, height: 1),
71+
Expanded(
72+
child: IosHtmlContentViewerWidget(
73+
contentHtml: emlPreviewer.content,
74+
useDefaultFont: true,
75+
direction: AppUtils.getCurrentDirection(context),
76+
onMailtoDelegateAction: onMailtoDelegateAction,
77+
onPreviewEMLDelegateAction: onPreviewEMLDelegateAction,
78+
onDownloadAttachmentDelegateAction: onDownloadAttachmentDelegateAction,
79+
),
80+
),
81+
],
82+
),
83+
),
84+
);
85+
} else {
86+
return Scaffold(
87+
backgroundColor: Colors.white,
88+
body: SingleChildScrollView(
89+
child: HtmlContentViewer(
90+
contentHtml: emlPreviewer.content,
91+
initialWidth: context.width,
92+
useDefaultFont: true,
93+
direction: AppUtils.getCurrentDirection(context),
94+
onMailtoDelegateAction: onMailtoDelegateAction,
95+
onPreviewEMLDelegateAction: onPreviewEMLDelegateAction,
96+
onDownloadAttachmentDelegateAction: onDownloadAttachmentDelegateAction,
97+
),
98+
),
99+
);
100+
}
38101
}
39102
}

0 commit comments

Comments
 (0)