From 44165993b4e789180be9b1c33ceb3cb1b1ecddb8 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Thu, 24 Oct 2019 19:18:27 +0000 Subject: [PATCH] Integration setting improvement --- lib/managers/location_manager.class.dart | 34 +++++++++++++----------- lib/pages/integration_settings.page.dart | 30 ++++++++++++--------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/lib/managers/location_manager.class.dart b/lib/managers/location_manager.class.dart index ff20f6e..d86d6bd 100644 --- a/lib/managers/location_manager.class.dart +++ b/lib/managers/location_manager.class.dart @@ -143,26 +143,30 @@ void updateDeviceLocationIsolate() { //print("[Background $backgroundTask] Getting device location..."); Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium).then((location) { //print("[Background $backgroundTask] Got location: ${location.latitude} ${location.longitude}"); - data["data"]["gps"] = [location.latitude, location.longitude]; - data["data"]["gps_accuracy"] = location.accuracy; - //print("[Background $backgroundTask] Sending data home..."); - http.post( - url, - headers: headers, - body: json.encode(data) - ); - }).catchError((e) { - //print("[Background $backgroundTask] Error getting current location: ${e.toString()}. Trying last known..."); - Geolocator().getLastKnownPosition(desiredAccuracy: LocationAccuracy.medium).then((location){ - //print("[Background $backgroundTask] Got last known location: ${location.latitude} ${location.longitude}"); + if (location != null) { data["data"]["gps"] = [location.latitude, location.longitude]; data["data"]["gps_accuracy"] = location.accuracy; //print("[Background $backgroundTask] Sending data home..."); http.post( - url, - headers: headers, - body: json.encode(data) + url, + headers: headers, + body: json.encode(data) ); + } + }).catchError((e) { + //print("[Background $backgroundTask] Error getting current location: ${e.toString()}. Trying last known..."); + Geolocator().getLastKnownPosition(desiredAccuracy: LocationAccuracy.medium).then((location){ + //print("[Background $backgroundTask] Got last known location: ${location.latitude} ${location.longitude}"); + if (location != null) { + data["data"]["gps"] = [location.latitude, location.longitude]; + data["data"]["gps_accuracy"] = location.accuracy; + //print("[Background $backgroundTask] Sending data home..."); + http.post( + url, + headers: headers, + body: json.encode(data) + ); + } }); }); }); diff --git a/lib/pages/integration_settings.page.dart b/lib/pages/integration_settings.page.dart index 6d231ee..850e601 100644 --- a/lib/pages/integration_settings.page.dart +++ b/lib/pages/integration_settings.page.dart @@ -89,6 +89,16 @@ class _IntegrationSettingsPageState extends State { )); } + _switchLocationTrackingState(bool state) async { + if (state) { + await LocationManager().updateDeviceLocation(); + } + await LocationManager().setSettings(_locationTrackingEnabled, _locationInterval); + setState(() { + _wait = false; + }); + } + @override Widget build(BuildContext context) { return new Scaffold( @@ -114,11 +124,7 @@ class _IntegrationSettingsPageState extends State { _locationTrackingEnabled = value; _wait = true; }); - LocationManager().setSettings(_locationTrackingEnabled, _locationInterval).then((_){ - setState(() { - _wait = false; - }); - }); + _switchLocationTrackingState(value); }, ), ], @@ -132,19 +138,19 @@ class _IntegrationSettingsPageState extends State { //Expanded(child: Container(),), FlatButton( padding: EdgeInsets.all(0.0), - child: Text("+", style: TextStyle(fontSize: Sizes.largeFontSize)), - onPressed: () => incLocationInterval(), + child: Text("-", style: TextStyle(fontSize: Sizes.largeFontSize)), + onPressed: () => decLocationInterval(), ), Text("$_locationInterval", style: TextStyle(fontSize: Sizes.largeFontSize)), FlatButton( padding: EdgeInsets.all(0.0), - child: Text("-", style: TextStyle(fontSize: Sizes.largeFontSize)), - onPressed: () => decLocationInterval(), + child: Text("+", style: TextStyle(fontSize: Sizes.largeFontSize)), + onPressed: () => incLocationInterval(), ), ], ), Divider(), - Text("Registration", style: TextStyle(fontSize: Sizes.largeFontSize-2)), + Text("Integration status", style: TextStyle(fontSize: Sizes.largeFontSize-2)), Container(height: Sizes.rowPadding,), Text("${HomeAssistant().userName}'s ${DeviceInfoManager().model}, ${DeviceInfoManager().osName} ${DeviceInfoManager().osVersion}"), Container(height: 6.0,), @@ -156,13 +162,13 @@ class _IntegrationSettingsPageState extends State { RaisedButton( color: Colors.blue, onPressed: () => updateRegistration(), - child: Text("Check registration", style: TextStyle(color: Colors.white)) + child: Text("Check integration", style: TextStyle(color: Colors.white)) ), Container(width: 10.0,), RaisedButton( color: Colors.redAccent, onPressed: () => resetRegistration(), - child: Text("Reset registration", style: TextStyle(color: Colors.white)) + child: Text("Reset integration", style: TextStyle(color: Colors.white)) ) ], ),