feat: Refactor attendance data loading logic to streamline project change handling and improve initial data fetch
This commit is contained in:
parent
daf132c3b5
commit
11d9f107ad
@ -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<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.
|
||||
Future<void> 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<String, List<AttendanceLogModel>>.fromEntries(sortedEntries);
|
||||
final sortedMap =
|
||||
Map<String, List<AttendanceLogModel>>.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;
|
||||
|
@ -40,23 +40,29 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
|
||||
final projectController = Get.find<ProjectController>();
|
||||
final attendanceController = Get.find<AttendanceController>();
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
ever<String?>(
|
||||
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<String?>(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<AttendanceScreen> 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<AttendanceScreen> with UIMixin {
|
||||
final projectId = Get.find<ProjectController>()
|
||||
.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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user