2018-09-10 03:06:35 +03:00
|
|
|
part of 'main.dart';
|
|
|
|
|
2018-09-17 00:28:19 +03:00
|
|
|
class ConnectionSettingsPage extends StatefulWidget {
|
|
|
|
ConnectionSettingsPage({Key key, this.title}) : super(key: key);
|
2018-09-10 03:06:35 +03:00
|
|
|
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
@override
|
2018-09-17 00:28:19 +03:00
|
|
|
_ConnectionSettingsPageState createState() => new _ConnectionSettingsPageState();
|
2018-09-10 03:06:35 +03:00
|
|
|
}
|
|
|
|
|
2018-09-17 00:28:19 +03:00
|
|
|
class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
|
2018-09-10 03:06:35 +03:00
|
|
|
String _hassioDomain = "";
|
2018-10-06 20:03:20 +03:00
|
|
|
String _newHassioDomain = "";
|
2018-10-06 16:01:38 +03:00
|
|
|
String _hassioPort = "";
|
2018-10-06 20:03:20 +03:00
|
|
|
String _newHassioPort = "";
|
2018-09-10 03:06:35 +03:00
|
|
|
String _hassioPassword = "";
|
2018-10-06 20:03:20 +03:00
|
|
|
String _newHassioPassword = "";
|
2018-09-16 21:27:16 +03:00
|
|
|
String _socketProtocol = "wss";
|
2018-10-06 20:03:20 +03:00
|
|
|
String _newSocketProtocol = "wss";
|
2018-09-21 00:39:49 +03:00
|
|
|
String _authType = "access_token";
|
2018-10-06 20:03:20 +03:00
|
|
|
String _newAuthType = "access_token";
|
2018-10-06 16:01:38 +03:00
|
|
|
bool _edited = false;
|
2018-10-06 20:03:20 +03:00
|
|
|
FocusNode _domainFocusNode;
|
|
|
|
FocusNode _portFocusNode;
|
|
|
|
FocusNode _passwordFocusNode;
|
2018-09-10 03:06:35 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2018-10-06 20:03:20 +03:00
|
|
|
_domainFocusNode = FocusNode();
|
|
|
|
_portFocusNode = FocusNode();
|
|
|
|
_passwordFocusNode = FocusNode();
|
|
|
|
_domainFocusNode.addListener(_checkConfigChanged);
|
|
|
|
_portFocusNode.addListener(_checkConfigChanged);
|
|
|
|
_passwordFocusNode.addListener(_checkConfigChanged);
|
2018-09-10 03:06:35 +03:00
|
|
|
_loadSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadSettings() async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
setState(() {
|
2018-10-06 20:03:20 +03:00
|
|
|
_hassioDomain = _newHassioDomain = prefs.getString("hassio-domain")?? "";
|
|
|
|
_hassioPort = _newHassioPort = prefs.getString("hassio-port") ?? "";
|
|
|
|
_hassioPassword = _newHassioPassword = prefs.getString("hassio-password") ?? "";
|
|
|
|
_socketProtocol = _newSocketProtocol = prefs.getString("hassio-protocol") ?? 'wss';
|
|
|
|
_authType = _newAuthType = prefs.getString("hassio-auth-type") ?? 'access_token';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _checkConfigChanged() {
|
|
|
|
setState(() {
|
|
|
|
_edited = ((_newHassioPassword != _hassioPassword) ||
|
|
|
|
(_newHassioPort != _hassioPort) ||
|
|
|
|
(_newHassioDomain != _hassioDomain) ||
|
|
|
|
(_newSocketProtocol != _socketProtocol) ||
|
|
|
|
(_newAuthType != _authType));
|
2018-09-10 03:06:35 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_saveSettings() async {
|
2018-10-07 09:55:37 +03:00
|
|
|
if (_newHassioDomain.indexOf("http") == 0 && _newHassioDomain.indexOf("//") > 0) {
|
|
|
|
_newHassioDomain = _newHassioDomain.split("//")[1];
|
2018-09-23 22:24:48 +03:00
|
|
|
}
|
2018-09-10 03:06:35 +03:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2018-10-07 09:55:37 +03:00
|
|
|
prefs.setString("hassio-domain", _newHassioDomain);
|
|
|
|
prefs.setString("hassio-port", _newHassioPort);
|
|
|
|
prefs.setString("hassio-password", _newHassioPassword);
|
|
|
|
prefs.setString("hassio-protocol", _newSocketProtocol);
|
|
|
|
prefs.setString("hassio-res-protocol", _newSocketProtocol == "wss" ? "https" : "http");
|
|
|
|
prefs.setString("hassio-auth-type", _newAuthType);
|
2018-09-10 03:06:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return new Scaffold(
|
|
|
|
appBar: new AppBar(
|
|
|
|
leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){
|
2018-10-03 14:36:23 +03:00
|
|
|
Navigator.pop(context);
|
2018-09-10 03:06:35 +03:00
|
|
|
}),
|
|
|
|
title: new Text(widget.title),
|
2018-10-03 14:36:23 +03:00
|
|
|
actions: <Widget>[
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.check),
|
2018-10-06 16:01:38 +03:00
|
|
|
onPressed: _edited ? (){
|
2018-10-03 14:36:23 +03:00
|
|
|
_saveSettings().then((r){
|
|
|
|
Navigator.pop(context);
|
2018-10-06 16:01:38 +03:00
|
|
|
eventBus.fire(SettingsChangedEvent(true));
|
2018-10-03 14:36:23 +03:00
|
|
|
});
|
2018-10-06 16:01:38 +03:00
|
|
|
} : null
|
2018-10-03 14:36:23 +03:00
|
|
|
)
|
|
|
|
],
|
2018-09-10 03:06:35 +03:00
|
|
|
),
|
|
|
|
body: ListView(
|
|
|
|
padding: const EdgeInsets.all(20.0),
|
|
|
|
children: <Widget>[
|
2018-09-16 21:27:16 +03:00
|
|
|
new Row(
|
|
|
|
children: [
|
2018-09-24 10:27:08 +03:00
|
|
|
Text("Use ssl (HTTPS)"),
|
2018-09-16 21:27:16 +03:00
|
|
|
Switch(
|
2018-10-06 20:03:20 +03:00
|
|
|
value: (_newSocketProtocol == "wss"),
|
2018-09-16 21:27:16 +03:00
|
|
|
onChanged: (value) {
|
2018-10-06 20:03:20 +03:00
|
|
|
_newSocketProtocol = value ? "wss" : "ws";
|
|
|
|
_checkConfigChanged();
|
2018-09-16 21:27:16 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2018-09-10 03:06:35 +03:00
|
|
|
new TextField(
|
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: "Home Assistant domain or ip address"
|
|
|
|
),
|
2018-10-06 20:03:20 +03:00
|
|
|
controller: new TextEditingController.fromValue(
|
|
|
|
new TextEditingValue(
|
|
|
|
text: _newHassioDomain,
|
|
|
|
selection:
|
|
|
|
new TextSelection.collapsed(offset: _newHassioDomain.length)
|
|
|
|
)
|
2018-09-10 03:06:35 +03:00
|
|
|
),
|
|
|
|
onChanged: (value) {
|
2018-10-06 20:03:20 +03:00
|
|
|
_newHassioDomain = value;
|
2018-09-10 03:06:35 +03:00
|
|
|
},
|
2018-10-06 20:03:20 +03:00
|
|
|
focusNode: _domainFocusNode,
|
|
|
|
onEditingComplete: _checkConfigChanged,
|
2018-09-10 03:06:35 +03:00
|
|
|
),
|
|
|
|
new TextField(
|
|
|
|
decoration: InputDecoration(
|
2018-10-06 16:01:38 +03:00
|
|
|
labelText: "Home Assistant port (default is 8123)"
|
2018-09-10 03:06:35 +03:00
|
|
|
),
|
2018-10-06 20:03:20 +03:00
|
|
|
controller: new TextEditingController.fromValue(
|
|
|
|
new TextEditingValue(
|
|
|
|
text: _newHassioPort,
|
|
|
|
selection:
|
|
|
|
new TextSelection.collapsed(offset: _newHassioPort.length)
|
|
|
|
)
|
2018-09-10 03:06:35 +03:00
|
|
|
),
|
|
|
|
onChanged: (value) {
|
2018-10-06 20:03:20 +03:00
|
|
|
_newHassioPort = value;
|
2018-10-06 16:01:38 +03:00
|
|
|
//_saveSettings();
|
2018-09-10 03:06:35 +03:00
|
|
|
},
|
2018-10-06 20:03:20 +03:00
|
|
|
focusNode: _portFocusNode,
|
|
|
|
onEditingComplete: _checkConfigChanged,
|
2018-09-10 03:06:35 +03:00
|
|
|
),
|
2018-09-21 00:39:49 +03:00
|
|
|
new Row(
|
|
|
|
children: [
|
|
|
|
Text("Login with access token (HA >= 0.78.0)"),
|
|
|
|
Switch(
|
2018-10-06 20:03:20 +03:00
|
|
|
value: (_newAuthType == "access_token"),
|
2018-09-21 00:39:49 +03:00
|
|
|
onChanged: (value) {
|
2018-10-06 20:03:20 +03:00
|
|
|
_newAuthType = value ? "access_token" : "api_password";
|
|
|
|
_checkConfigChanged();
|
2018-10-06 16:01:38 +03:00
|
|
|
//_saveSettings();
|
2018-09-21 00:39:49 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2018-09-10 03:06:35 +03:00
|
|
|
new TextField(
|
|
|
|
decoration: InputDecoration(
|
2018-09-21 00:39:49 +03:00
|
|
|
labelText: _authType == "access_token" ? "Access token" : "API password"
|
2018-09-10 03:06:35 +03:00
|
|
|
),
|
2018-10-06 20:03:20 +03:00
|
|
|
controller: new TextEditingController.fromValue(
|
|
|
|
new TextEditingValue(
|
|
|
|
text: _newHassioPassword,
|
|
|
|
selection:
|
|
|
|
new TextSelection.collapsed(offset: _newHassioPassword.length)
|
|
|
|
)
|
2018-09-10 03:06:35 +03:00
|
|
|
),
|
|
|
|
onChanged: (value) {
|
2018-10-06 20:03:20 +03:00
|
|
|
_newHassioPassword = value;
|
2018-10-06 16:01:38 +03:00
|
|
|
//_saveSettings();
|
2018-09-10 03:06:35 +03:00
|
|
|
},
|
2018-10-06 20:03:20 +03:00
|
|
|
focusNode: _passwordFocusNode,
|
|
|
|
onEditingComplete: _checkConfigChanged,
|
2018-09-10 03:06:35 +03:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2018-10-06 20:03:20 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_domainFocusNode.removeListener(_checkConfigChanged);
|
|
|
|
_portFocusNode.removeListener(_checkConfigChanged);
|
|
|
|
_passwordFocusNode.removeListener(_checkConfigChanged);
|
|
|
|
_domainFocusNode.dispose();
|
|
|
|
_portFocusNode.dispose();
|
|
|
|
_passwordFocusNode.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
2018-09-24 10:27:08 +03:00
|
|
|
}
|