support new fields in adding
All checks were successful
All checks were successful
This commit is contained in:
@@ -4,16 +4,25 @@ extension DataServiceTrips on DataService {
|
||||
Future<void> fetchTripDetails() async {
|
||||
_isTripDetailsLoading = true;
|
||||
try {
|
||||
final json = await api.get('/trips/legs-and-stats');
|
||||
if (json is List) {
|
||||
final tripMap = json.map((e) => TripDetail.fromJson(e)).toList();
|
||||
_tripDetails = [...tripMap]..sort((a, b) => b.id.compareTo(a.id));
|
||||
} else {
|
||||
_tripDetails = [];
|
||||
}
|
||||
final json = await api.get('/trips/info');
|
||||
final tripDetails = _parseTripInfoList(json);
|
||||
_tripDetails = [...tripDetails]..sort((a, b) => b.id.compareTo(a.id));
|
||||
_tripList = tripDetails
|
||||
.map(
|
||||
(detail) => TripSummary(
|
||||
tripId: detail.id,
|
||||
tripName: detail.name,
|
||||
tripMileage: detail.mileage,
|
||||
legCount: detail.legCount,
|
||||
locoStats: detail.locoStats,
|
||||
),
|
||||
)
|
||||
.toList()
|
||||
..sort((a, b) => b.tripId.compareTo(a.tripId));
|
||||
} catch (e) {
|
||||
debugPrint('Failed to fetch trip_map: $e');
|
||||
_tripDetails = [];
|
||||
_tripList = [];
|
||||
} finally {
|
||||
_isTripDetailsLoading = false;
|
||||
_notifyAsync();
|
||||
@@ -23,48 +32,17 @@ extension DataServiceTrips on DataService {
|
||||
Future<List<TripLocoStat>> fetchTripLocoStats(int tripId) async {
|
||||
try {
|
||||
final json = await api.get('/trips/stats/$tripId');
|
||||
return _parseTripLocoStats(json);
|
||||
return TripLocoStat.listFromJson(json);
|
||||
} catch (e) {
|
||||
debugPrint('Failed to fetch trip loco stats: $e');
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
List<TripLocoStat> _parseTripLocoStats(dynamic json) {
|
||||
List<dynamic>? list;
|
||||
if (json is List) {
|
||||
list = json.expand((e) => e is List ? e : [e]).toList();
|
||||
} else if (json is Map) {
|
||||
for (final key in ['locos', 'stats', 'data', 'trip_locos']) {
|
||||
final candidate = json[key];
|
||||
if (candidate is List) {
|
||||
list = candidate.expand((e) => e is List ? e : [e]).toList();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (list == null) return [];
|
||||
return list
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map((e) => TripLocoStat.fromJson(e))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<void> fetchTrips() async {
|
||||
try {
|
||||
final json = await api.get('/trips/mileage');
|
||||
Iterable<dynamic>? raw;
|
||||
if (json is List) {
|
||||
raw = json;
|
||||
} else if (json is Map) {
|
||||
for (final key in ['trips', 'trip_data', 'data']) {
|
||||
final value = json[key];
|
||||
if (value is List) {
|
||||
raw = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
final json = await api.get('/trips/info');
|
||||
final raw = _extractTrips(json);
|
||||
if (raw != null) {
|
||||
final tripMap = raw
|
||||
.whereType<Map<String, dynamic>>()
|
||||
@@ -119,8 +97,9 @@ extension DataServiceTrips on DataService {
|
||||
}
|
||||
|
||||
void upsertTripSummary(TripSummary trip) {
|
||||
final existingIndex =
|
||||
_tripList.indexWhere((element) => element.tripId == trip.tripId);
|
||||
final existingIndex = _tripList.indexWhere(
|
||||
(element) => element.tripId == trip.tripId,
|
||||
);
|
||||
if (existingIndex >= 0) {
|
||||
_tripList[existingIndex] = trip;
|
||||
} else {
|
||||
@@ -129,4 +108,24 @@ extension DataServiceTrips on DataService {
|
||||
_tripList.sort((a, b) => b.tripId.compareTo(a.tripId));
|
||||
_notifyAsync();
|
||||
}
|
||||
|
||||
Iterable<dynamic>? _extractTrips(dynamic json) {
|
||||
if (json is List) return json;
|
||||
if (json is Map) {
|
||||
for (final key in ['trips', 'trip_data', 'data', 'trip_info']) {
|
||||
final value = json[key];
|
||||
if (value is List) return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
List<TripDetail> _parseTripInfoList(dynamic json) {
|
||||
final raw = _extractTrips(json);
|
||||
if (raw == null) return const [];
|
||||
return raw
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map((e) => TripDetail.fromJson(e))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user