Skip to content

Commit

Permalink
Get user location solely from location controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbendebiene committed Jul 26, 2024
1 parent 3c15bdc commit 04cb158
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions lib/view_models/home_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class HomeViewModel extends ViewModel with MakeTickerProvider, PromptMediator, N
_stopAreaSubscription = _appWorker.subscribeStopAreas().listen(_markStopArea);

// request location permissions and service on startup
_acquireUserLocation().then((pos) {
if (pos != null) locationIndicatorController.activate();
_requestLocationPermission().then((granted) {
if (granted) locationIndicatorController.activate();
});
}

Expand Down Expand Up @@ -135,49 +135,52 @@ class HomeViewModel extends ViewModel with MakeTickerProvider, PromptMediator, N
/// Activate or deactivate location following depending on its current state.
void toggleLocationFollowing() async {
if (cameraIsFollowingLocation == false) {
final location = await _acquireUserLocation();
if (location != null) {
if (!(await _requestLocationPermission())) {
return runInAction(() {
_cameraIsFollowingLocation.value = false;
});
}
else if (cameraIsFollowingLocation == false) {
if (!locationIndicatorController.isActive) {
locationIndicatorController.activate();
}
// if no location permission was granted the initial location is unset, so we need to wait for it
await _locationIsInitialized;
try {
await mapController.animateTo(
ticker: this,
location: LatLng(location.latitude, location.longitude),
location: locationIndicatorController.rawLocation,
id: 'KeepCameraTracking',
);
).orCancel;
}
on TickerCanceled {
return;
}
}
runInAction(() {
_cameraIsFollowingLocation.value = !cameraIsFollowingLocation;
});
}

/// This function will automatically request permissions if not already granted
/// as well as the activation of the device's location service if not already activated.
Future<Position?> _acquireUserLocation() async {
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
// permissions are denied don't continue
return null;
Future<void> get _locationIsInitialized async {
if (locationIndicatorController.rawLocation == null) {
final completer = Completer<void>();
void complete() {
if (locationIndicatorController.rawLocation != null) {
locationIndicatorController.removeRawListener(complete);
completer.complete();
}
}
}
else if (permission == LocationPermission.deniedForever) {
// permissions are denied forever don't continue
return null;
}
// if permissions are granted try to access the position of the device
// on android the user will be automatically asked if he wants to enable the location service if it is disabled
try {
// if no previous position is known automatically try to get the current one
return await Geolocator.getCurrentPosition();
}
on LocationServiceDisabledException {
return null;
locationIndicatorController.addRawListener(complete);
return completer.future;
}
}

Future<bool> _requestLocationPermission() async {
final permission = await Geolocator.requestPermission();
return permission == LocationPermission.always ||
permission == LocationPermission.whileInUse;
}

//////////////////////////////
/// Preferences properties ///
Expand Down

0 comments on commit 04cb158

Please sign in to comment.