Resolves #114 Error going back from settings

This commit is contained in:
estevez 2018-10-03 14:36:23 +03:00
parent ed3e4ba272
commit 285447a5b7
3 changed files with 118 additions and 56 deletions

View File

@ -37,15 +37,18 @@ class HomeAssistant {
EntityCollection get entities => _entities; EntityCollection get entities => _entities;
HomeAssistant(String url, String password, String authType) { HomeAssistant() {
_hassioAPIEndpoint = url;
_hassioPassword = password;
_hassioAuthType = authType;
_entities = EntityCollection(); _entities = EntityCollection();
_uiBuilder = UIBuilder(); _uiBuilder = UIBuilder();
_messageQueue = SendMessageQueue(messageExpirationTime); _messageQueue = SendMessageQueue(messageExpirationTime);
} }
void updateConnectionSettings(String url, String password, String authType) {
_hassioAPIEndpoint = url;
_hassioPassword = password;
_hassioAuthType = authType;
}
Future fetch() { Future fetch() {
if ((_fetchCompleter != null) && (!_fetchCompleter.isCompleted)) { if ((_fetchCompleter != null) && (!_fetchCompleter.isCompleted)) {
TheLogger.log("Warning","Previous fetch is not complited"); TheLogger.log("Warning","Previous fetch is not complited");

View File

@ -88,6 +88,9 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
HomeAssistant _homeAssistant; HomeAssistant _homeAssistant;
EntityCollection _entities; EntityCollection _entities;
//Map _instanceConfig; //Map _instanceConfig;
String _apiEndpoint;
String _apiPassword;
String _authType;
int _uiViewsCount = 0; int _uiViewsCount = 0;
String _instanceHost; String _instanceHost;
int _errorCodeToBeShown = 0; int _errorCodeToBeShown = 0;
@ -107,14 +110,35 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
void initState() { void initState() {
super.initState(); super.initState();
WidgetsBinding.instance.addObserver(this); WidgetsBinding.instance.addObserver(this);
_homeAssistant = HomeAssistant();
_settingsSubscription = eventBus.on<SettingsChangedEvent>().listen((event) { _settingsSubscription = eventBus.on<SettingsChangedEvent>().listen((event) {
TheLogger.log("Debug","Settings change event: reconnect=${event.reconnect}"); TheLogger.log("Debug","Settings change event: reconnect=${event.reconnect}");
if (event.reconnect) {
_homeAssistant.closeConnection();
_initConnection().then((b){
setState(() { setState(() {
_errorCodeToBeShown = 0; _homeAssistant.updateConnectionSettings(_apiEndpoint, _apiPassword, _authType);
_errorCodeToBeShown = 10;
_lastErrorMessage = "Connection settings was changed.";
});
}, onError: (_) {
setState(() {
_lastErrorMessage = _;
_errorCodeToBeShown = 5;
});
});
}
});
_initConnection().then((_){
_createConnection();
}, onError: (_) {
setState(() {
_lastErrorMessage = _;
_errorCodeToBeShown = 5;
}); });
_initConnection();
}); });
_initConnection();
} }
@override @override
@ -130,25 +154,19 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
String domain = prefs.getString('hassio-domain'); String domain = prefs.getString('hassio-domain');
String port = prefs.getString('hassio-port'); String port = prefs.getString('hassio-port');
_instanceHost = "$domain:$port"; _instanceHost = "$domain:$port";
String apiEndpoint = "${prefs.getString('hassio-protocol')}://$domain:$port/api/websocket"; _apiEndpoint = "${prefs.getString('hassio-protocol')}://$domain:$port/api/websocket";
homeAssistantWebHost = "${prefs.getString('hassio-res-protocol')}://$domain:$port"; homeAssistantWebHost = "${prefs.getString('hassio-res-protocol')}://$domain:$port";
String apiPassword = prefs.getString('hassio-password'); _apiPassword = prefs.getString('hassio-password');
String authType = prefs.getString('hassio-auth-type'); _authType = prefs.getString('hassio-auth-type');
if ((domain == null) || (port == null) || (apiPassword == null) || if ((domain == null) || (port == null) || (_apiPassword == null) ||
(domain.length == 0) || (port.length == 0) || (apiPassword.length == 0)) { (domain.length == 0) || (port.length == 0) || (_apiPassword.length == 0)) {
setState(() { throw("Check connection settings");
_errorCodeToBeShown = 5;
});
} else {
if (_homeAssistant != null) _homeAssistant.closeConnection();
_createConnection(apiEndpoint, apiPassword, authType);
} }
} }
_createConnection(String apiEndpoint, String apiPassword, String authType) { _createConnection() {
_homeAssistant = HomeAssistant(apiEndpoint, apiPassword, authType);
_refreshData(); _refreshData();
if (_stateSubscription != null) _stateSubscription.cancel(); if (_stateSubscription == null) {
_stateSubscription = eventBus.on<StateChangedEvent>().listen((event) { _stateSubscription = eventBus.on<StateChangedEvent>().listen((event) {
setState(() { setState(() {
if (event.localChange) { if (event.localChange) {
@ -158,23 +176,30 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
} }
}); });
}); });
if (_serviceCallSubscription != null) _serviceCallSubscription.cancel(); }
_serviceCallSubscription = eventBus.on<ServiceCallEvent>().listen((event) { if (_serviceCallSubscription == null) {
_callService(event.domain, event.service, event.entityId, event.additionalParams); _serviceCallSubscription =
}); eventBus.on<ServiceCallEvent>().listen((event) {
_callService(event.domain, event.service, event.entityId,
if (_showEntityPageSubscription != null) _showEntityPageSubscription.cancel(); event.additionalParams);
_showEntityPageSubscription = eventBus.on<ShowEntityPageEvent>().listen((event) {
_showEntityPage(event.entity);
}); });
} }
if (_showEntityPageSubscription == null) {
_showEntityPageSubscription =
eventBus.on<ShowEntityPageEvent>().listen((event) {
_showEntityPage(event.entity);
});
}
}
_refreshData() async { _refreshData() async {
_homeAssistant.updateConnectionSettings(_apiEndpoint, _apiPassword, _authType);
setState(() { setState(() {
_isLoading = true; _isLoading = true;
}); });
_errorCodeToBeShown = 0; _errorCodeToBeShown = 0;
if (_homeAssistant != null) { _lastErrorMessage = "";
await _homeAssistant.fetch().then((result) { await _homeAssistant.fetch().then((result) {
setState(() { setState(() {
//_instanceConfig = _homeAssistant.instanceConfig; //_instanceConfig = _homeAssistant.instanceConfig;
@ -186,7 +211,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
_setErrorState(e); _setErrorState(e);
}); });
} }
}
_setErrorState(e) { _setErrorState(e) {
setState(() { setState(() {
@ -483,6 +507,25 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
HAUtils.launchURL("https://github.com/estevez-dev/ha_client_pub/issues/new"); HAUtils.launchURL("https://github.com/estevez-dev/ha_client_pub/issues/new");
}, },
), ),
Container(
height: 30.0,
decoration: new BoxDecoration(
border: new Border(
top: BorderSide(
width: 2.0,
color: Colors.black26,
)
),
)
),
new ListTile(
leading: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:coffee")),
title: Text("By me a coffee"),
onTap: () {
Navigator.of(context).pop();
HAUtils.launchURL("https://www.buymeacoffee.com/estevez");
},
),
new AboutListTile( new AboutListTile(
applicationName: appName, applicationName: appName,
applicationVersion: appVersion, applicationVersion: appVersion,
@ -499,6 +542,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
SnackBarAction action; SnackBarAction action;
switch (_errorCodeToBeShown) { switch (_errorCodeToBeShown) {
case 9: case 9:
case 11:
case 7:
case 1: { case 1: {
action = SnackBarAction( action = SnackBarAction(
label: "Retry", label: "Retry",
@ -533,9 +578,9 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
break; break;
} }
case 7: { case 10: {
action = SnackBarAction( action = SnackBarAction(
label: "Retry", label: "Refresh",
onPressed: () { onPressed: () {
_scaffoldKey?.currentState?.hideCurrentSnackBar(); _scaffoldKey?.currentState?.hideCurrentSnackBar();
_refreshData(); _refreshData();

View File

@ -15,6 +15,7 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
String _hassioPassword = ""; String _hassioPassword = "";
String _socketProtocol = "wss"; String _socketProtocol = "wss";
String _authType = "access_token"; String _authType = "access_token";
bool _connectionSettingsChanged = false;
@override @override
void initState() { void initState() {
@ -45,6 +46,7 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
prefs.setString("hassio-protocol", _socketProtocol); prefs.setString("hassio-protocol", _socketProtocol);
prefs.setString("hassio-res-protocol", _socketProtocol == "wss" ? "https" : "http"); prefs.setString("hassio-res-protocol", _socketProtocol == "wss" ? "https" : "http");
prefs.setString("hassio-auth-type", _authType); prefs.setString("hassio-auth-type", _authType);
_connectionSettingsChanged = true;
} }
@override @override
@ -52,12 +54,24 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
return new Scaffold( return new Scaffold(
appBar: new AppBar( appBar: new AppBar(
leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){ leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){
_saveSettings().then((r){
Navigator.pop(context); Navigator.pop(context);
});
eventBus.fire(SettingsChangedEvent(true));
}), }),
title: new Text(widget.title), title: new Text(widget.title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.check),
onPressed:(){
if (_connectionSettingsChanged) {
_saveSettings().then((r){
Navigator.pop(context);
eventBus.fire(SettingsChangedEvent(_connectionSettingsChanged));
});
} else {
Navigator.pop(context);
}
}
)
],
), ),
body: ListView( body: ListView(
padding: const EdgeInsets.all(20.0), padding: const EdgeInsets.all(20.0),