From 3b497fecafc0fa7c96a88924e6ffe2db1bb68db7 Mon Sep 17 00:00:00 2001 From: Vaibhav Surve Date: Wed, 17 Sep 2025 11:32:32 +0530 Subject: [PATCH] feat: Update API endpoint configuration; enhance attendance logs sorting and grouping logic --- lib/helpers/services/api_endpoints.dart | 4 +- lib/helpers/utils/date_time_utils.dart | 37 ++-- lib/view/Attendence/attendance_logs_tab.dart | 182 ++++++++++++------- 3 files changed, 131 insertions(+), 92 deletions(-) diff --git a/lib/helpers/services/api_endpoints.dart b/lib/helpers/services/api_endpoints.dart index eee0216..0d4f903 100644 --- a/lib/helpers/services/api_endpoints.dart +++ b/lib/helpers/services/api_endpoints.dart @@ -1,6 +1,6 @@ class ApiEndpoints { - static const String baseUrl = "https://stageapi.marcoaiot.com/api"; - // static const String baseUrl = "https://api.marcoaiot.com/api"; + // static const String baseUrl = "https://stageapi.marcoaiot.com/api"; + static const String baseUrl = "https://api.marcoaiot.com/api"; // static const String baseUrl = "https://devapi.marcoaiot.com/api"; // Dashboard Module API Endpoints diff --git a/lib/helpers/utils/date_time_utils.dart b/lib/helpers/utils/date_time_utils.dart index cf446f5..169ce78 100644 --- a/lib/helpers/utils/date_time_utils.dart +++ b/lib/helpers/utils/date_time_utils.dart @@ -1,12 +1,10 @@ import 'package:intl/intl.dart'; -// import 'package:marco/helpers/services/app_logger.dart'; class DateTimeUtils { /// Converts a UTC datetime string to local time and formats it. - static String convertUtcToLocal(String utcTimeString, {String format = 'dd-MM-yyyy'}) { + static String convertUtcToLocal(String utcTimeString, + {String format = 'dd-MM-yyyy'}) { try { - // logSafe('Received UTC string: $utcTimeString'); // 🔹 Log input - final parsed = DateTime.parse(utcTimeString); final utcDateTime = DateTime.utc( parsed.year, @@ -20,16 +18,8 @@ class DateTimeUtils { ); final localDateTime = utcDateTime.toLocal(); - - final formatted = _formatDateTime(localDateTime, format: format); - - // logSafe('Converted Local DateTime: $localDateTime'); // 🔹 Log raw local datetime - // logSafe('Formatted Local DateTime: $formatted'); // 🔹 Log formatted string - - return formatted; - } catch (e, stackTrace) { - // logSafe('DateTime conversion failed: $e', - // error: e, stackTrace: stackTrace); + return _formatDateTime(localDateTime, format: format); + } catch (e) { return 'Invalid Date'; } } @@ -37,17 +27,24 @@ class DateTimeUtils { /// Public utility for formatting any DateTime. static String formatDate(DateTime date, String format) { try { - final formatted = DateFormat(format).format(date); - // logSafe('Formatted DateTime ($date) => $formatted'); // 🔹 Log input/output - return formatted; - } catch (e, stackTrace) { - // logSafe('formatDate failed: $e', error: e, stackTrace: stackTrace); + return DateFormat(format).format(date); + } catch (e) { return 'Invalid Date'; } } + /// Parses a date string using the given format. + static DateTime? parseDate(String dateString, String format) { + try { + return DateFormat(format).parse(dateString); + } catch (e) { + return null; + } + } + /// Internal formatter with default format. - static String _formatDateTime(DateTime dateTime, {String format = 'dd-MM-yyyy'}) { + static String _formatDateTime(DateTime dateTime, + {String format = 'dd-MM-yyyy'}) { return DateFormat(format).format(dateTime); } } diff --git a/lib/view/Attendence/attendance_logs_tab.dart b/lib/view/Attendence/attendance_logs_tab.dart index 7309524..24fbd2b 100644 --- a/lib/view/Attendence/attendance_logs_tab.dart +++ b/lib/view/Attendence/attendance_logs_tab.dart @@ -10,6 +10,8 @@ import 'package:marco/helpers/widgets/my_text.dart'; import 'package:marco/helpers/widgets/my_custom_skeleton.dart'; import 'package:marco/model/attendance/log_details_view.dart'; import 'package:marco/model/attendance/attendence_action_button.dart'; +import 'package:marco/helpers/utils/attendance_actions.dart'; +import 'package:marco/helpers/services/app_logger.dart'; class AttendanceLogsTab extends StatefulWidget { final AttendanceController controller; @@ -23,8 +25,9 @@ class AttendanceLogsTab extends StatefulWidget { class _AttendanceLogsTabState extends State { Widget _buildStatusHeader() { return Obx(() { - final showPending = widget.controller.showPendingOnly.value; - if (!showPending) return const SizedBox.shrink(); + if (!widget.controller.showPendingOnly.value) { + return const SizedBox.shrink(); + } return Container( width: double.infinity, @@ -32,11 +35,7 @@ class _AttendanceLogsTabState extends State { color: Colors.orange.shade50, child: Row( children: [ - const Icon( - Icons.pending_actions, - color: Colors.orange, - size: 18, - ), + const Icon(Icons.pending_actions, color: Colors.orange, size: 18), const SizedBox(width: 8), const Expanded( child: Text( @@ -48,14 +47,8 @@ class _AttendanceLogsTabState extends State { ), ), InkWell( - onTap: () { - widget.controller.showPendingOnly.value = false; - }, - child: const Icon( - Icons.close, - size: 18, - color: Colors.orange, - ), + onTap: () => widget.controller.showPendingOnly.value = false, + child: const Icon(Icons.close, size: 18, color: Colors.orange), ), ], ), @@ -63,25 +56,86 @@ class _AttendanceLogsTabState extends State { }); } + /// Return button text priority for sorting inside same date + int _getActionPriority(employee) { + final text = AttendanceButtonHelper.getButtonText( + activity: employee.activity, + checkIn: employee.checkIn, + checkOut: employee.checkOut, + isTodayApproved: AttendanceButtonHelper.isTodayApproved( + employee.activity, + employee.checkIn, + ), + ).toLowerCase(); + + final isYesterdayCheckIn = employee.checkIn != null && + DateUtils.isSameDay( + employee.checkIn, + DateTime.now().subtract(const Duration(days: 1)), + ); + final isMissingCheckout = employee.checkOut == null; + + final isCheckoutAction = + text.contains("checkout") || text.contains("check out"); + + int priority; + if (isYesterdayCheckIn && isMissingCheckout && isCheckoutAction) { + priority = 0; + } else if (isCheckoutAction) { + priority = 0; + } else if (text.contains("regular")) { + priority = 1; + } else if (text == "requested") { + priority = 2; + } else if (text == "approved") { + priority = 3; + } else if (text == "rejected") { + priority = 4; + } else { + priority = 5; + } + + // ✅ Use AppLogger instead of print + logSafe( + "[AttendanceLogs] Priority calculated " + "name=${employee.name}, activity=${employee.activity}, " + "checkIn=${employee.checkIn}, checkOut=${employee.checkOut}, " + "buttonText=$text, priority=$priority", + level: LogLevel.debug, + ); + + return priority; + } + @override Widget build(BuildContext context) { return Obx(() { - final logs = List.of(widget.controller.filteredLogs); - logs.sort((a, b) { - final aDate = a.checkIn ?? DateTime(0); - final bDate = b.checkIn ?? DateTime(0); - return bDate.compareTo(aDate); - }); + final allLogs = List.of(widget.controller.filteredLogs); - // Use controller's observable for pending filter + // Filter logs if "pending only" final showPendingOnly = widget.controller.showPendingOnly.value; - final filteredLogs = showPendingOnly - ? logs - .where((employee) => - employee.activity == 1 || employee.activity == 2) + ? allLogs + .where((emp) => emp.activity == 1 || emp.activity == 2) .toList() - : logs; + : allLogs; + + // Group logs by date string + final groupedLogs = >{}; + for (var log in filteredLogs) { + final dateKey = log.checkIn != null + ? DateTimeUtils.formatDate(log.checkIn!, 'dd MMM yyyy') + : 'Unknown'; + groupedLogs.putIfAbsent(dateKey, () => []).add(log); + } + + // Sort dates (latest first) + final sortedDates = groupedLogs.keys.toList() + ..sort((a, b) { + final da = DateTimeUtils.parseDate(a, 'dd MMM yyyy') ?? DateTime(0); + final db = DateTimeUtils.parseDate(b, 'dd MMM yyyy') ?? DateTime(0); + return db.compareTo(da); + }); final dateRangeText = widget.controller.startDateAttendance != null && widget.controller.endDateAttendance != null @@ -92,7 +146,7 @@ class _AttendanceLogsTabState extends State { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - // Header row: Title and Date Range + // Header row Padding( padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8), child: Row( @@ -111,11 +165,11 @@ class _AttendanceLogsTabState extends State { ), ), - // ✅ Pending status header + // Pending-only header _buildStatusHeader(), MySpacing.height(8), - // Content: Skeleton, Empty, or List + // Content: loader, empty, or logs if (widget.controller.isLoadingAttendanceLogs.value) SkeletonLoaders.employeeListSkeletonLoader() else if (filteredLogs.isEmpty) @@ -131,39 +185,28 @@ class _AttendanceLogsTabState extends State { MyCard.bordered( paddingAll: 8, child: Column( - children: List.generate(filteredLogs.length, (index) { - final employee = filteredLogs[index]; - final currentDate = employee.checkIn != null - ? DateTimeUtils.formatDate( - employee.checkIn!, 'dd MMM yyyy') - : ''; - final previousDate = - index > 0 && filteredLogs[index - 1].checkIn != null - ? DateTimeUtils.formatDate( - filteredLogs[index - 1].checkIn!, 'dd MMM yyyy') - : ''; - final showDateHeader = - index == 0 || currentDate != previousDate; + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + for (final date in sortedDates) ...[ + Padding( + padding: const EdgeInsets.symmetric(vertical: 8), + child: MyText.bodyMedium(date, fontWeight: 700), + ), - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - if (showDateHeader) - Padding( - padding: const EdgeInsets.symmetric(vertical: 8), - child: MyText.bodyMedium( - currentDate, - fontWeight: 700, - ), - ), + // Sort employees inside this date by action priority + for (final emp in (groupedLogs[date]! + ..sort( + (a, b) => _getActionPriority(a) + .compareTo(_getActionPriority(b)), + ))) ...[ MyContainer( paddingAll: 8, child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Avatar( - firstName: employee.firstName, - lastName: employee.lastName, + firstName: emp.firstName, + lastName: emp.lastName, size: 31, ), MySpacing.width(16), @@ -175,7 +218,7 @@ class _AttendanceLogsTabState extends State { children: [ Flexible( child: MyText.bodyMedium( - employee.name, + emp.name, fontWeight: 600, overflow: TextOverflow.ellipsis, ), @@ -183,7 +226,7 @@ class _AttendanceLogsTabState extends State { MySpacing.width(6), Flexible( child: MyText.bodySmall( - '(${employee.designation})', + '(${emp.designation})', fontWeight: 600, color: Colors.grey[700], overflow: TextOverflow.ellipsis, @@ -192,28 +235,28 @@ class _AttendanceLogsTabState extends State { ], ), MySpacing.height(8), - if (employee.checkIn != null || - employee.checkOut != null) + if (emp.checkIn != null || + emp.checkOut != null) Row( children: [ - if (employee.checkIn != null) ...[ + if (emp.checkIn != null) ...[ const Icon(Icons.arrow_circle_right, size: 16, color: Colors.green), MySpacing.width(4), MyText.bodySmall( DateTimeUtils.formatDate( - employee.checkIn!, 'hh:mm a'), + emp.checkIn!, 'hh:mm a'), fontWeight: 600, ), MySpacing.width(16), ], - if (employee.checkOut != null) ...[ + if (emp.checkOut != null) ...[ const Icon(Icons.arrow_circle_left, size: 16, color: Colors.red), MySpacing.width(4), MyText.bodySmall( DateTimeUtils.formatDate( - employee.checkOut!, 'hh:mm a'), + emp.checkOut!, 'hh:mm a'), fontWeight: 600, ), ], @@ -224,12 +267,12 @@ class _AttendanceLogsTabState extends State { mainAxisAlignment: MainAxisAlignment.end, children: [ AttendanceActionButton( - employee: employee, + employee: emp, attendanceController: widget.controller, ), MySpacing.width(8), AttendanceLogViewButton( - employee: employee, + employee: emp, attendanceController: widget.controller, ), ], @@ -240,11 +283,10 @@ class _AttendanceLogsTabState extends State { ], ), ), - if (index != filteredLogs.length - 1) - Divider(color: Colors.grey.withOpacity(0.3)), + Divider(color: Colors.grey.withOpacity(0.3)), ], - ); - }), + ], + ], ), ), ],