WIP #339 Open unsupported Panels as authenticared webviews
This commit is contained in:
parent
691e48a36b
commit
f2fdfb0a32
16
assets/js/externalAuth.js
Normal file
16
assets/js/externalAuth.js
Normal file
@ -0,0 +1,16 @@
|
||||
window.externalApp = {};
|
||||
window.externalApp.getExternalAuth = function(options) {
|
||||
console.log("Starting external auth");
|
||||
var options = JSON.parse(options);
|
||||
if (options && options.callback) {
|
||||
var responseData = {
|
||||
access_token: "[token]",
|
||||
expires_in: 1800
|
||||
};
|
||||
console.log("Waiting for callback to be added");
|
||||
setTimeout(function(){
|
||||
console.log("Calling a callback");
|
||||
window[options.callback](true, responseData);
|
||||
}, 500);
|
||||
}
|
||||
};
|
@ -134,10 +134,10 @@ void main() async {
|
||||
};
|
||||
|
||||
runZoned(() {
|
||||
AndroidAlarmManager.initialize().then((_) {
|
||||
//AndroidAlarmManager.initialize().then((_) {
|
||||
runApp(new HAClientApp());
|
||||
print("Running MAIN isolate ${Isolate.current.hashCode}");
|
||||
});
|
||||
// print("Running MAIN isolate ${Isolate.current.hashCode}");
|
||||
//});
|
||||
|
||||
}, onError: (error, stack) {
|
||||
Logger.e("$error");
|
||||
@ -181,6 +181,16 @@ class HAClientApp extends StatelessWidget {
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
"/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']}"),
|
||||
),
|
||||
)
|
||||
},
|
||||
);
|
||||
@ -250,8 +260,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
||||
flutterLocalNotificationsPlugin.initialize(initializationSettings,
|
||||
onSelectNotification: onSelectNotification);
|
||||
|
||||
|
||||
|
||||
_settingsSubscription = eventBus.on<SettingsChangedEvent>().listen((event) {
|
||||
Logger.d("Settings change event: reconnect=${event.reconnect}");
|
||||
if (event.reconnect) {
|
||||
@ -495,6 +503,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
||||
);
|
||||
}
|
||||
|
||||
//TODO remove this shit
|
||||
void _callService(String domain, String service, String entityId, Map additionalParams) {
|
||||
_showInfoBottomBar(
|
||||
message: "Calling $domain.$service",
|
||||
|
@ -22,23 +22,35 @@ class Panel {
|
||||
if (icon == null || !icon.startsWith("mdi:")) {
|
||||
icon = Panel.iconsByComponent[type];
|
||||
}
|
||||
isHidden = (type != "iframe" && type != "config");
|
||||
Logger.d("New panel '$title'. type=$type, icon=$icon, urlPath=$urlPath");
|
||||
isHidden = (type == 'lovelace' || type == 'kiosk' || type == 'states');
|
||||
}
|
||||
|
||||
void handleOpen(BuildContext context) {
|
||||
if (type == "iframe") {
|
||||
Logger.d("Launching custom tab with ${config["url"]}");
|
||||
HAUtils.launchURLInCustomTab(context: context, url: config["url"]);
|
||||
} else if (type == "config") {
|
||||
if (type == "config") {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => PanelPage(title: "$title", panel: this),
|
||||
)
|
||||
);
|
||||
} else {
|
||||
String url = "${ConnectionManager().httpWebHost}/$urlPath";
|
||||
Logger.d("Launching custom tab with $url");
|
||||
HAUtils.launchURLInCustomTab(context: context, url: url);
|
||||
String url = "${ConnectionManager().httpWebHost}/$urlPath?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": "${this.title}"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,9 +50,9 @@ flutter:
|
||||
# the material Icons class.
|
||||
uses-material-design: true
|
||||
|
||||
# To add assets to your application, add an assets section, like this:
|
||||
assets:
|
||||
- images/hassio-192x192.png
|
||||
- assets/js/externalAuth.js
|
||||
|
||||
# An image asset can refer to one or more resolution-specific "variants", see
|
||||
# https://flutter.io/assets-and-images/#resolution-aware.
|
||||
|
Reference in New Issue
Block a user