JS interface improvements

This commit is contained in:
Yegor Vialov 2020-02-12 22:42:43 +00:00
parent 6695756727
commit 6e448d3458
5 changed files with 16 additions and 13 deletions

View File

@ -30,6 +30,6 @@ window.externalApp.externalBus = function(message) {
window.externalBus(responseData);
}, 500);
} else if (messageObj.type == "config_screen/show") {
window.location.href = "htcmd://show-settings";
HAClient.postMessage('show-settings');
}
};

View File

@ -30,7 +30,7 @@ import 'package:workmanager/workmanager.dart' as workManager;
import 'package:geolocator/geolocator.dart';
import 'package:battery/battery.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart' as standaloneWebview;
import 'package:webview_flutter/webview_flutter.dart';
import 'utils/logger.dart';
@ -231,7 +231,7 @@ class _HAClientAppState extends State<HAClientApp> {
mediaType: "${ModalRoute.of(context).settings.arguments != null ? (ModalRoute.of(context).settings.arguments as Map)['type'] ?? '' : ''}",
),
"/log-view": (context) => LogViewPage(title: "Log"),
"/webview": (context) => WebviewScaffold(
"/webview": (context) => standaloneWebview.WebviewScaffold(
url: "${(ModalRoute.of(context).settings.arguments as Map)['url']}",
appBar: new AppBar(
leading: IconButton(
@ -242,7 +242,7 @@ class _HAClientAppState extends State<HAClientApp> {
),
),
"/whats-new": (context) => WhatsNewPage(),
"/auth": (context) => new WebviewScaffold(
"/auth": (context) => new standaloneWebview.WebviewScaffold(
url: "${ConnectionManager().oauthUrl}",
appBar: new AppBar(
leading: IconButton(

View File

@ -12,7 +12,7 @@ class AuthManager {
Future start({String oauthUrl}) {
Completer completer = Completer();
final flutterWebviewPlugin = new FlutterWebviewPlugin();
final flutterWebviewPlugin = new standaloneWebview.FlutterWebviewPlugin();
flutterWebviewPlugin.onUrlChanged.listen((String url) {
if (url.startsWith("https://ha-client.app/service/auth_callback.html")) {
Logger.d("url=$url");

View File

@ -871,10 +871,10 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
primary: false,
bottomNavigationBar: bottomBar,
body: Container(
color: Colors.white,
color: Colors.blue,
)
);
} else if (ConnectionManager().settingsLoaded && ConnectionManager().useWebView) {
} else if (ConnectionManager().settingsLoaded && ConnectionManager().useWebView && !_showLoginButton) {
return WillPopScope(
child: Scaffold(
primary: true,
@ -890,10 +890,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
Logger.d("[MainWebView] Page started: $url");
if (url.contains(ConnectionManager()._domain)) {
_loadJSInterface();
} else if (url.contains("htcmd://show-settings")) {
Navigator.of(context).pushNamed("/connection-settings").then((_) {
_mainWebViewController.goBack();
});
}
},
gestureNavigationEnabled: true,
@ -902,6 +898,13 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
() => new EagerGestureRecognizer(),
),
].toSet(),
javascriptChannels: {
new JavascriptChannel(name: 'HAClient', onMessageReceived: (JavascriptMessage message) {
if (message.message == "show-settings") {
Navigator.of(context).pushNamed("/connection-settings");
}
})
}
)
),
onWillPop: () {

View File

@ -41,9 +41,9 @@ class Launcher {
} else {
url += "?external_auth=1";
}
final flutterWebViewPlugin = new FlutterWebviewPlugin();
final flutterWebViewPlugin = new standaloneWebview.FlutterWebviewPlugin();
flutterWebViewPlugin.onStateChanged.listen((viewState) async {
if (viewState.type == WebViewState.startLoad) {
if (viewState.type == standaloneWebview.WebViewState.startLoad) {
Logger.d("[WebView] Injecting external auth JS");
rootBundle.loadString('assets/js/externalAuth.js').then((js){
flutterWebViewPlugin.evalJavascript(js.replaceFirst("[token]", ConnectionManager()._token));