Webview for external panels and everything

This commit is contained in:
Yegor Vialov 2020-01-27 21:25:55 +00:00
parent cf6039b279
commit fda8fb7182
5 changed files with 39 additions and 3 deletions

View File

@ -201,6 +201,16 @@ class HAClientApp extends StatelessWidget {
mediaType: "${ModalRoute.of(context).settings.arguments != null ? (ModalRoute.of(context).settings.arguments as Map)['type'] ?? '' : ''}",
),
"/log-view": (context) => LogViewPage(title: "Log"),
"/webview": (context) => WebviewScaffold(
url: "${(ModalRoute.of(context).settings.arguments as Map)['url']}",
appBar: new AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => Navigator.of(context).pop()
),
title: new Text("${(ModalRoute.of(context).settings.arguments as Map)['title']}"),
),
),
"/whats-new": (context) => WhatsNewPage(),
"/auth": (context) => new WebviewScaffold(
url: "${ConnectionManager().oauthUrl}",

View File

@ -376,7 +376,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
children: <Widget>[
Text("${panel.title}"),
Container(width: 4.0,),
panel.isWebView ? Text("WEB", style: TextStyle(fontSize: 8.0, color: Colors.black45),) : Container(width: 1.0,)
panel.isWebView ? Text("webview", style: TextStyle(fontSize: 8.0, color: Colors.black45),) : Container(width: 1.0,)
],
),
onTap: () {
@ -892,6 +892,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.dispose();
_viewsTabController?.dispose();
_stateSubscription?.cancel();
_settingsSubscription?.cancel();

View File

@ -35,7 +35,7 @@ class Panel {
)
);
} else {
Launcher.launchURLInCustomTab(url: "${ConnectionManager().httpWebHost}/$urlPath");
Launcher.launchAuthenticatedWebView(context: context, url: "${ConnectionManager().httpWebHost}/$urlPath", title: "${this.title}");
}
}

View File

@ -19,7 +19,7 @@ class LinkToWebConfig extends StatelessWidget {
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: Sizes.largeFontSize)),
subtitle: Text("Tap to open web version"),
onTap: () {
Launcher.launchURLInCustomTab(url: this.url);
Launcher.launchAuthenticatedWebView(context: context, url: this.url, title: this.name);
},
)
],

View File

@ -35,4 +35,28 @@ class Launcher {
}
}
static void launchAuthenticatedWebView({BuildContext context, String url, String title}) {
if (url.contains("?")) {
url += "&external_auth=1";
} else {
url += "?external_auth=1";
}
final flutterWebViewPlugin = new FlutterWebviewPlugin();
flutterWebViewPlugin.onStateChanged.listen((viewState) async {
if (viewState.type == WebViewState.startLoad) {
Logger.d("[WebView] Injecting external auth JS");
rootBundle.loadString('assets/js/externalAuth.js').then((js){
flutterWebViewPlugin.evalJavascript(js.replaceFirst("[token]", ConnectionManager()._token));
});
}
});
Navigator.of(context).pushNamed(
"/webview",
arguments: {
"url": "$url",
"title": "${title ?? ''}"
}
);
}
}