[#39] Protocol settings. Saving settings improvement

This commit is contained in:
estevez 2018-09-16 21:27:16 +03:00
parent c20fa12c3d
commit fa40a23b4b
3 changed files with 33 additions and 23 deletions

View File

@ -37,8 +37,8 @@ class HassioDataModel {
} else {
//Fetch timeout timer
_fetchingTimer = Timer(Duration(seconds: 10), () {
_fetchCompleter.completeError({"message": "Data fetching timeout."});
closeConnection();
_fetchCompleter.completeError({"message": "Data fetching timeout."});
});
_fetchCompleter = new Completer();
_reConnectSocket().then((r) {
@ -51,11 +51,11 @@ class HassioDataModel {
return _fetchCompleter.future;
}
void closeConnection() {
if (_hassioChannel != null) {
_hassioChannel.sink.close();
_hassioChannel = null;
closeConnection() {
if (_hassioChannel?.closeCode == null) {
_hassioChannel.sink?.close();
}
_hassioChannel = null;
}
Future _reConnectSocket() {
@ -64,7 +64,7 @@ class HassioDataModel {
debugPrint("Socket connecting...");
_hassioChannel = IOWebSocketChannel.connect(_hassioAPIEndpoint);
_hassioChannel.stream.handleError((e) {
debugPrint("Socket error: ${e.toString()}");
debugPrint("Unhandled socket error: ${e.toString()}");
});
_hassioChannel.stream.listen((message) =>
_handleMessage(_connectionCompleter, message));
@ -93,11 +93,8 @@ class HassioDataModel {
var data = json.decode(message);
debugPrint("[Received]Message type: ${data['type']}");
if (data["type"] == "auth_required") {
debugPrint(" sending auth!");
_sendMessageRaw('{"type": "auth","api_password": "$_hassioPassword"}');
} else if (data["type"] == "auth_ok") {
debugPrint(" auth done");
debugPrint("Connection done.");
_sendSubscribe();
connectionCompleter.complete();
} else if (data["type"] == "auth_invalid") {

View File

@ -76,7 +76,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
_init() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String _hassioAPIEndpoint = "wss://" +
String _hassioAPIEndpoint = prefs.getString('hassio-protocol')+"://" +
prefs.getString('hassio-domain') +
":" +
prefs.getString('hassio-port') +
@ -318,6 +318,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
child: Icon(
_createMDIfromCode(MaterialDesignIcons.getCustomIconByName("mdi:home-assistant")),
size: 40.0,
color: _dataModelErrorMessage == null ? Colors.blue : Colors.redAccent,
),
),
]

View File

@ -13,6 +13,7 @@ class _SettingsPageState extends State<SettingsPage> {
String _hassioDomain = "";
String _hassioPort = "";
String _hassioPassword = "";
String _socketProtocol = "wss";
@override
void initState() {
@ -27,28 +28,22 @@ class _SettingsPageState extends State<SettingsPage> {
_hassioDomain = prefs.getString("hassio-domain");
_hassioPort = prefs.getString("hassio-port");
_hassioPassword = prefs.getString("hassio-password");
_socketProtocol = prefs.getString("hassio-protocol");
});
}
_saveSettings() async {
debugPrint("Saving settings....");
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
prefs.setString("hassio-domain", _hassioDomain);
prefs.setString("hassio-port", _hassioPort);
prefs.setString("hassio-password", _hassioPassword);
_hassioPassword = prefs.getString('hassio-password');
});
prefs.setString("hassio-protocol", _socketProtocol);
debugPrint("Done saving settings....");
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return new Scaffold(
appBar: new AppBar(
leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){
@ -62,6 +57,20 @@ class _SettingsPageState extends State<SettingsPage> {
body: ListView(
padding: const EdgeInsets.all(20.0),
children: <Widget>[
new Row(
children: [
Text("HTTPS"),
Switch(
value: (_socketProtocol == "wss"),
onChanged: (value) {
setState(() {
_socketProtocol = value ? "wss" : "ws";
});
_saveSettings();
},
)
],
),
new TextField(
decoration: InputDecoration(
labelText: "Home Assistant domain or ip address"
@ -71,6 +80,7 @@ class _SettingsPageState extends State<SettingsPage> {
),
onChanged: (value) {
_hassioDomain = value;
_saveSettings();
},
),
new TextField(
@ -82,6 +92,7 @@ class _SettingsPageState extends State<SettingsPage> {
),
onChanged: (value) {
_hassioPort = value;
_saveSettings();
},
),
new TextField(
@ -93,6 +104,7 @@ class _SettingsPageState extends State<SettingsPage> {
),
onChanged: (value) {
_hassioPassword = value;
_saveSettings();
},
)
],