This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/utils/launcher.dart

61 lines
2.0 KiB
Dart
Raw Normal View History

part of '../main.dart';
class Launcher {
2020-05-01 18:48:54 +03:00
static void launchURLInBrowser(String url) async {
if (await urlLauncher.canLaunch(url)) {
await urlLauncher.launch(url);
} else {
Logger.e( "Could not launch $url");
}
}
static void launchURLInCustomTab({BuildContext context, String url, bool enableDefaultShare: true, bool showPageTitle: true}) async {
try {
await launch(
"$url",
option: new CustomTabsOption(
2019-10-20 20:18:23 +03:00
toolbarColor: context != null ? Theme.of(context).primaryColor : Colors.blue,
enableDefaultShare: enableDefaultShare,
enableUrlBarHiding: true,
showPageTitle: showPageTitle,
extraCustomTabs: <String>[
// ref. https://play.google.com/store/apps/details?id=org.mozilla.firefox
'org.mozilla.firefox',
// ref. https://play.google.com/store/apps/details?id=com.microsoft.emmx
'com.microsoft.emmx',
],
),
);
} catch (e) {
Logger.w("Can't open custom tab: ${e.toString()}");
Logger.w("Launching in default browser");
2020-05-01 18:48:54 +03:00
Launcher.launchURLInBrowser(url);
}
}
static void launchAuthenticatedWebView({BuildContext context, String url, String title}) {
if (url.contains("?")) {
url += "&external_auth=1";
} else {
url += "?external_auth=1";
}
2020-02-13 00:42:43 +02:00
final flutterWebViewPlugin = new standaloneWebview.FlutterWebviewPlugin();
flutterWebViewPlugin.onStateChanged.listen((viewState) async {
2020-02-13 00:42:43 +02:00
if (viewState.type == standaloneWebview.WebViewState.startLoad) {
Logger.d("[WebView] Injecting external auth JS");
rootBundle.loadString('assets/js/externalAuth.js').then((js){
2020-05-13 15:46:25 +03:00
flutterWebViewPlugin.evalJavascript(js.replaceFirst("[token]", AppSettings().longLivedToken));
});
}
});
Navigator.of(context).pushNamed(
"/webview",
arguments: {
"url": "$url",
"title": "${title ?? ''}"
}
);
}
}