refactor: streamline date conversion logic in repository implementations

This commit is contained in:
Achintha Isuru
2026-02-01 11:28:50 -05:00
parent 6441e72ae3
commit 0dfe7a4e6d
3 changed files with 55 additions and 28 deletions

View File

@@ -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),

View File

@@ -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

View File

@@ -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;
}