diff --git a/lib/managers/location_manager.class.dart b/lib/managers/location_manager.class.dart index 25458fe..866fa20 100644 --- a/lib/managers/location_manager.class.dart +++ b/lib/managers/location_manager.class.dart @@ -52,42 +52,66 @@ class LocationManager { } LocationManager._internal() { - startLocationService(); + init(); } final int defaultUpdateIntervalMinutes = 15; final int alarmId = 34901199; + Duration _updateInterval; + bool _isEnabled; - void startLocationService() async { + void init() async { SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.reload(); - bool enabled = prefs.getBool("location-enabled") ?? false; - if (enabled) { - Duration locationUpdateInterval = Duration( - minutes: prefs.getInt("location-interval") ?? - defaultUpdateIntervalMinutes); - Logger.d("Scheduling location update for every ${locationUpdateInterval - .inMinutes} minutes..."); - await AndroidAlarmManager.periodic( - locationUpdateInterval, - alarmId, - LocationManager.updateDeviceLocationIsolate, - wakeup: true, - rescheduleOnReboot: true - ); - } else { - Logger.d("Location tracking is disabled"); - Logger.d("Canceling previous schedule if any..."); - await AndroidAlarmManager.cancel(alarmId); + _updateInterval = Duration(minutes: prefs.getInt("location-interval") ?? + defaultUpdateIntervalMinutes); + _isEnabled = prefs.getBool("location-enabled") ?? false; + if (_isEnabled) { + _startLocationService(); } } - void updateDeviceLocation() async { - print("[Location] started"); + void setSettings(bool enabled, int interval) async { SharedPreferences prefs = await SharedPreferences.getInstance(); - await prefs.reload(); - bool enabled = prefs.getBool("location-enabled") ?? false; - if (enabled) { + if (interval != _updateInterval.inMinutes) { + prefs.setInt("location-interval", interval); + _updateInterval = Duration(minutes: interval); + } + if (enabled && !_isEnabled) { + Logger.d("Enabling location service"); + SharedPreferences prefs = await SharedPreferences.getInstance(); + prefs.setBool("location-enabled", enabled); + _isEnabled = true; + _startLocationService(); + updateDeviceLocation(); + } else if (!enabled && _isEnabled) { + Logger.d("Disabling location service"); + SharedPreferences prefs = await SharedPreferences.getInstance(); + prefs.setBool("location-enabled", enabled); + _isEnabled = false; + _stopLocationService(); + } + } + + void _startLocationService() async { + Logger.d("Scheduling location update for every ${_updateInterval + .inMinutes} minutes..."); + await AndroidAlarmManager.periodic( + _updateInterval, + alarmId, + LocationManager.updateDeviceLocationIsolate, + wakeup: true, + rescheduleOnReboot: true + ); + } + + void _stopLocationService() async { + Logger.d("Canceling previous schedule if any..."); + await AndroidAlarmManager.cancel(alarmId); + } + + void updateDeviceLocation() async { + if (_isEnabled) { if (ConnectionManager().webhookId != null && ConnectionManager().webhookId.isNotEmpty) { String url = "${ConnectionManager() diff --git a/lib/managers/startup_user_messages_manager.class.dart b/lib/managers/startup_user_messages_manager.class.dart index 6e6c9c6..3b43e94 100644 --- a/lib/managers/startup_user_messages_manager.class.dart +++ b/lib/managers/startup_user_messages_manager.class.dart @@ -35,12 +35,7 @@ class StartupUserMessagesManager { positiveText: "Enable now", negativeText: "Cancel", onPositive: () { - SharedPreferences.getInstance().then((prefs) { - prefs.setBool("location-enabled", true); - prefs.setBool(_locationTrackingMessageKey, true); - LocationManager().startLocationService(); - LocationManager().updateDeviceLocation(); - }); + LocationManager().setSettings(true, 15); }, onNegative: () { SharedPreferences.getInstance().then((prefs) { diff --git a/lib/panels/config_panel_widget.dart b/lib/panels/config_panel_widget.dart index 0e9dc89..9e58dbb 100644 --- a/lib/panels/config_panel_widget.dart +++ b/lib/panels/config_panel_widget.dart @@ -11,7 +11,6 @@ class _ConfigPanelWidgetState extends State { int _locationInterval = LocationManager().defaultUpdateIntervalMinutes; bool _locationTrackingEnabled = false; - bool _needToRestartLocationTracking = false; @override void initState() { @@ -31,24 +30,18 @@ class _ConfigPanelWidgetState extends State { } void incLocationInterval() { - _needToRestartLocationTracking = true; if (_locationInterval < 720) { setState(() { _locationInterval = _locationInterval + 1; }); - SharedPreferences.getInstance().then((prefs) => prefs.setInt("location-interval", _locationInterval)); } } void decLocationInterval() { - _needToRestartLocationTracking = true; if (_locationInterval > 1) { setState(() { _locationInterval = _locationInterval - 1; }); - SharedPreferences.getInstance().then((prefs) { - prefs.setInt("location-interval", _locationInterval); - }); } } @@ -142,7 +135,6 @@ class _ConfigPanelWidgetState extends State { SharedPreferences.getInstance().then((prefs) => prefs.setBool("location-enabled", value)); setState(() { _locationTrackingEnabled = value; - _needToRestartLocationTracking = true; }); }, ), @@ -191,13 +183,7 @@ class _ConfigPanelWidgetState extends State { @override void dispose() { - if (_needToRestartLocationTracking) { - Logger.d("Location tracking settings was changed. Restarting location service..."); - LocationManager().startLocationService(); - if (_locationTrackingEnabled) { - LocationManager().updateDeviceLocation(); - } - } + LocationManager().setSettings(_locationTrackingEnabled, _locationInterval); super.dispose(); } }