feat: Refactor attendance data loading logic to streamline project change handling and improve initial data fetch

This commit is contained in:
Vaibhav Surve 2025-06-12 19:29:38 +05:30
parent daf132c3b5
commit 11d9f107ad
2 changed files with 45 additions and 41 deletions

View File

@ -93,12 +93,10 @@ class AttendanceController extends GetxController {
if (response != null && response.isNotEmpty) { if (response != null && response.isNotEmpty) {
projects = response.map((json) => ProjectModel.fromJson(json)).toList(); projects = response.map((json) => ProjectModel.fromJson(json)).toList();
selectedProjectId = projects.first.id.toString();
log.i("Projects fetched: ${projects.length}"); log.i("Projects fetched: ${projects.length}");
await fetchProjectData(selectedProjectId);
} else { } else {
log.w("No projects found or API call failed."); log.e("Failed to fetch projects or no projects available.");
projects = [];
} }
isLoadingProjects.value = false; isLoadingProjects.value = false;
@ -107,6 +105,13 @@ class AttendanceController extends GetxController {
update(['attendance_dashboard_controller']); update(['attendance_dashboard_controller']);
} }
Future<void> 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. /// Fetches employees, attendance logs and regularization logs for a project.
Future<void> fetchProjectData(String? projectId) async { Future<void> fetchProjectData(String? projectId) async {
if (projectId == null) return; if (projectId == null) return;
@ -176,7 +181,8 @@ class AttendanceController extends GetxController {
return false; return false;
} }
final compressedBytes = await compressImageToUnder100KB(File(image.path)); final compressedBytes =
await compressImageToUnder100KB(File(image.path));
if (compressedBytes == null) { if (compressedBytes == null) {
log.e("Image compression failed."); log.e("Image compression failed.");
uploadingStates[employeeId]?.value = false; uploadingStates[employeeId]?.value = false;
@ -239,9 +245,9 @@ class AttendanceController extends GetxController {
lastDate: todayDateOnly.subtract(const Duration(days: 1)), lastDate: todayDateOnly.subtract(const Duration(days: 1)),
initialDateRange: DateTimeRange( initialDateRange: DateTimeRange(
start: startDateAttendance ?? today.subtract(const Duration(days: 7)), 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) { builder: (BuildContext context, Widget? child) {
return Center( return Center(
child: SizedBox( child: SizedBox(
@ -332,7 +338,8 @@ class AttendanceController extends GetxController {
return dateB.compareTo(dateA); return dateB.compareTo(dateA);
}); });
final sortedMap = Map<String, List<AttendanceLogModel>>.fromEntries(sortedEntries); final sortedMap =
Map<String, List<AttendanceLogModel>>.fromEntries(sortedEntries);
log.i("Logs grouped and sorted by check-in date."); log.i("Logs grouped and sorted by check-in date.");
return sortedMap; return sortedMap;
@ -352,8 +359,9 @@ class AttendanceController extends GetxController {
final response = await ApiService.getRegularizationLogs(projectId); final response = await ApiService.getRegularizationLogs(projectId);
if (response != null) { if (response != null) {
regularizationLogs = regularizationLogs = response
response.map((json) => RegularizationLogModel.fromJson(json)).toList(); .map((json) => RegularizationLogModel.fromJson(json))
.toList();
log.i("Regularization logs fetched: ${regularizationLogs.length}"); log.i("Regularization logs fetched: ${regularizationLogs.length}");
update(); update();
} else { } else {
@ -374,8 +382,9 @@ class AttendanceController extends GetxController {
final response = await ApiService.getAttendanceLogView(id); final response = await ApiService.getAttendanceLogView(id);
if (response != null) { if (response != null) {
attendenceLogsView = attendenceLogsView = response
response.map((json) => AttendanceLogViewModel.fromJson(json)).toList(); .map((json) => AttendanceLogViewModel.fromJson(json))
.toList();
attendenceLogsView.sort((a, b) { attendenceLogsView.sort((a, b) {
if (a.activityTime == null || b.activityTime == null) return 0; if (a.activityTime == null || b.activityTime == null) return 0;

View File

@ -40,23 +40,29 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
final projectController = Get.find<ProjectController>(); final projectController = Get.find<ProjectController>();
final attendanceController = Get.find<AttendanceController>(); final attendanceController = Get.find<AttendanceController>();
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) async {
ever<String?>( // Listen for future changes in selected project
projectController.selectedProjectId!, ever<String?>(projectController.selectedProjectId!, (projectId) async {
(projectId) async { if (projectId != null && projectId.isNotEmpty) {
if (projectId != null && projectId.isNotEmpty) { try {
try { await attendanceController.loadAttendanceData(projectId);
await attendanceController.fetchEmployeesByProject(projectId); attendanceController.update(['attendance_dashboard_controller']);
await attendanceController.fetchAttendanceLogs(projectId); } catch (e) {
await attendanceController.fetchRegularizationLogs(projectId); debugPrint("Error updating data on project change: $e");
await attendanceController.fetchProjectData(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<AttendanceScreen> with UIMixin {
final selectedView = final selectedView =
result['selectedTab'] as String?; result['selectedTab'] as String?;
if (selectedProjectId != null && if (selectedProjectId != null) {
selectedProjectId !=
attendanceController.selectedProjectId) {
attendanceController.selectedProjectId =
selectedProjectId;
try { try {
await attendanceController await attendanceController
.fetchEmployeesByProject( .fetchEmployeesByProject(
@ -211,17 +213,10 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
final projectId = Get.find<ProjectController>() final projectId = Get.find<ProjectController>()
.selectedProjectId .selectedProjectId
?.value; ?.value;
if (projectId != null && projectId.isNotEmpty) { if (projectId != null && projectId.isNotEmpty) {
try { try {
await attendanceController await attendanceController
.fetchEmployeesByProject(projectId); .loadAttendanceData(projectId);
await attendanceController
.fetchAttendanceLogs(projectId);
await attendanceController
.fetchRegularizationLogs(projectId);
await attendanceController
.fetchProjectData(projectId);
attendanceController.update( attendanceController.update(
['attendance_dashboard_controller']); ['attendance_dashboard_controller']);
} catch (e) { } catch (e) {