refactor: streamline date conversion logic in repository implementations
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user