feat: Update API endpoint configuration; enhance attendance logs sorting and grouping logic
This commit is contained in:
parent
8fb725a5cf
commit
3b497fecaf
@ -1,6 +1,6 @@
|
|||||||
class ApiEndpoints {
|
class ApiEndpoints {
|
||||||
static const String baseUrl = "https://stageapi.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://api.marcoaiot.com/api";
|
||||||
// static const String baseUrl = "https://devapi.marcoaiot.com/api";
|
// static const String baseUrl = "https://devapi.marcoaiot.com/api";
|
||||||
|
|
||||||
// Dashboard Module API Endpoints
|
// Dashboard Module API Endpoints
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
// import 'package:marco/helpers/services/app_logger.dart';
|
|
||||||
|
|
||||||
class DateTimeUtils {
|
class DateTimeUtils {
|
||||||
/// Converts a UTC datetime string to local time and formats it.
|
/// 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 {
|
try {
|
||||||
// logSafe('Received UTC string: $utcTimeString'); // 🔹 Log input
|
|
||||||
|
|
||||||
final parsed = DateTime.parse(utcTimeString);
|
final parsed = DateTime.parse(utcTimeString);
|
||||||
final utcDateTime = DateTime.utc(
|
final utcDateTime = DateTime.utc(
|
||||||
parsed.year,
|
parsed.year,
|
||||||
@ -20,16 +18,8 @@ class DateTimeUtils {
|
|||||||
);
|
);
|
||||||
|
|
||||||
final localDateTime = utcDateTime.toLocal();
|
final localDateTime = utcDateTime.toLocal();
|
||||||
|
return _formatDateTime(localDateTime, format: format);
|
||||||
final formatted = _formatDateTime(localDateTime, format: format);
|
} catch (e) {
|
||||||
|
|
||||||
// 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 'Invalid Date';
|
return 'Invalid Date';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -37,17 +27,24 @@ class DateTimeUtils {
|
|||||||
/// Public utility for formatting any DateTime.
|
/// Public utility for formatting any DateTime.
|
||||||
static String formatDate(DateTime date, String format) {
|
static String formatDate(DateTime date, String format) {
|
||||||
try {
|
try {
|
||||||
final formatted = DateFormat(format).format(date);
|
return DateFormat(format).format(date);
|
||||||
// logSafe('Formatted DateTime ($date) => $formatted'); // 🔹 Log input/output
|
} catch (e) {
|
||||||
return formatted;
|
|
||||||
} catch (e, stackTrace) {
|
|
||||||
// logSafe('formatDate failed: $e', error: e, stackTrace: stackTrace);
|
|
||||||
return 'Invalid Date';
|
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.
|
/// 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);
|
return DateFormat(format).format(dateTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,8 @@ import 'package:marco/helpers/widgets/my_text.dart';
|
|||||||
import 'package:marco/helpers/widgets/my_custom_skeleton.dart';
|
import 'package:marco/helpers/widgets/my_custom_skeleton.dart';
|
||||||
import 'package:marco/model/attendance/log_details_view.dart';
|
import 'package:marco/model/attendance/log_details_view.dart';
|
||||||
import 'package:marco/model/attendance/attendence_action_button.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 {
|
class AttendanceLogsTab extends StatefulWidget {
|
||||||
final AttendanceController controller;
|
final AttendanceController controller;
|
||||||
@ -23,8 +25,9 @@ class AttendanceLogsTab extends StatefulWidget {
|
|||||||
class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
||||||
Widget _buildStatusHeader() {
|
Widget _buildStatusHeader() {
|
||||||
return Obx(() {
|
return Obx(() {
|
||||||
final showPending = widget.controller.showPendingOnly.value;
|
if (!widget.controller.showPendingOnly.value) {
|
||||||
if (!showPending) return const SizedBox.shrink();
|
return const SizedBox.shrink();
|
||||||
|
}
|
||||||
|
|
||||||
return Container(
|
return Container(
|
||||||
width: double.infinity,
|
width: double.infinity,
|
||||||
@ -32,11 +35,7 @@ class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
|||||||
color: Colors.orange.shade50,
|
color: Colors.orange.shade50,
|
||||||
child: Row(
|
child: Row(
|
||||||
children: [
|
children: [
|
||||||
const Icon(
|
const Icon(Icons.pending_actions, color: Colors.orange, size: 18),
|
||||||
Icons.pending_actions,
|
|
||||||
color: Colors.orange,
|
|
||||||
size: 18,
|
|
||||||
),
|
|
||||||
const SizedBox(width: 8),
|
const SizedBox(width: 8),
|
||||||
const Expanded(
|
const Expanded(
|
||||||
child: Text(
|
child: Text(
|
||||||
@ -48,14 +47,8 @@ class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
InkWell(
|
InkWell(
|
||||||
onTap: () {
|
onTap: () => widget.controller.showPendingOnly.value = false,
|
||||||
widget.controller.showPendingOnly.value = false;
|
child: const Icon(Icons.close, size: 18, color: Colors.orange),
|
||||||
},
|
|
||||||
child: const Icon(
|
|
||||||
Icons.close,
|
|
||||||
size: 18,
|
|
||||||
color: Colors.orange,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@ -63,25 +56,86 @@ class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Obx(() {
|
return Obx(() {
|
||||||
final logs = List.of(widget.controller.filteredLogs);
|
final allLogs = 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);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Use controller's observable for pending filter
|
// Filter logs if "pending only"
|
||||||
final showPendingOnly = widget.controller.showPendingOnly.value;
|
final showPendingOnly = widget.controller.showPendingOnly.value;
|
||||||
|
|
||||||
final filteredLogs = showPendingOnly
|
final filteredLogs = showPendingOnly
|
||||||
? logs
|
? allLogs
|
||||||
.where((employee) =>
|
.where((emp) => emp.activity == 1 || emp.activity == 2)
|
||||||
employee.activity == 1 || employee.activity == 2)
|
|
||||||
.toList()
|
.toList()
|
||||||
: logs;
|
: allLogs;
|
||||||
|
|
||||||
|
// Group logs by date string
|
||||||
|
final groupedLogs = <String, List<dynamic>>{};
|
||||||
|
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 &&
|
final dateRangeText = widget.controller.startDateAttendance != null &&
|
||||||
widget.controller.endDateAttendance != null
|
widget.controller.endDateAttendance != null
|
||||||
@ -92,7 +146,7 @@ class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
|||||||
return Column(
|
return Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
// Header row: Title and Date Range
|
// Header row
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
|
||||||
child: Row(
|
child: Row(
|
||||||
@ -111,11 +165,11 @@ class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
// ✅ Pending status header
|
// Pending-only header
|
||||||
_buildStatusHeader(),
|
_buildStatusHeader(),
|
||||||
MySpacing.height(8),
|
MySpacing.height(8),
|
||||||
|
|
||||||
// Content: Skeleton, Empty, or List
|
// Content: loader, empty, or logs
|
||||||
if (widget.controller.isLoadingAttendanceLogs.value)
|
if (widget.controller.isLoadingAttendanceLogs.value)
|
||||||
SkeletonLoaders.employeeListSkeletonLoader()
|
SkeletonLoaders.employeeListSkeletonLoader()
|
||||||
else if (filteredLogs.isEmpty)
|
else if (filteredLogs.isEmpty)
|
||||||
@ -131,39 +185,28 @@ class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
|||||||
MyCard.bordered(
|
MyCard.bordered(
|
||||||
paddingAll: 8,
|
paddingAll: 8,
|
||||||
child: Column(
|
child: Column(
|
||||||
children: List.generate(filteredLogs.length, (index) {
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
final employee = filteredLogs[index];
|
children: [
|
||||||
final currentDate = employee.checkIn != null
|
for (final date in sortedDates) ...[
|
||||||
? DateTimeUtils.formatDate(
|
Padding(
|
||||||
employee.checkIn!, 'dd MMM yyyy')
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||||
: '';
|
child: MyText.bodyMedium(date, fontWeight: 700),
|
||||||
final previousDate =
|
),
|
||||||
index > 0 && filteredLogs[index - 1].checkIn != null
|
|
||||||
? DateTimeUtils.formatDate(
|
|
||||||
filteredLogs[index - 1].checkIn!, 'dd MMM yyyy')
|
|
||||||
: '';
|
|
||||||
final showDateHeader =
|
|
||||||
index == 0 || currentDate != previousDate;
|
|
||||||
|
|
||||||
return Column(
|
// Sort employees inside this date by action priority
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
for (final emp in (groupedLogs[date]!
|
||||||
children: [
|
..sort(
|
||||||
if (showDateHeader)
|
(a, b) => _getActionPriority(a)
|
||||||
Padding(
|
.compareTo(_getActionPriority(b)),
|
||||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
))) ...[
|
||||||
child: MyText.bodyMedium(
|
|
||||||
currentDate,
|
|
||||||
fontWeight: 700,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
MyContainer(
|
MyContainer(
|
||||||
paddingAll: 8,
|
paddingAll: 8,
|
||||||
child: Row(
|
child: Row(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Avatar(
|
Avatar(
|
||||||
firstName: employee.firstName,
|
firstName: emp.firstName,
|
||||||
lastName: employee.lastName,
|
lastName: emp.lastName,
|
||||||
size: 31,
|
size: 31,
|
||||||
),
|
),
|
||||||
MySpacing.width(16),
|
MySpacing.width(16),
|
||||||
@ -175,7 +218,7 @@ class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
|||||||
children: [
|
children: [
|
||||||
Flexible(
|
Flexible(
|
||||||
child: MyText.bodyMedium(
|
child: MyText.bodyMedium(
|
||||||
employee.name,
|
emp.name,
|
||||||
fontWeight: 600,
|
fontWeight: 600,
|
||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
),
|
),
|
||||||
@ -183,7 +226,7 @@ class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
|||||||
MySpacing.width(6),
|
MySpacing.width(6),
|
||||||
Flexible(
|
Flexible(
|
||||||
child: MyText.bodySmall(
|
child: MyText.bodySmall(
|
||||||
'(${employee.designation})',
|
'(${emp.designation})',
|
||||||
fontWeight: 600,
|
fontWeight: 600,
|
||||||
color: Colors.grey[700],
|
color: Colors.grey[700],
|
||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
@ -192,28 +235,28 @@ class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
MySpacing.height(8),
|
MySpacing.height(8),
|
||||||
if (employee.checkIn != null ||
|
if (emp.checkIn != null ||
|
||||||
employee.checkOut != null)
|
emp.checkOut != null)
|
||||||
Row(
|
Row(
|
||||||
children: [
|
children: [
|
||||||
if (employee.checkIn != null) ...[
|
if (emp.checkIn != null) ...[
|
||||||
const Icon(Icons.arrow_circle_right,
|
const Icon(Icons.arrow_circle_right,
|
||||||
size: 16, color: Colors.green),
|
size: 16, color: Colors.green),
|
||||||
MySpacing.width(4),
|
MySpacing.width(4),
|
||||||
MyText.bodySmall(
|
MyText.bodySmall(
|
||||||
DateTimeUtils.formatDate(
|
DateTimeUtils.formatDate(
|
||||||
employee.checkIn!, 'hh:mm a'),
|
emp.checkIn!, 'hh:mm a'),
|
||||||
fontWeight: 600,
|
fontWeight: 600,
|
||||||
),
|
),
|
||||||
MySpacing.width(16),
|
MySpacing.width(16),
|
||||||
],
|
],
|
||||||
if (employee.checkOut != null) ...[
|
if (emp.checkOut != null) ...[
|
||||||
const Icon(Icons.arrow_circle_left,
|
const Icon(Icons.arrow_circle_left,
|
||||||
size: 16, color: Colors.red),
|
size: 16, color: Colors.red),
|
||||||
MySpacing.width(4),
|
MySpacing.width(4),
|
||||||
MyText.bodySmall(
|
MyText.bodySmall(
|
||||||
DateTimeUtils.formatDate(
|
DateTimeUtils.formatDate(
|
||||||
employee.checkOut!, 'hh:mm a'),
|
emp.checkOut!, 'hh:mm a'),
|
||||||
fontWeight: 600,
|
fontWeight: 600,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@ -224,12 +267,12 @@ class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
|||||||
mainAxisAlignment: MainAxisAlignment.end,
|
mainAxisAlignment: MainAxisAlignment.end,
|
||||||
children: [
|
children: [
|
||||||
AttendanceActionButton(
|
AttendanceActionButton(
|
||||||
employee: employee,
|
employee: emp,
|
||||||
attendanceController: widget.controller,
|
attendanceController: widget.controller,
|
||||||
),
|
),
|
||||||
MySpacing.width(8),
|
MySpacing.width(8),
|
||||||
AttendanceLogViewButton(
|
AttendanceLogViewButton(
|
||||||
employee: employee,
|
employee: emp,
|
||||||
attendanceController: widget.controller,
|
attendanceController: widget.controller,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@ -240,11 +283,10 @@ class _AttendanceLogsTabState extends State<AttendanceLogsTab> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (index != filteredLogs.length - 1)
|
Divider(color: Colors.grey.withOpacity(0.3)),
|
||||||
Divider(color: Colors.grey.withOpacity(0.3)),
|
|
||||||
],
|
],
|
||||||
);
|
],
|
||||||
}),
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user