From 0dfe7a4e6de57c55aaa841d43d01222fc70a950b Mon Sep 17 00:00:00 2001 From: Achintha Isuru Date: Sun, 1 Feb 2026 11:28:50 -0500 Subject: [PATCH] refactor: streamline date conversion logic in repository implementations --- .../get_started_background.dart | 1 - .../clock_in_repository_impl.dart | 38 +++++++++++----- .../payments_repository_impl.dart | 44 ++++++++++++------- 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/apps/mobile/packages/features/staff/authentication/lib/src/presentation/widgets/get_started_page/get_started_background.dart b/apps/mobile/packages/features/staff/authentication/lib/src/presentation/widgets/get_started_page/get_started_background.dart index 09836e61..a3c0bafa 100644 --- a/apps/mobile/packages/features/staff/authentication/lib/src/presentation/widgets/get_started_page/get_started_background.dart +++ b/apps/mobile/packages/features/staff/authentication/lib/src/presentation/widgets/get_started_page/get_started_background.dart @@ -7,7 +7,6 @@ class GetStartedBackground extends StatelessWidget { @override Widget build(BuildContext context) { return Container( - color: Colors.white, child: Column( children: [ const SizedBox(height: 32), diff --git a/apps/mobile/packages/features/staff/clock_in/lib/src/data/repositories_impl/clock_in_repository_impl.dart b/apps/mobile/packages/features/staff/clock_in/lib/src/data/repositories_impl/clock_in_repository_impl.dart index f23ca9dd..51b1b5ac 100644 --- a/apps/mobile/packages/features/staff/clock_in/lib/src/data/repositories_impl/clock_in_repository_impl.dart +++ b/apps/mobile/packages/features/staff/clock_in/lib/src/data/repositories_impl/clock_in_repository_impl.dart @@ -2,6 +2,7 @@ import 'package:firebase_auth/firebase_auth.dart' as firebase; import 'package:firebase_data_connect/firebase_data_connect.dart'; import 'package:krow_data_connect/krow_data_connect.dart' as dc; import 'package:krow_domain/krow_domain.dart'; +import 'package:krow_core/core.dart'; import '../../domain/repositories/clock_in_repository_interface.dart'; /// Implementation of [ClockInRepositoryInterface] using Firebase Data Connect. @@ -30,18 +31,35 @@ class ClockInRepositoryImpl implements ClockInRepositoryInterface { /// Helper to convert Data Connect Timestamp to DateTime DateTime? _toDateTime(dynamic t) { if (t == null) return null; - // Attempt to use toJson assuming it matches the generated code's expectation of String - try { - // If t has toDate (e.g. cloud_firestore), usage would be t.toDate() - // But here we rely on toJson or toString - return DateTime.tryParse(t.toJson() as String); - } catch (_) { + DateTime? dt; + if (t is DateTime) { + dt = t; + } else if (t is String) { + dt = DateTime.tryParse(t); + } else { try { - return DateTime.tryParse(t.toString()); - } catch (e) { - return null; - } + if (t is Timestamp) { + dt = t.toDateTime(); + } + } catch (_) {} + + try { + if (dt == null && t.runtimeType.toString().contains('Timestamp')) { + dt = (t as dynamic).toDate(); + } + } catch (_) {} + + try { + if (dt == null) { + dt = DateTime.tryParse(t.toString()); + } + } catch (_) {} } + + if (dt != null) { + return DateTimeUtils.toDeviceTime(dt); + } + return null; } /// Helper to create Timestamp from DateTime diff --git a/apps/mobile/packages/features/staff/payments/lib/src/data/repositories/payments_repository_impl.dart b/apps/mobile/packages/features/staff/payments/lib/src/data/repositories/payments_repository_impl.dart index a465db34..f1b4afc4 100644 --- a/apps/mobile/packages/features/staff/payments/lib/src/data/repositories/payments_repository_impl.dart +++ b/apps/mobile/packages/features/staff/payments/lib/src/data/repositories/payments_repository_impl.dart @@ -3,6 +3,7 @@ import 'package:krow_data_connect/krow_data_connect.dart' as dc; import 'package:krow_data_connect/src/session/staff_session_store.dart'; import 'package:krow_domain/krow_domain.dart'; import 'package:firebase_auth/firebase_auth.dart'; +import 'package:krow_core/core.dart'; import '../../domain/repositories/payments_repository.dart'; class PaymentsRepositoryImpl implements PaymentsRepository { @@ -46,25 +47,34 @@ class PaymentsRepositoryImpl implements PaymentsRepository { /// Helper to convert Data Connect Timestamp to DateTime DateTime? _toDateTime(dynamic t) { if (t == null) return null; - if (t is DateTime) return t; - if (t is String) return DateTime.tryParse(t); + DateTime? dt; + if (t is DateTime) { + dt = t; + } else if (t is String) { + dt = DateTime.tryParse(t); + } else { + try { + if (t is Timestamp) { + dt = t.toDateTime(); + } + } catch (_) {} - try { - if (t is Timestamp) { - return t.toDateTime(); - } - } catch (_) {} - - try { - if (t.runtimeType.toString().contains('Timestamp')) { - return (t as dynamic).toDate(); - } - } catch (_) {} - - try { - return DateTime.tryParse(t.toString()); - } catch (_) {} + try { + if (dt == null && t.runtimeType.toString().contains('Timestamp')) { + dt = (t as dynamic).toDate(); + } + } catch (_) {} + try { + if (dt == null) { + dt = DateTime.tryParse(t.toString()); + } + } catch (_) {} + } + + if (dt != null) { + return DateTimeUtils.toDeviceTime(dt); + } return null; }