diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index 9f0a1b8..7aa7df0 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -37,15 +37,18 @@ class HomeAssistant { EntityCollection get entities => _entities; - HomeAssistant(String url, String password, String authType) { - _hassioAPIEndpoint = url; - _hassioPassword = password; - _hassioAuthType = authType; + HomeAssistant() { _entities = EntityCollection(); _uiBuilder = UIBuilder(); _messageQueue = SendMessageQueue(messageExpirationTime); } + void updateConnectionSettings(String url, String password, String authType) { + _hassioAPIEndpoint = url; + _hassioPassword = password; + _hassioAuthType = authType; + } + Future fetch() { if ((_fetchCompleter != null) && (!_fetchCompleter.isCompleted)) { TheLogger.log("Warning","Previous fetch is not complited"); diff --git a/lib/main.dart b/lib/main.dart index efee6cc..636eb12 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -88,6 +88,9 @@ class _MainPageState extends State with WidgetsBindingObserver { HomeAssistant _homeAssistant; EntityCollection _entities; //Map _instanceConfig; + String _apiEndpoint; + String _apiPassword; + String _authType; int _uiViewsCount = 0; String _instanceHost; int _errorCodeToBeShown = 0; @@ -107,14 +110,35 @@ class _MainPageState extends State with WidgetsBindingObserver { void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); + + _homeAssistant = HomeAssistant(); + _settingsSubscription = eventBus.on().listen((event) { TheLogger.log("Debug","Settings change event: reconnect=${event.reconnect}"); - setState(() { - _errorCodeToBeShown = 0; - }); - _initConnection(); + if (event.reconnect) { + _homeAssistant.closeConnection(); + _initConnection().then((b){ + setState(() { + _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(); } @override @@ -130,62 +154,62 @@ class _MainPageState extends State with WidgetsBindingObserver { String domain = prefs.getString('hassio-domain'); String port = prefs.getString('hassio-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"; - String apiPassword = prefs.getString('hassio-password'); - String authType = prefs.getString('hassio-auth-type'); - if ((domain == null) || (port == null) || (apiPassword == null) || - (domain.length == 0) || (port.length == 0) || (apiPassword.length == 0)) { - setState(() { - _errorCodeToBeShown = 5; - }); - } else { - if (_homeAssistant != null) _homeAssistant.closeConnection(); - _createConnection(apiEndpoint, apiPassword, authType); + _apiPassword = prefs.getString('hassio-password'); + _authType = prefs.getString('hassio-auth-type'); + if ((domain == null) || (port == null) || (_apiPassword == null) || + (domain.length == 0) || (port.length == 0) || (_apiPassword.length == 0)) { + throw("Check connection settings"); } } - _createConnection(String apiEndpoint, String apiPassword, String authType) { - _homeAssistant = HomeAssistant(apiEndpoint, apiPassword, authType); + _createConnection() { _refreshData(); - if (_stateSubscription != null) _stateSubscription.cancel(); - _stateSubscription = eventBus.on().listen((event) { - setState(() { - if (event.localChange) { - _entities - .get(event.entityId) - .state = event.newState; - } + if (_stateSubscription == null) { + _stateSubscription = eventBus.on().listen((event) { + setState(() { + if (event.localChange) { + _entities + .get(event.entityId) + .state = event.newState; + } + }); }); - }); - if (_serviceCallSubscription != null) _serviceCallSubscription.cancel(); - _serviceCallSubscription = eventBus.on().listen((event) { - _callService(event.domain, event.service, event.entityId, event.additionalParams); - }); + } + if (_serviceCallSubscription == null) { + _serviceCallSubscription = + eventBus.on().listen((event) { + _callService(event.domain, event.service, event.entityId, + event.additionalParams); + }); + } - if (_showEntityPageSubscription != null) _showEntityPageSubscription.cancel(); - _showEntityPageSubscription = eventBus.on().listen((event) { - _showEntityPage(event.entity); - }); + if (_showEntityPageSubscription == null) { + _showEntityPageSubscription = + eventBus.on().listen((event) { + _showEntityPage(event.entity); + }); + } } _refreshData() async { + _homeAssistant.updateConnectionSettings(_apiEndpoint, _apiPassword, _authType); setState(() { _isLoading = true; }); _errorCodeToBeShown = 0; - if (_homeAssistant != null) { - await _homeAssistant.fetch().then((result) { - setState(() { - //_instanceConfig = _homeAssistant.instanceConfig; - _entities = _homeAssistant.entities; - _uiViewsCount = _homeAssistant.viewsCount; - _isLoading = false; - }); - }).catchError((e) { - _setErrorState(e); + _lastErrorMessage = ""; + await _homeAssistant.fetch().then((result) { + setState(() { + //_instanceConfig = _homeAssistant.instanceConfig; + _entities = _homeAssistant.entities; + _uiViewsCount = _homeAssistant.viewsCount; + _isLoading = false; }); - } + }).catchError((e) { + _setErrorState(e); + }); } _setErrorState(e) { @@ -483,6 +507,25 @@ class _MainPageState extends State with WidgetsBindingObserver { 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( applicationName: appName, applicationVersion: appVersion, @@ -499,6 +542,8 @@ class _MainPageState extends State with WidgetsBindingObserver { SnackBarAction action; switch (_errorCodeToBeShown) { case 9: + case 11: + case 7: case 1: { action = SnackBarAction( label: "Retry", @@ -533,9 +578,9 @@ class _MainPageState extends State with WidgetsBindingObserver { break; } - case 7: { + case 10: { action = SnackBarAction( - label: "Retry", + label: "Refresh", onPressed: () { _scaffoldKey?.currentState?.hideCurrentSnackBar(); _refreshData(); diff --git a/lib/settings.page.dart b/lib/settings.page.dart index 6894f60..805be06 100644 --- a/lib/settings.page.dart +++ b/lib/settings.page.dart @@ -15,6 +15,7 @@ class _ConnectionSettingsPageState extends State { String _hassioPassword = ""; String _socketProtocol = "wss"; String _authType = "access_token"; + bool _connectionSettingsChanged = false; @override void initState() { @@ -45,6 +46,7 @@ class _ConnectionSettingsPageState extends State { prefs.setString("hassio-protocol", _socketProtocol); prefs.setString("hassio-res-protocol", _socketProtocol == "wss" ? "https" : "http"); prefs.setString("hassio-auth-type", _authType); + _connectionSettingsChanged = true; } @override @@ -52,12 +54,24 @@ class _ConnectionSettingsPageState extends State { return new Scaffold( appBar: new AppBar( leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){ - _saveSettings().then((r){ - Navigator.pop(context); - }); - eventBus.fire(SettingsChangedEvent(true)); + Navigator.pop(context); }), title: new Text(widget.title), + actions: [ + IconButton( + icon: Icon(Icons.check), + onPressed:(){ + if (_connectionSettingsChanged) { + _saveSettings().then((r){ + Navigator.pop(context); + eventBus.fire(SettingsChangedEvent(_connectionSettingsChanged)); + }); + } else { + Navigator.pop(context); + } + } + ) + ], ), body: ListView( padding: const EdgeInsets.all(20.0),