-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
launch_url: Deduplicate launchUrl functions
- Deduplicate logic for realm-based and non-realm URLs. - Utilize i18n for consistent error messaging. - Refactor error handling into a private function. - Move shared functionality to a new file: ```dart lib/widgets/launch_url.dart ```.
- Loading branch information
1 parent
f0c82eb
commit 3585ecd
Showing
3 changed files
with
66 additions
and
80 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 'package:flutter_gen/gen_l10n/zulip_localizations.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: ZulipLocalizations.of(context).errorUnableToOpenLinkTitle, | ||
message: [ | ||
ZulipLocalizations.of(context).errorLinkCouldNotBeOpened(urlString), | ||
if (message != null) message, | ||
].join("\n\n")); | ||
} | ||
|
||
/// Launches a URL without considering a realm base URL. | ||
void launchUrlWithoutRealm(BuildContext context, String urlString) async { | ||
bool launched = false; | ||
String? errorMessage; | ||
try { | ||
launched = await ZulipBinding.instance.launchUrl(Uri.parse(urlString), | ||
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, urlString); | ||
} | ||
} | ||
|
||
/// Launches a URL considering a realm base URL (if available). | ||
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.toString()); | ||
} |
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