-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
launch_url [nfc]: Move launchUrl logic to a new file
- Move the existing launchUrl logic from content.dart to a new file named launch_url.dart. - Split function into pieces to handle realm-based and non-realm URLs. - Refactor error handling into a private function.
- Loading branch information
1 parent
baea6d7
commit ed81d05
Showing
2 changed files
with
65 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
|
||
import '../model/binding.dart'; | ||
import '../model/internal_link.dart'; | ||
import 'dialog.dart'; | ||
import 'message_list.dart'; | ||
import 'store.dart'; | ||
|
||
/// Handles showing an error dialog with a customizable message. | ||
Future<void> _showError(BuildContext context, String? message, String urlString) { | ||
return showErrorDialog( | ||
context: context, | ||
title: 'Unable to open link', | ||
message: [ | ||
'Link could not be opened: $urlString', | ||
if (message != null) message, | ||
].join("\n\n")); | ||
} | ||
|
||
/// Launches a URL without considering a realm base URL. | ||
void launchUrlWithoutRealm(BuildContext context, Uri url) async { | ||
bool launched = false; | ||
String? errorMessage; | ||
try { | ||
launched = await ZulipBinding.instance.launchUrl(url, | ||
mode: switch (defaultTargetPlatform) { | ||
// On iOS we prefer LaunchMode.externalApplication because (for | ||
// HTTP URLs) LaunchMode.platformDefault uses SFSafariViewController, | ||
// which gives an awkward UX as described here: | ||
// https://chat.zulip.org/#narrow/stream/48-mobile/topic/in-app.20browser/near/1169118 | ||
TargetPlatform.iOS => UrlLaunchMode.externalApplication, | ||
_ => UrlLaunchMode.platformDefault, | ||
}); | ||
} on PlatformException catch (e) { | ||
errorMessage = e.message; | ||
} | ||
if (!launched) { | ||
if (!context.mounted) return; | ||
await _showError(context, errorMessage, url.toString()); | ||
} | ||
} | ||
|
||
/// Launches a URL considering a realm base URL. | ||
void launchUrlWithRealm(BuildContext context, String urlString) async { | ||
final store = PerAccountStoreWidget.of(context); | ||
final url = store.tryResolveUrl(urlString); | ||
if (url == null) { // TODO(log) | ||
await _showError(context, null, urlString); | ||
return; | ||
} | ||
|
||
final internalNarrow = parseInternalLink(url, store); | ||
if (internalNarrow != null) { | ||
Navigator.push(context, | ||
MessageListPage.buildRoute(context: context, narrow: internalNarrow)); | ||
return; | ||
} | ||
|
||
launchUrlWithoutRealm(context, url); | ||
} |