add fallback if opening a link fails
This commit is contained in:
parent
98e7edd420
commit
9a8a54c4a3
2 changed files with 20 additions and 7 deletions
|
|
@ -1,17 +1,30 @@
|
||||||
import "package:flutter/services.dart";
|
import "package:flutter/services.dart";
|
||||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||||
|
import "package:material_ui/material_ui.dart";
|
||||||
|
import "package:nexus/helpers/extensions/show_error_snack_bar.dart";
|
||||||
|
import "package:nexus/main.dart";
|
||||||
import "package:url_launcher/url_launcher.dart" as ul;
|
import "package:url_launcher/url_launcher.dart" as ul;
|
||||||
|
|
||||||
class LaunchHelper(Ref ref) {
|
class LaunchHelper(Ref ref) {
|
||||||
Future<bool> launchUrl(Uri url, {bool useWebview = false}) async {
|
Future<bool> launchUrl(Uri url, {bool useWebview = false}) async {
|
||||||
try {
|
final launched = await ul
|
||||||
return await ul.launchUrl(
|
.launchUrl(
|
||||||
url,
|
url,
|
||||||
mode: useWebview ? .inAppBrowserView : .externalApplication,
|
mode: useWebview ? .inAppBrowserView : .externalApplication,
|
||||||
|
)
|
||||||
|
.catchError((_) => false, test: (error) => error is PlatformException);
|
||||||
|
|
||||||
|
if (navigatorKey.currentContext case final context?
|
||||||
|
when !launched && context.mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showErrorSnackBar(
|
||||||
|
"Couldn't open $url. Is there a browser installed?",
|
||||||
|
action: (
|
||||||
|
label: "Copy link",
|
||||||
|
onPressed: () => Clipboard.setData(.new(text: url.toString())),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
} on PlatformException catch (_) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return launched;
|
||||||
}
|
}
|
||||||
|
|
||||||
static final provider = Provider<LaunchHelper>(LaunchHelper.new);
|
static final provider = Provider<LaunchHelper>(LaunchHelper.new);
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ class const SelectServerPage({super.key}) extends HookConsumerWidget {
|
||||||
Future<void> tryLogin(Uri url) async {
|
Future<void> tryLogin(Uri url) async {
|
||||||
final OAuthAuthCodeResponse codeResponse;
|
final OAuthAuthCodeResponse codeResponse;
|
||||||
try {
|
try {
|
||||||
codeResponse = await ref.watch(
|
codeResponse = await ref.read(
|
||||||
AuthUrlController.provider(url).future,
|
AuthUrlController.provider(url).future,
|
||||||
);
|
);
|
||||||
} on GomuksException catch (error) {
|
} on GomuksException catch (error) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue