diff --git a/lib/controller/dashboard/attendance_screen_controller.dart b/lib/controller/dashboard/attendance_screen_controller.dart index 234c2c3..00b663e 100644 --- a/lib/controller/dashboard/attendance_screen_controller.dart +++ b/lib/controller/dashboard/attendance_screen_controller.dart @@ -93,12 +93,10 @@ class AttendanceController extends GetxController { if (response != null && response.isNotEmpty) { projects = response.map((json) => ProjectModel.fromJson(json)).toList(); - selectedProjectId = projects.first.id.toString(); log.i("Projects fetched: ${projects.length}"); - - await fetchProjectData(selectedProjectId); } else { - log.w("No projects found or API call failed."); + log.e("Failed to fetch projects or no projects available."); + projects = []; } isLoadingProjects.value = false; @@ -107,6 +105,13 @@ class AttendanceController extends GetxController { update(['attendance_dashboard_controller']); } + Future loadAttendanceData(String projectId) async { + await fetchEmployeesByProject(projectId); + await fetchAttendanceLogs(projectId); + await fetchRegularizationLogs(projectId); + await fetchProjectData(projectId); + } + /// Fetches employees, attendance logs and regularization logs for a project. Future fetchProjectData(String? projectId) async { if (projectId == null) return; @@ -176,7 +181,8 @@ class AttendanceController extends GetxController { return false; } - final compressedBytes = await compressImageToUnder100KB(File(image.path)); + final compressedBytes = + await compressImageToUnder100KB(File(image.path)); if (compressedBytes == null) { log.e("Image compression failed."); uploadingStates[employeeId]?.value = false; @@ -239,9 +245,9 @@ class AttendanceController extends GetxController { lastDate: todayDateOnly.subtract(const Duration(days: 1)), initialDateRange: DateTimeRange( start: startDateAttendance ?? today.subtract(const Duration(days: 7)), - end: endDateAttendance ?? todayDateOnly.subtract(const Duration(days: 1)), + end: endDateAttendance ?? + todayDateOnly.subtract(const Duration(days: 1)), ), - builder: (BuildContext context, Widget? child) { return Center( child: SizedBox( @@ -332,7 +338,8 @@ class AttendanceController extends GetxController { return dateB.compareTo(dateA); }); - final sortedMap = Map>.fromEntries(sortedEntries); + final sortedMap = + Map>.fromEntries(sortedEntries); log.i("Logs grouped and sorted by check-in date."); return sortedMap; @@ -352,8 +359,9 @@ class AttendanceController extends GetxController { final response = await ApiService.getRegularizationLogs(projectId); if (response != null) { - regularizationLogs = - response.map((json) => RegularizationLogModel.fromJson(json)).toList(); + regularizationLogs = response + .map((json) => RegularizationLogModel.fromJson(json)) + .toList(); log.i("Regularization logs fetched: ${regularizationLogs.length}"); update(); } else { @@ -374,8 +382,9 @@ class AttendanceController extends GetxController { final response = await ApiService.getAttendanceLogView(id); if (response != null) { - attendenceLogsView = - response.map((json) => AttendanceLogViewModel.fromJson(json)).toList(); + attendenceLogsView = response + .map((json) => AttendanceLogViewModel.fromJson(json)) + .toList(); attendenceLogsView.sort((a, b) { if (a.activityTime == null || b.activityTime == null) return 0; diff --git a/lib/view/dashboard/Attendence/attendance_screen.dart b/lib/view/dashboard/Attendence/attendance_screen.dart index eb0f3cc..0d47140 100644 --- a/lib/view/dashboard/Attendence/attendance_screen.dart +++ b/lib/view/dashboard/Attendence/attendance_screen.dart @@ -40,23 +40,29 @@ class _AttendanceScreenState extends State with UIMixin { final projectController = Get.find(); final attendanceController = Get.find(); - WidgetsBinding.instance.addPostFrameCallback((_) { - ever( - projectController.selectedProjectId!, - (projectId) async { - if (projectId != null && projectId.isNotEmpty) { - try { - await attendanceController.fetchEmployeesByProject(projectId); - await attendanceController.fetchAttendanceLogs(projectId); - await attendanceController.fetchRegularizationLogs(projectId); - await attendanceController.fetchProjectData(projectId); - attendanceController.update(['attendance_dashboard_controller']); - } catch (e) { - debugPrint("Error updating data on project change: $e"); - } + WidgetsBinding.instance.addPostFrameCallback((_) async { + // Listen for future changes in selected project + ever(projectController.selectedProjectId!, (projectId) async { + if (projectId != null && projectId.isNotEmpty) { + try { + await attendanceController.loadAttendanceData(projectId); + attendanceController.update(['attendance_dashboard_controller']); + } catch (e) { + debugPrint("Error updating data on project change: $e"); } - }, - ); + } + }); + + // Load data initially if project is already selected + final initialProjectId = projectController.selectedProjectId?.value; + if (initialProjectId != null && initialProjectId.isNotEmpty) { + try { + await attendanceController.loadAttendanceData(initialProjectId); + attendanceController.update(['attendance_dashboard_controller']); + } catch (e) { + debugPrint("Error loading initial data: $e"); + } + } }); } @@ -159,11 +165,7 @@ class _AttendanceScreenState extends State with UIMixin { final selectedView = result['selectedTab'] as String?; - if (selectedProjectId != null && - selectedProjectId != - attendanceController.selectedProjectId) { - attendanceController.selectedProjectId = - selectedProjectId; + if (selectedProjectId != null) { try { await attendanceController .fetchEmployeesByProject( @@ -211,17 +213,10 @@ class _AttendanceScreenState extends State with UIMixin { final projectId = Get.find() .selectedProjectId ?.value; - if (projectId != null && projectId.isNotEmpty) { try { await attendanceController - .fetchEmployeesByProject(projectId); - await attendanceController - .fetchAttendanceLogs(projectId); - await attendanceController - .fetchRegularizationLogs(projectId); - await attendanceController - .fetchProjectData(projectId); + .loadAttendanceData(projectId); attendanceController.update( ['attendance_dashboard_controller']); } catch (e) {