import 'package:flutter/foundation.dart' show kIsWeb; import 'package:permission_handler/permission_handler.dart'; import '../services/permission_service.dart'; /// Helpers for requesting and checking the platform location permission. /// /// Mirrors the pattern in [notification_permission.dart] so that location /// can be requested at app launch and before location-dependent actions. Future requestLocationPermission() async { if (kIsWeb) { return PermissionStatus.granted; } return requestPermission(Permission.location); } /// Ensures location permission is granted. Returns `true` if granted. /// /// Call on app launch and before check-in, check-out, or toggling location /// tracking so the user always has the opportunity to grant permission. Future ensureLocationPermission() async { if (kIsWeb) return true; final status = await Permission.location.status; if (status.isGranted) return true; if (status.isDenied || status.isRestricted || status.isLimited) { final newStatus = await requestLocationPermission(); return newStatus.isGranted; } // permanently denied – send user to settings so they can enable it await openAppSettings(); return false; } /// Ensures "Allow all the time" (background) location permission is granted. /// /// This is required for `background_locator_2` to track location when the app /// is backgrounded or killed. On Android 10+ this triggers the system prompt /// for `ACCESS_BACKGROUND_LOCATION`. Must be called **after** the foreground /// location permission (`Permission.location`) has been granted. Future ensureBackgroundLocationPermission() async { if (kIsWeb) return true; // Foreground location must be granted first; this helper already opens // settings if the user permanently denied the fine/coarse prompt. final fgGranted = await ensureLocationPermission(); if (!fgGranted) return false; final bgStatus = await Permission.locationAlways.status; if (bgStatus.isGranted) return true; if (bgStatus.isDenied || bgStatus.isRestricted || bgStatus.isLimited) { final newStatus = await Permission.locationAlways.request(); if (newStatus.isGranted) return true; if (newStatus.isPermanentlyDenied) { // user chose "Don't ask again" – send them to settings await openAppSettings(); } return false; } // Permanently denied – user must open settings await openAppSettings(); return false; }