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() {
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()

View File

@ -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) {

View File

@ -11,7 +11,6 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
int _locationInterval = LocationManager().defaultUpdateIntervalMinutes;
bool _locationTrackingEnabled = false;
bool _needToRestartLocationTracking = false;
@override
void initState() {
@ -31,24 +30,18 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
}
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<ConfigPanelWidget> {
SharedPreferences.getInstance().then((prefs) => prefs.setBool("location-enabled", value));
setState(() {
_locationTrackingEnabled = value;
_needToRestartLocationTracking = true;
});
},
),
@ -191,13 +183,7 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
@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();
}
}