// * Service for handling GPS location functionality // * Provides methods for: // * - Location permission handling // * - GPS service status checks // * - Position determination // * - Always-on location checks 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'; /// Service class for handling all GPS location related functionality class GeolocatorService { /// Determine the current device position with permission checks /// @param alwaysOnNeeded Whether the app needs always-on location permission /// @throws LocationDisabledException if location services are disabled /// @throws LocationForbiddenException if location permission is denied /// @throws NeedAlwaysLocation if always-on permission is needed but not granted /// @return Future[Position] The current GPS position static Future deteterminePosition({bool alwaysOnNeeded = false}) async { bool locationEnabled; LocationPermission permissionGiven; // Check if location services are enabled locationEnabled = await Geolocator.isLocationServiceEnabled(); if (!locationEnabled) { throw LocationDisabledException(); } // Check and request location permissions if needed permissionGiven = await Geolocator.checkPermission(); if (permissionGiven == LocationPermission.denied) { permissionGiven = await Geolocator.requestPermission(); if (permissionGiven == LocationPermission.denied || permissionGiven == LocationPermission.deniedForever) { throw LocationForbiddenException(); } } // Check for always-on permission if required if (alwaysOnNeeded && permissionGiven != LocationPermission.always) { throw NeedAlwaysLocation(); } return await Geolocator.getCurrentPosition(); } /// Check if always-on location permission is enabled /// @return Future[bool] True if always-on permission is granted or location is disabled static Future alwaysPositionEnabled() async { LocationPermission permissionGiven = await Geolocator.checkPermission(); bool locationEnabled = await Geolocator.isLocationServiceEnabled(); if (permissionGiven == LocationPermission.always || !locationEnabled) { return true; } else { return false; } } }