Settings loading refactored. WIP #338

This commit is contained in:
estevez-dev
2019-03-19 23:07:40 +02:00
parent d70ba0a55a
commit ccb88884a7
30 changed files with 163 additions and 85 deletions

View File

@ -21,10 +21,21 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
bool _useLovelace = true;
bool _newUseLovelace = true;
String oauthUrl;
final flutterWebviewPlugin = new FlutterWebviewPlugin();
@override
void initState() {
super.initState();
_loadSettings();
flutterWebviewPlugin.onUrlChanged.listen((String url) {
Logger.d("Launched url: $url");
if (url.startsWith("http://ha-client.homemade.systems/service/auth_callback.html")) {
String authCode = url.split("=")[1];
Logger.d("Auth code: $authCode");
flutterWebviewPlugin.close();
}
});
}
_loadSettings() async {
@ -40,6 +51,8 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
} catch (e) {
_useLovelace = _newUseLovelace = true;
}
oauthUrl = "${ _newSocketProtocol == "wss" ? "https" : "http"}://$_newHassioDomain:${_newHassioPort ?? ''}/auth/authorize?client_id=${Uri.encodeComponent('http://ha-client.homemade.systems/')}&redirect_uri=${Uri.encodeComponent('http://ha-client.homemade.systems/service/auth_callback.html')}";
Logger.d("OAuth url: $oauthUrl");
});
}
@ -67,6 +80,24 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
@override
Widget build(BuildContext context) {
Widget webViewButton;
if (oauthUrl != null) {
webViewButton = FlatButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => WebviewScaffold(
url: oauthUrl,
appBar: new AppBar(
title: new Text("Login"),
)
)
));
},
child: Text("Login with Home Assistant")
);
} else {
webViewButton = Container(height: 0.0,);
}
return new Scaffold(
appBar: new AppBar(
leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){
@ -149,6 +180,7 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
"Try ports 80 and 443 if default is not working and you don't know why.",
style: TextStyle(color: Colors.grey),
),
webViewButton,
new TextField(
decoration: InputDecoration(
labelText: "Access token"