import 'package:fforte/screens/addCam/exceptions/location_disabled_exception.dart'; import 'package:fforte/screens/addCam/exceptions/location_forbidden_exception.dart'; import 'package:fforte/screens/excursion/exceptions/need_always_location_exception.dart'; import 'package:geolocator/geolocator.dart'; class GeolocatorService { // determine live position with checks for denied permission and turned off location service static Future deteterminePosition({bool alwaysOnNeeded = false}) async { bool locationEnabled; LocationPermission permissionGiven; locationEnabled = await Geolocator.isLocationServiceEnabled(); if (!locationEnabled) { throw LocationDisabledException(); } permissionGiven = await Geolocator.checkPermission(); if (permissionGiven == LocationPermission.denied) { permissionGiven = await Geolocator.requestPermission(); if (permissionGiven == LocationPermission.denied || permissionGiven == LocationPermission.deniedForever) { throw LocationForbiddenException(); } } if (alwaysOnNeeded && permissionGiven != LocationPermission.always) { throw NeedAlwaysLocation(); } return await Geolocator.getCurrentPosition(); } static Future alwaysPositionEnabled() async { LocationPermission permissionGiven = await Geolocator.checkPermission(); bool locationEnabled = await Geolocator.isLocationServiceEnabled(); if (permissionGiven == LocationPermission.always || !locationEnabled) { return true; } else { return false; } } }