2020-04-11 19:09:35 +03:00
|
|
|
part of '../../main.dart';
|
2018-09-10 03:06:35 +03:00
|
|
|
|
2018-09-17 00:28:19 +03:00
|
|
|
class ConnectionSettingsPage extends StatefulWidget {
|
2020-05-03 02:02:18 +03:00
|
|
|
ConnectionSettingsPage({Key key, this.title, this.quickStart: false}) : super(key: key);
|
2018-09-10 03:06:35 +03:00
|
|
|
|
|
|
|
final String title;
|
2020-05-03 02:02:18 +03:00
|
|
|
final bool quickStart;
|
2018-09-10 03:06:35 +03:00
|
|
|
|
|
|
|
@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> {
|
2020-05-03 02:02:18 +03:00
|
|
|
String _homeAssistantUrl = '';
|
|
|
|
String _deviceName;
|
|
|
|
bool _loaded = false;
|
|
|
|
bool _includeDeviceName = false;
|
2018-09-10 03:06:35 +03:00
|
|
|
|
2020-05-03 02:02:18 +03:00
|
|
|
final _formKey = GlobalKey<FormState>();
|
2019-03-19 23:07:40 +02:00
|
|
|
|
2018-09-10 03:06:35 +03:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2020-05-03 02:02:18 +03:00
|
|
|
if (!widget.quickStart) {
|
|
|
|
_loadSettings();
|
|
|
|
} else {
|
|
|
|
_deviceName = MobileAppIntegrationManager.getDefaultDeviceName();
|
2020-05-03 14:25:09 +03:00
|
|
|
_includeDeviceName = true;
|
2020-05-03 02:02:18 +03:00
|
|
|
_loaded = true;
|
|
|
|
}
|
2018-09-10 03:06:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_loadSettings() async {
|
2020-05-03 02:02:18 +03:00
|
|
|
_includeDeviceName = widget.quickStart || ConnectionManager().webhookId == null;
|
2018-09-10 03:06:35 +03:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2020-05-03 02:02:18 +03:00
|
|
|
String domain = prefs.getString('hassio-domain')?? '';
|
|
|
|
String port = prefs.getString('hassio-port') ?? '';
|
|
|
|
String urlProtocol = prefs.getString('hassio-res-protocol') ?? 'https';
|
|
|
|
_homeAssistantUrl = '$urlProtocol://$domain:$port';
|
|
|
|
_deviceName = prefs.getString('app-integration-device-name') ?? MobileAppIntegrationManager.getDefaultDeviceName();
|
2018-09-10 03:06:35 +03:00
|
|
|
setState(() {
|
2020-05-03 02:02:18 +03:00
|
|
|
_loaded = true;
|
2018-10-06 20:03:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-10 03:06:35 +03:00
|
|
|
_saveSettings() async {
|
2020-05-03 02:02:18 +03:00
|
|
|
String socketProtocol;
|
|
|
|
String domain;
|
|
|
|
String port;
|
|
|
|
if (_homeAssistantUrl.startsWith("http") && _homeAssistantUrl.indexOf("//") > 0) {
|
|
|
|
_homeAssistantUrl.startsWith("https") ? socketProtocol = "wss" : socketProtocol = "ws";
|
|
|
|
domain = _homeAssistantUrl.split("//")[1];
|
|
|
|
} else {
|
|
|
|
domain = _homeAssistantUrl;
|
2020-05-03 14:48:09 +03:00
|
|
|
socketProtocol = "ws";
|
2018-09-23 22:24:48 +03:00
|
|
|
}
|
2020-05-03 02:02:18 +03:00
|
|
|
domain = domain.split("/")[0];
|
|
|
|
if (domain.contains(":")) {
|
|
|
|
List<String> domainAndPort = domain.split(":");
|
|
|
|
domain = domainAndPort[0];
|
|
|
|
port = domainAndPort[1];
|
2019-11-28 20:57:41 +02:00
|
|
|
}
|
2018-09-10 03:06:35 +03:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2020-05-03 02:02:18 +03:00
|
|
|
await prefs.setString("hassio-domain", domain);
|
|
|
|
if (port == null || port.isEmpty) {
|
|
|
|
port = socketProtocol == "wss" ? "443" : "80";
|
2019-09-05 00:51:29 +03:00
|
|
|
} else {
|
2020-05-03 02:02:18 +03:00
|
|
|
port = port.trim();
|
|
|
|
}
|
|
|
|
await prefs.setString("hassio-port", port);
|
|
|
|
await prefs.setString("hassio-protocol", socketProtocol);
|
|
|
|
await prefs.setString("hassio-res-protocol", socketProtocol == "wss" ? "https" : "http");
|
|
|
|
if (_includeDeviceName) {
|
|
|
|
await prefs.setString('app-integration-device-name', _deviceName);
|
2019-09-05 00:48:20 +03:00
|
|
|
}
|
2018-09-10 03:06:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-05-03 02:02:18 +03:00
|
|
|
if (!_loaded) {
|
|
|
|
return PageLoadingIndicator();
|
|
|
|
}
|
|
|
|
List<Widget> formChildren = <Widget>[
|
|
|
|
Text(
|
|
|
|
"Home Assistant url:",
|
|
|
|
style: Theme.of(context).textTheme.headline,
|
|
|
|
),
|
|
|
|
TextFormField(
|
|
|
|
initialValue: _homeAssistantUrl,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
hintText: "Please enter url",
|
|
|
|
contentPadding: EdgeInsets.all(0),
|
|
|
|
hintStyle: Theme.of(context).textTheme.subhead.copyWith(
|
|
|
|
color: Theme.of(context).textTheme.overline.color
|
|
|
|
)
|
2020-04-11 19:09:35 +03:00
|
|
|
),
|
2020-05-03 02:02:18 +03:00
|
|
|
onSaved: (newValue) {
|
2020-05-03 16:47:11 +03:00
|
|
|
_homeAssistantUrl = newValue.trim();
|
2020-05-03 02:02:18 +03:00
|
|
|
},
|
|
|
|
validator: (value) {
|
2020-05-03 16:47:11 +03:00
|
|
|
if (value.trim().isEmpty) {
|
2020-05-03 02:02:18 +03:00
|
|
|
return 'Url is required';
|
2020-04-11 19:09:35 +03:00
|
|
|
}
|
2020-05-03 02:02:18 +03:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
height: 10,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
"For example:",
|
|
|
|
style: Theme.of(context).textTheme.body1,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
"192.186.2.14:8123",
|
|
|
|
style: Theme.of(context).textTheme.subhead,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
"http://myhome.duckdns.org:8123",
|
|
|
|
style: Theme.of(context).textTheme.subhead,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
"https://efkmfrwk3r4fsfwrfrg5.ui.nabu.casa/",
|
|
|
|
style: Theme.of(context).textTheme.subhead,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (_includeDeviceName) {
|
|
|
|
formChildren.addAll(<Widget>[
|
|
|
|
Container(
|
|
|
|
height: 30,
|
2020-04-11 19:09:35 +03:00
|
|
|
),
|
|
|
|
Text(
|
2020-05-03 02:02:18 +03:00
|
|
|
"Device name:",
|
|
|
|
style: Theme.of(context).textTheme.headline,
|
2020-04-11 19:09:35 +03:00
|
|
|
),
|
2020-05-03 02:02:18 +03:00
|
|
|
TextFormField(
|
|
|
|
initialValue: _deviceName,
|
|
|
|
onSaved: (newValue) {
|
2020-05-03 16:47:11 +03:00
|
|
|
_deviceName = newValue.trim();
|
2020-05-03 02:02:18 +03:00
|
|
|
},
|
|
|
|
decoration: InputDecoration(
|
|
|
|
hintText: 'Please enter device name',
|
|
|
|
contentPadding: EdgeInsets.all(0),
|
|
|
|
hintStyle: Theme.of(context).textTheme.subhead.copyWith(
|
|
|
|
color: Theme.of(context).textTheme.overline.color
|
|
|
|
)
|
2018-09-10 03:06:35 +03:00
|
|
|
),
|
2020-05-03 02:02:18 +03:00
|
|
|
validator: (value) {
|
2020-05-03 16:47:11 +03:00
|
|
|
if (value.trim().isEmpty) {
|
2020-05-03 02:02:18 +03:00
|
|
|
return 'Device name is required';
|
2018-11-06 14:01:00 +02:00
|
|
|
}
|
2020-05-03 02:02:18 +03:00
|
|
|
return null;
|
|
|
|
},
|
2020-04-11 19:09:35 +03:00
|
|
|
),
|
2020-05-03 02:02:18 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
formChildren.addAll(<Widget>[
|
|
|
|
Container(
|
|
|
|
height: 30,
|
|
|
|
),
|
|
|
|
ButtonTheme(
|
|
|
|
height: 60,
|
|
|
|
child: RaisedButton(
|
|
|
|
child: Text(widget.quickStart ? 'Engage' : 'Apply', style: Theme.of(context).textTheme.button.copyWith(fontSize: 20)),
|
|
|
|
color: Theme.of(context).primaryColor,
|
2020-04-11 19:09:35 +03:00
|
|
|
onPressed: () {
|
2020-05-03 02:02:18 +03:00
|
|
|
if (_formKey.currentState.validate()) {
|
|
|
|
_formKey.currentState.save();
|
2020-04-11 19:09:35 +03:00
|
|
|
_saveSettings().then((r) {
|
2020-05-03 02:02:18 +03:00
|
|
|
if (widget.quickStart) {
|
|
|
|
Navigator.pushReplacementNamed(context, '/');
|
|
|
|
} else {
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
2020-04-11 19:09:35 +03:00
|
|
|
eventBus.fire(SettingsChangedEvent(true));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-05-03 02:02:18 +03:00
|
|
|
)
|
|
|
|
]);
|
|
|
|
|
|
|
|
return Form(
|
|
|
|
key: _formKey,
|
|
|
|
child: ListView(
|
|
|
|
scrollDirection: Axis.vertical,
|
|
|
|
padding: const EdgeInsets.all(20.0),
|
|
|
|
children: formChildren,
|
|
|
|
),
|
2018-09-10 03:06:35 +03:00
|
|
|
);
|
|
|
|
}
|
2018-10-06 20:03:20 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
}
|
2018-09-24 10:27:08 +03:00
|
|
|
}
|