Add location permission handling in AttendanceController and improve fetchLogsView method

This commit is contained in:
Vaibhav Surve 2025-05-09 20:56:06 +05:30
parent 82dcf0c8fe
commit 4ce22b149f

View File

@ -49,6 +49,26 @@ class AttendanceController extends GetxController {
log.i("Default date range set: $startDateAttendance to $endDateAttendance");
}
Future<bool> _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<void> 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<void> fetchLogsView(String? id) async {
if (id == null) return;
Future<void> 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");
}
}
}
}