diff --git a/lib/view/dashboard/attendanceScreen.dart b/lib/view/dashboard/attendanceScreen.dart index dbc63a2..982b6b8 100644 --- a/lib/view/dashboard/attendanceScreen.dart +++ b/lib/view/dashboard/attendanceScreen.dart @@ -624,12 +624,11 @@ class _AttendanceScreenState extends State with UIMixin { ), DataCell( Obx(() { - // Check if any record for this employee is uploading final uniqueLogKey = '${log.employeeId}_${log.id}'; final isUploading = attendanceController.uploadingStates[uniqueLogKey]?.value ?? false; - // Check if both checkIn and checkOut exist and the date is yesterday + final isYesterday = log.checkIn != null && log.checkOut != null && DateUtils.isSameDay(log.checkIn!, @@ -637,21 +636,26 @@ class _AttendanceScreenState extends State with UIMixin { DateUtils.isSameDay(log.checkOut!, DateTime.now().subtract(Duration(days: 1))); + final isTodayApproved = log.activity == 4 && + DateUtils.isSameDay( + log.checkIn ?? DateTime(2000), DateTime.now()); + + final isApprovedButNotToday = + log.activity == 4 && !isTodayApproved; + + final isButtonDisabled = isUploading || + isYesterday || + log.activity == 2 || + log.activity == 5 || + isApprovedButNotToday; + return SizedBox( width: 90, height: 25, child: ElevatedButton( - onPressed: isUploading || - isYesterday || - log.activity == 2 || - log.activity == 5 || - (log.activity == 4 && - !(DateUtils.isSameDay( - log.checkIn ?? DateTime(2000), - DateTime.now()))) + onPressed: isButtonDisabled ? null : () async { - // Set the uploading state for the employee when the action starts attendanceController.uploadingStates[uniqueLogKey] = RxBool(true); @@ -666,17 +670,16 @@ class _AttendanceScreenState extends State with UIMixin { return; } - // Existing logic for updating action int updatedAction; String actionText; bool imageCapture = true; + if (log.activity == 0) { updatedAction = 0; actionText = "Check In"; } else if (log.activity == 1) { - DateTime currentDate = DateTime.now(); - DateTime twoDaysAgo = - currentDate.subtract(Duration(days: 2)); + final twoDaysAgo = + DateTime.now().subtract(Duration(days: 2)); if (log.checkOut == null && log.checkIn != null && @@ -695,11 +698,7 @@ class _AttendanceScreenState extends State with UIMixin { } else if (log.activity == 2) { updatedAction = 2; actionText = "Request Regularize"; - } else if (log.activity == 4 && - log.checkOut != null && - log.checkIn != null && - DateTime.now().difference(log.checkIn!).inDays <= - 2) { + } else if (isTodayApproved) { updatedAction = 0; actionText = "Check In"; } else { @@ -707,7 +706,6 @@ class _AttendanceScreenState extends State with UIMixin { actionText = "Unknown Action"; } - // Proceed with capturing and uploading attendance final success = await attendanceController .captureAndUploadAttendance( log.id, @@ -718,7 +716,6 @@ class _AttendanceScreenState extends State with UIMixin { imageCapture: imageCapture, ); - // Show result in SnackBar ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(success @@ -727,12 +724,10 @@ class _AttendanceScreenState extends State with UIMixin { ), ); - // Reset the uploading state after the action is complete attendanceController.uploadingStates[uniqueLogKey] = RxBool(false); if (success) { - // Update the UI with the new data attendanceController.fetchEmployeesByProject( attendanceController.selectedProjectId!); attendanceController.fetchAttendanceLogs( @@ -746,15 +741,8 @@ class _AttendanceScreenState extends State with UIMixin { }, style: ElevatedButton.styleFrom( backgroundColor: isYesterday - ? Colors - .grey // Button color for the disabled state (Yesterday's date) - : (log.activity == 4 && - log.checkOut != null && - log.checkIn != null && - DateTime.now() - .difference(log.checkIn!) - .inDays <= - 2) + ? Colors.grey + : isTodayApproved ? Colors.green : AttendanceActionColors.colors[(log.activity == 0) ? ButtonActions.checkIn @@ -774,20 +762,14 @@ class _AttendanceScreenState extends State with UIMixin { ), ) : Text( - (log.activity == 5) + log.activity == 5 ? ButtonActions.rejected - : (log.activity == 4 && - log.checkOut != null && - log.checkIn != null && - DateTime.now() - .difference(log.checkIn!) - .inDays <= - 2) + : isTodayApproved ? ButtonActions.checkIn - : (log.activity == 2) - ? "Requested" - : (log.activity == 4) - ? ButtonActions.approved + : log.activity == 4 + ? ButtonActions.approved + : log.activity == 2 + ? ButtonActions.requested : (log.activity == 0 && !(log.checkIn != null && log.checkOut != null && @@ -803,17 +785,16 @@ class _AttendanceScreenState extends State with UIMixin { .inDays <= 2) ? ButtonActions.checkOut - : (log.activity == 2 || - (log.activity == 1 && - log.checkOut == - null && - log.checkIn != - null && - log.checkIn!.isBefore( - DateTime.now() - .subtract(Duration( + : (log.activity == 1 && + log.checkOut == + null && + log.checkIn != null && + log.checkIn!.isBefore( + DateTime.now() + .subtract( + Duration( days: - 2))))) + 2)))) ? ButtonActions .requestRegularize : ButtonActions.checkOut,