Skip to content

Commit

Permalink
optionally specify an urlLauncherDelegate
Browse files Browse the repository at this point in the history
This should resolve #11
  • Loading branch information
robert-virkus committed Oct 16, 2021
1 parent 097667b commit 097f8cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/src/mime_message_downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class MimeMessageDownloader extends StatefulWidget {
final String? emptyMessageText;
final Future Function(Uri mailto, MimeMessage mimeMessage)? mailtoDelegate;
final Future Function(InteractiveMediaWidget mediaWidget)? showMediaDelegate;

/// Handler for any non-media URLs that the user taps on the website, return `true` when the given `url` was handled.
final Future<bool> Function(String url)? urlLauncherDelegate;
final void Function(InAppWebViewController controller)? onWebViewCreated;
final void Function(InAppWebViewController controller, double zoomFactor)?
onZoomed;
Expand Down Expand Up @@ -61,6 +64,7 @@ class MimeMessageDownloader extends StatefulWidget {
/// [emptyMessageText] The default text that should be shown for empty messages.
/// [mailtoDelegate] Handler for mailto: links. Typically you will want to open a new compose view prepulated with a `MessageBuilder.prepareMailtoBasedMessage(uri,from)` instance.
/// [showMediaDelegate] Handler for showing the given media widget, typically in its own screen
/// Specify a [urlLauncherDelegate] when you want to handle URL invocations yourself.
/// Set the [onWebViewCreated] callback if you want a reference to the [InAppWebViewController].
/// Set the [onZoomed] callback if you want to be notified when the webview is zoomed out after loading.
/// Set the [onError] callback in case you want to be notfied about processing errors such as format exceptions.
Expand All @@ -83,6 +87,7 @@ class MimeMessageDownloader extends StatefulWidget {
this.emptyMessageText,
this.mailtoDelegate,
this.showMediaDelegate,
this.urlLauncherDelegate,
this.onWebViewCreated,
this.onZoomed,
this.onError,
Expand Down Expand Up @@ -147,6 +152,7 @@ class _MimeMessageDownloaderState extends State<MimeMessageDownloader> {
emptyMessageText: widget.emptyMessageText,
mailtoDelegate: widget.mailtoDelegate,
showMediaDelegate: widget.showMediaDelegate,
urlLauncherDelegate: widget.urlLauncherDelegate,
maxImageWidth: widget.maxImageWidth,
onWebViewCreated: widget.onWebViewCreated,
onZoomed: widget.onZoomed,
Expand Down
14 changes: 13 additions & 1 deletion lib/src/mime_message_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class MimeMessageViewer extends StatelessWidget {
/// Handler for showing the given media widget, typically in its own screen
final Future Function(InteractiveMediaWidget mediaViewer)? showMediaDelegate;

/// Handler for any non-media URLs that the user taps on the website, return `true` when the given `url` was handled.
final Future<bool> Function(String url)? urlLauncherDelegate;

/// Register this callback if you want a reference to the [InAppWebViewController].
final void Function(InAppWebViewController controller)? onWebViewCreated;

Expand All @@ -69,7 +72,8 @@ class MimeMessageViewer extends StatelessWidget {
/// Set [enableDarkMode] to `true` to enforce dark mode on devices with older browsers.
/// [emptyMessageText] The default text that should be shown for empty messages.
/// [mailtoDelegate] Handler for mailto: links. Typically you will want to open a new compose view prepulated with a `MessageBuilder.prepareMailtoBasedMessage(uri,from)` instance.
/// [showMediaDelegate] Handler for showing the given media widget, typically in its own screen
/// [showMediaDelegate] Handler for showing the given media widget, typically in its own screen.
/// Specify a [urlLauncherDelegate] when you want to handle URL invocations yourself.
/// Optionally specify the [maxImageWidth] to set the maximum width for embedded images.
/// Set the [onWebViewCreated] callback if you want a reference to the [InAppWebViewController].
/// Set the [onZoomed] callback if you want to be notified when the webview is zoomed out after loading.
Expand All @@ -85,6 +89,7 @@ class MimeMessageViewer extends StatelessWidget {
this.emptyMessageText,
this.mailtoDelegate,
this.showMediaDelegate,
this.urlLauncherDelegate,
this.maxImageWidth,
this.onWebViewCreated,
this.onZoomed,
Expand Down Expand Up @@ -355,6 +360,13 @@ class _HtmlViewerState extends State<_HtmlMimeMessageViewer> {
return NavigationActionPolicy.CANCEL;
}
final url = requestUri.toString();
final urlDelegate = widget.config.urlLauncherDelegate;
if (urlDelegate != null) {
final handled = await urlDelegate(url);
if (handled) {
return NavigationActionPolicy.CANCEL;
}
}
if (await launcher.canLaunch(url)) {
await launcher.launch(url);
return NavigationActionPolicy.CANCEL;
Expand Down

0 comments on commit 097f8cd

Please sign in to comment.