diff --git a/lib/controller/dashboard/attendance_screen_controller.dart b/lib/controller/dashboard/attendance_screen_controller.dart index 7c41018..bf68af0 100644 --- a/lib/controller/dashboard/attendance_screen_controller.dart +++ b/lib/controller/dashboard/attendance_screen_controller.dart @@ -49,6 +49,26 @@ class AttendanceController extends GetxController { log.i("Default date range set: $startDateAttendance to $endDateAttendance"); } + Future _handleLocationPermission() async { + LocationPermission permission; + + permission = await Geolocator.checkPermission(); + if (permission == LocationPermission.denied) { + permission = await Geolocator.requestPermission(); + if (permission == LocationPermission.denied) { + log.w('Location permissions are denied'); + return false; + } + } + + if (permission == LocationPermission.deniedForever) { + log.e('Location permissions are permanently denied'); + return false; + } + + return true; + } + Future fetchProjects() async { isLoading.value = true; final response = await ApiService.getProjects(); @@ -125,6 +145,12 @@ class AttendanceController extends GetxController { } } + final hasLocationPermission = await _handleLocationPermission(); + if (!hasLocationPermission) { + uploadingStates[employeeId]?.value = false; + return false; + } + final position = await Geolocator.getCurrentPosition( desiredAccuracy: LocationAccuracy.high, ); @@ -260,28 +286,28 @@ class AttendanceController extends GetxController { } } - Future fetchLogsView(String? id) async { - if (id == null) return; + Future fetchLogsView(String? id) async { + if (id == null) return; - isLoading.value = true; - final response = await ApiService.getAttendanceLogView(id); - isLoading.value = false; + isLoading.value = true; + final response = await ApiService.getAttendanceLogView(id); + isLoading.value = false; - if (response != null) { - attendenceLogsView = response - .map((json) => AttendanceLogViewModel.fromJson(json)) - .toList(); + if (response != null) { + attendenceLogsView = response + .map((json) => AttendanceLogViewModel.fromJson(json)) + .toList(); - // Sort by activityTime field (latest first) - attendenceLogsView.sort((a, b) { + // Sort by activityTime field (latest first) + attendenceLogsView.sort((a, b) { if (a.activityTime == null || b.activityTime == null) return 0; // Handle null values if any return b.activityTime!.compareTo(a.activityTime!); // Sort descending (latest first) - }); + }); - log.i("Attendance log view fetched for ID: $id"); - update(); - } else { - log.e("Failed to fetch attendance log view for ID $id"); + log.i("Attendance log view fetched for ID: $id"); + update(); + } else { + log.e("Failed to fetch attendance log view for ID $id"); + } } } -}