diff --git a/lib/main.dart b/lib/main.dart index f4dc82a..b8f8a97 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -149,8 +149,8 @@ EventBus eventBus = new EventBus(); final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin(); const String appName = "HA Client"; -const appVersionNumber = "0.8.3"; -const appVersionAdd = ""; +const appVersionNumber = "0.8.4"; +const appVersionAdd = "alpha1"; const appVersion = "$appVersionNumber$appVersionAdd"; Future _reportError(dynamic error, dynamic stackTrace) async { diff --git a/lib/managers/connection_manager.class.dart b/lib/managers/connection_manager.class.dart index 60cb40b..a58a9e7 100644 --- a/lib/managers/connection_manager.class.dart +++ b/lib/managers/connection_manager.class.dart @@ -20,6 +20,7 @@ class ConnectionManager { String oauthUrl; String webhookId; bool settingsLoaded = false; + int appIntegrationVersion; bool get isAuthenticated => _token != null; StreamSubscription _socketSubscription; Duration connectTimeout = Duration(seconds: 15); @@ -43,6 +44,7 @@ class ConnectionManager { _domain = prefs.getString('hassio-domain'); _port = prefs.getString('hassio-port'); webhookId = prefs.getString('app-webhook-id'); + appIntegrationVersion = prefs.getInt('app-integration-version') ?? 0; displayHostname = "$_domain:$_port"; _webSocketAPIEndpoint = "${prefs.getString('hassio-protocol')}://$_domain:$_port/api/websocket"; diff --git a/lib/managers/mobile_app_integration_manager.class.dart b/lib/managers/mobile_app_integration_manager.class.dart index 84d96a7..ff7266f 100644 --- a/lib/managers/mobile_app_integration_manager.class.dart +++ b/lib/managers/mobile_app_integration_manager.class.dart @@ -2,9 +2,11 @@ part of '../main.dart'; class MobileAppIntegrationManager { + static const INTEGRATION_VERSION = 3; + static final _appRegistrationData = { - "app_version": "$appVersion", "device_name": "", + "app_version": "$appVersion", "manufacturer": DeviceInfoManager().manufacturer, "model": DeviceInfoManager().model, "os_version": DeviceInfoManager().osVersion, @@ -22,6 +24,7 @@ class MobileAppIntegrationManager { Logger.d("Mobile app was not registered yet or need to be reseted. Registering..."); var registrationData = Map.from(_appRegistrationData); registrationData.addAll({ + "device_id": "${DeviceInfoManager().unicDeviceId}", "app_id": "ha_client", "app_name": "$appName", "os_name": DeviceInfoManager().osName, @@ -34,9 +37,11 @@ class MobileAppIntegrationManager { ).then((response) { Logger.d("Processing registration responce..."); var responseObject = json.decode(response); + ConnectionManager().webhookId = responseObject["webhook_id"]; + ConnectionManager().appIntegrationVersion = INTEGRATION_VERSION; SharedPreferences.getInstance().then((prefs) { prefs.setString("app-webhook-id", responseObject["webhook_id"]); - ConnectionManager().webhookId = responseObject["webhook_id"]; + prefs.setInt('app-integration-version', INTEGRATION_VERSION); completer.complete(); eventBus.fire(ShowPopupDialogEvent( @@ -65,18 +70,29 @@ class MobileAppIntegrationManager { includeAuthHeader: false, data: json.encode(updateData) ).then((response) { - if (response == null || response.isEmpty) { - Logger.d("No registration data in response. MobileApp integration was removed"); + var registrationData; + try { + registrationData = json.decode(response); + } catch (e) { + registrationData = null; + } + if (registrationData == null || registrationData.isEmpty) { + Logger.d("No registration data in response. MobileApp integration was removed or broken"); _askToRegisterApp(); } else { - Logger.d("App registration works fine"); - if (showOkDialog) { - eventBus.fire(ShowPopupDialogEvent( - title: "All good", - body: "HA Client integration with your Home Assistant server works fine", - positiveText: "Nice!", - negativeText: "Ok" - )); + if (INTEGRATION_VERSION > ConnectionManager().appIntegrationVersion) { + Logger.d('App registration needs to be updated'); + _askToRemoveAndRegisterApp(); + } else { + Logger.d('App registration works fine'); + if (showOkDialog) { + eventBus.fire(ShowPopupDialogEvent( + title: "All good", + body: "HA Client integration with your Home Assistant server works fine", + positiveText: "Nice!", + negativeText: "Ok" + )); + } } } completer.complete(); @@ -105,10 +121,22 @@ class MobileAppIntegrationManager { } } + static void _askToRemoveAndRegisterApp() { + eventBus.fire(ShowPopupDialogEvent( + title: "Mobile app integration needs to be updated", + body: "You need to update HA Client integration to continue using notifications and location tracking. Please remove 'Mobile App' integration for this device from your Home Assistant and restart Home Assistant. Then go back to HA Client to create app integration again.", + positiveText: "Ok", + negativeText: "Report an issue", + onNegative: () { + Launcher.launchURL("https://github.com/estevez-dev/ha_client/issues/new"); + }, + )); + } + static void _askToRegisterApp() { eventBus.fire(ShowPopupDialogEvent( - title: "App integration was removed", - body: "Looks like app integration was removed from your Home Assistant. HA Client needs to be registered on your Home Assistant server to make it possible to use notifications and other useful stuff.", + title: "App integration is broken", + body: "Looks like app integration was removed from your Home Assistant or it needs to be updated. HA Client needs to be registered on your Home Assistant server to make it possible to use notifications and location tracking. Please remove 'Mobile App' integration for this device from your Home Assistant before registering and restart Home Assistant. Then go back here.", positiveText: "Register now", negativeText: "Cancel", onPositive: () {