From ae0aeab0482ee5beb3d70b2506b70c7d254f5dbf Mon Sep 17 00:00:00 2001 From: DatDang Date: Mon, 1 Apr 2024 14:09:35 +0700 Subject: [PATCH] TF-2533 Remove iframe listener dependency on dart --- .../html_content_viewer_on_web_widget.dart | 131 +++++++++--------- .../controller/single_email_controller.dart | 12 +- 2 files changed, 71 insertions(+), 72 deletions(-) diff --git a/core/lib/presentation/views/html_viewer/html_content_viewer_on_web_widget.dart b/core/lib/presentation/views/html_viewer/html_content_viewer_on_web_widget.dart index 065744c85..5e13e354d 100644 --- a/core/lib/presentation/views/html_viewer/html_content_viewer_on_web_widget.dart +++ b/core/lib/presentation/views/html_viewer/html_content_viewer_on_web_widget.dart @@ -52,7 +52,9 @@ class _HtmlContentViewerOnWebState extends State { String? _htmlData; bool _isLoading = true; double minHeight = 100; - final _jsonEncoder = const JsonEncoder(); + late final StreamSubscription sizeListener; + bool _iframeLoaded = false; + static const String iframeOnLoadMessage = 'iframeHasBeenLoaded'; @override void initState() { @@ -61,6 +63,57 @@ class _HtmlContentViewerOnWebState extends State { _actualWidth = widget.widthContent; _createdViewId = _getRandString(10); _setUpWeb(); + + sizeListener = html.window.onMessage.listen((event) { + var data = json.decode(event.data); + + if (data['view'] != _createdViewId) return; + + if (data['message'] == iframeOnLoadMessage) { + _iframeLoaded = true; + } + + if (!_iframeLoaded) return; + + if (data['type'] != null && data['type'].contains('toDart: htmlHeight')) { + final docHeight = data['height'] ?? _actualHeight; + if (docHeight != null && mounted) { + final scrollHeightWithBuffer = docHeight + 30.0; + if (scrollHeightWithBuffer > minHeight) { + setState(() { + _actualHeight = scrollHeightWithBuffer; + _isLoading = false; + }); + } + } + if (mounted && _isLoading) { + setState(() { + _isLoading = false; + }); + } + } + + if (data['type'] != null && data['type'].contains('toDart: htmlWidth')) { + final docWidth = data['width'] ?? _actualWidth; + if (docWidth != null && mounted) { + if (docWidth > _minWidth && widget.allowResizeToDocumentSize) { + setState(() { + _actualWidth = docWidth; + }); + } + } + } + + if (data['type'] != null && data['type'].contains('toDart: OpenLink')) { + final link = data['url']; + if (link != null && mounted) { + final urlString = link as String; + if (urlString.startsWith('mailto:')) { + widget.mailtoDelegate?.call(Uri.parse(urlString)); + } + } + } + }); } @override @@ -93,6 +146,7 @@ class _HtmlContentViewerOnWebState extends State { '''; @@ -184,53 +244,7 @@ class _HtmlContentViewerOnWebState extends State { ..style.border = 'none' ..style.overflow = 'hidden' ..style.width = '100%' - ..style.height = '100%' - ..onLoad.listen((event) async { - _sendMessageToWebViewForGetHeight(); - _sendMessageToWebViewForGetWidth(); - - html.window.onMessage.listen((event) { - var data = json.decode(event.data); - if (data['type'] != null && data['type'].contains('toDart: htmlHeight') && data['view'] == _createdViewId) { - final docHeight = data['height'] ?? _actualHeight; - if (docHeight != null && mounted) { - final scrollHeightWithBuffer = docHeight + 30.0; - if (scrollHeightWithBuffer > minHeight) { - setState(() { - _actualHeight = scrollHeightWithBuffer; - _isLoading = false; - }); - } - } - if (mounted && _isLoading) { - setState(() { - _isLoading = false; - }); - } - } - - if (data['type'] != null && data['type'].contains('toDart: htmlWidth') && data['view'] == _createdViewId) { - final docWidth = data['width'] ?? _actualWidth; - if (docWidth != null && mounted) { - if (docWidth > _minWidth && widget.allowResizeToDocumentSize) { - setState(() { - _actualWidth = docWidth; - }); - } - } - } - - if (data['type'] != null && data['type'].contains('toDart: OpenLink') && data['view'] == _createdViewId) { - final link = data['url']; - if (link != null && mounted) { - final urlString = link as String; - if (urlString.startsWith('mailto:')) { - widget.mailtoDelegate?.call(Uri.parse(urlString)); - } - } - } - }); - }); + ..style.height = '100%'; ui.platformViewRegistry.registerViewFactory(_createdViewId, (int viewId) => iframe); @@ -286,29 +300,10 @@ class _HtmlContentViewerOnWebState extends State { }); } - void _sendMessageToWebViewForGetHeight() { - final dataGetHeight = { - 'type': 'toIframe: getHeight', - 'view' : _createdViewId - }; - final jsonGetHeight = _jsonEncoder.convert(dataGetHeight); - - html.window.postMessage(jsonGetHeight, '*'); - } - - void _sendMessageToWebViewForGetWidth() { - final dataGetWidth = { - 'type': 'toIframe: getWidth', - 'view' : _createdViewId - }; - final jsonGetWidth = _jsonEncoder.convert(dataGetWidth); - - html.window.postMessage(jsonGetWidth, '*'); - } - @override void dispose() { _htmlData = null; + sizeListener.cancel(); super.dispose(); } } \ No newline at end of file diff --git a/lib/features/email/presentation/controller/single_email_controller.dart b/lib/features/email/presentation/controller/single_email_controller.dart index f0335d989..13ab26c23 100644 --- a/lib/features/email/presentation/controller/single_email_controller.dart +++ b/lib/features/email/presentation/controller/single_email_controller.dart @@ -257,7 +257,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin { emailSupervisorController.disposePageViewController(); } _updateCurrentEmailId(null); - _resetToOriginalValue(); + _resetToOriginalValue(isEmailClosing: true); mailboxDashBoardController.clearSelectedEmail(); mailboxDashBoardController.dispatchRoute(DashboardRoutes.thread); mailboxDashBoardController.clearEmailUIAction(); @@ -421,7 +421,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin { _identitySelected = identities.first; } - void _getEmailContentAction(EmailId emailId) async { + void _getEmailContentAction(EmailId emailId) { final emailLoaded = emailSupervisorController.getEmailInQueueByEmailId(emailId); if (emailLoaded != null) { @@ -578,7 +578,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin { } } - void _resetToOriginalValue() { + void _resetToOriginalValue({bool isEmailClosing = false}) { emailContents.value = null; _currentEmailLoaded = null; attachments.clear(); @@ -586,6 +586,10 @@ class SingleEmailController extends BaseController with AppLoaderMixin { calendarEvent.value = null; eventActions.clear(); emailUnsubscribe.value = null; + if (isEmailClosing) { + emailLoadedViewState.value = Right(UIState.idle); + viewState.value = Right(UIState.idle); + } } PresentationMailbox? getMailboxContain(PresentationEmail email) { @@ -1273,7 +1277,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin { } mailboxDashBoardController.clearSelectedEmail(); _updateCurrentEmailId(null); - _resetToOriginalValue(); + _resetToOriginalValue(isEmailClosing: true); _replaceBrowserHistory(); if (mailboxDashBoardController.searchController.isSearchEmailRunning) { if (context != null && responsiveUtils.isWebDesktop(context)) {