Location manager optimizations

This commit is contained in:
estevez-dev 2019-09-01 23:12:43 +03:00
parent 27e6198d83
commit 389d28a1e1
3 changed files with 51 additions and 46 deletions

View File

@ -52,42 +52,66 @@ class LocationManager {
} }
LocationManager._internal() { LocationManager._internal() {
startLocationService(); init();
} }
final int defaultUpdateIntervalMinutes = 15; final int defaultUpdateIntervalMinutes = 15;
final int alarmId = 34901199; final int alarmId = 34901199;
Duration _updateInterval;
bool _isEnabled;
void startLocationService() async { void init() async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.reload(); await prefs.reload();
bool enabled = prefs.getBool("location-enabled") ?? false; _updateInterval = Duration(minutes: prefs.getInt("location-interval") ??
if (enabled) {
Duration locationUpdateInterval = Duration(
minutes: prefs.getInt("location-interval") ??
defaultUpdateIntervalMinutes); defaultUpdateIntervalMinutes);
Logger.d("Scheduling location update for every ${locationUpdateInterval _isEnabled = prefs.getBool("location-enabled") ?? false;
if (_isEnabled) {
_startLocationService();
}
}
void setSettings(bool enabled, int interval) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
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..."); .inMinutes} minutes...");
await AndroidAlarmManager.periodic( await AndroidAlarmManager.periodic(
locationUpdateInterval, _updateInterval,
alarmId, alarmId,
LocationManager.updateDeviceLocationIsolate, LocationManager.updateDeviceLocationIsolate,
wakeup: true, wakeup: true,
rescheduleOnReboot: true rescheduleOnReboot: true
); );
} else { }
Logger.d("Location tracking is disabled");
void _stopLocationService() async {
Logger.d("Canceling previous schedule if any..."); Logger.d("Canceling previous schedule if any...");
await AndroidAlarmManager.cancel(alarmId); await AndroidAlarmManager.cancel(alarmId);
} }
}
void updateDeviceLocation() async { void updateDeviceLocation() async {
print("[Location] started"); if (_isEnabled) {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.reload();
bool enabled = prefs.getBool("location-enabled") ?? false;
if (enabled) {
if (ConnectionManager().webhookId != null && if (ConnectionManager().webhookId != null &&
ConnectionManager().webhookId.isNotEmpty) { ConnectionManager().webhookId.isNotEmpty) {
String url = "${ConnectionManager() String url = "${ConnectionManager()

View File

@ -35,12 +35,7 @@ class StartupUserMessagesManager {
positiveText: "Enable now", positiveText: "Enable now",
negativeText: "Cancel", negativeText: "Cancel",
onPositive: () { onPositive: () {
SharedPreferences.getInstance().then((prefs) { LocationManager().setSettings(true, 15);
prefs.setBool("location-enabled", true);
prefs.setBool(_locationTrackingMessageKey, true);
LocationManager().startLocationService();
LocationManager().updateDeviceLocation();
});
}, },
onNegative: () { onNegative: () {
SharedPreferences.getInstance().then((prefs) { SharedPreferences.getInstance().then((prefs) {

View File

@ -11,7 +11,6 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
int _locationInterval = LocationManager().defaultUpdateIntervalMinutes; int _locationInterval = LocationManager().defaultUpdateIntervalMinutes;
bool _locationTrackingEnabled = false; bool _locationTrackingEnabled = false;
bool _needToRestartLocationTracking = false;
@override @override
void initState() { void initState() {
@ -31,24 +30,18 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
} }
void incLocationInterval() { void incLocationInterval() {
_needToRestartLocationTracking = true;
if (_locationInterval < 720) { if (_locationInterval < 720) {
setState(() { setState(() {
_locationInterval = _locationInterval + 1; _locationInterval = _locationInterval + 1;
}); });
SharedPreferences.getInstance().then((prefs) => prefs.setInt("location-interval", _locationInterval));
} }
} }
void decLocationInterval() { void decLocationInterval() {
_needToRestartLocationTracking = true;
if (_locationInterval > 1) { if (_locationInterval > 1) {
setState(() { setState(() {
_locationInterval = _locationInterval - 1; _locationInterval = _locationInterval - 1;
}); });
SharedPreferences.getInstance().then((prefs) {
prefs.setInt("location-interval", _locationInterval);
});
} }
} }
@ -142,7 +135,6 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
SharedPreferences.getInstance().then((prefs) => prefs.setBool("location-enabled", value)); SharedPreferences.getInstance().then((prefs) => prefs.setBool("location-enabled", value));
setState(() { setState(() {
_locationTrackingEnabled = value; _locationTrackingEnabled = value;
_needToRestartLocationTracking = true;
}); });
}, },
), ),
@ -191,13 +183,7 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
@override @override
void dispose() { void dispose() {
if (_needToRestartLocationTracking) { LocationManager().setSettings(_locationTrackingEnabled, _locationInterval);
Logger.d("Location tracking settings was changed. Restarting location service...");
LocationManager().startLocationService();
if (_locationTrackingEnabled) {
LocationManager().updateDeviceLocation();
}
}
super.dispose(); super.dispose();
} }
} }