From fda8fb71825dfdf0dcf664de687d1b427edda6f0 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Mon, 27 Jan 2020 21:25:55 +0000 Subject: [PATCH] Webview for external panels and everything --- lib/main.dart | 10 +++++++++ lib/pages/main.page.dart | 4 +++- lib/panels/panel_class.dart | 2 +- lib/panels/widgets/link_to_web_config.dart | 2 +- lib/utils/launcher.dart | 24 ++++++++++++++++++++++ 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 967e514..5d6dcae 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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}", diff --git a/lib/pages/main.page.dart b/lib/pages/main.page.dart index 61901a4..e9d1a6b 100644 --- a/lib/pages/main.page.dart +++ b/lib/pages/main.page.dart @@ -376,7 +376,7 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker children: [ 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 with WidgetsBindingObserver, Ticker @override void dispose() { WidgetsBinding.instance.removeObserver(this); + final flutterWebviewPlugin = new FlutterWebviewPlugin(); + flutterWebviewPlugin.dispose(); _viewsTabController?.dispose(); _stateSubscription?.cancel(); _settingsSubscription?.cancel(); diff --git a/lib/panels/panel_class.dart b/lib/panels/panel_class.dart index 3b671af..2be9775 100644 --- a/lib/panels/panel_class.dart +++ b/lib/panels/panel_class.dart @@ -35,7 +35,7 @@ class Panel { ) ); } else { - Launcher.launchURLInCustomTab(url: "${ConnectionManager().httpWebHost}/$urlPath"); + Launcher.launchAuthenticatedWebView(context: context, url: "${ConnectionManager().httpWebHost}/$urlPath", title: "${this.title}"); } } diff --git a/lib/panels/widgets/link_to_web_config.dart b/lib/panels/widgets/link_to_web_config.dart index 3da9416..501f1b8 100644 --- a/lib/panels/widgets/link_to_web_config.dart +++ b/lib/panels/widgets/link_to_web_config.dart @@ -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); }, ) ], diff --git a/lib/utils/launcher.dart b/lib/utils/launcher.dart index a51fb65..8190457 100644 --- a/lib/utils/launcher.dart +++ b/lib/utils/launcher.dart @@ -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 ?? ''}" + } + ); + } + } \ No newline at end of file