All checks were successful
Release / meta (push) Successful in 10s
Release / linux-build (push) Successful in 6m32s
Release / web-build (push) Successful in 5m50s
Release / android-build (push) Successful in 20m43s
Release / release-master (push) Successful in 26s
Release / release-dev (push) Successful in 28s
134 lines
3.6 KiB
Dart
134 lines
3.6 KiB
Dart
part of 'data_service.dart';
|
|
|
|
extension DataServiceTrips on DataService {
|
|
Future<void> fetchTripDetails() async {
|
|
_isTripDetailsLoading = true;
|
|
try {
|
|
final json = await api.get('/trips/info');
|
|
final tripDetails = _parseTripInfoList(json);
|
|
_tripDetails = [...tripDetails]..sort(TripDetail.compareByDateDesc);
|
|
_tripList = tripDetails
|
|
.map(
|
|
(detail) => TripSummary(
|
|
tripId: detail.id,
|
|
tripName: detail.name,
|
|
tripMileage: detail.mileage,
|
|
legCount: detail.legCount,
|
|
locoStats: detail.locoStats,
|
|
startDate: detail.startDate,
|
|
endDate: detail.endDate,
|
|
),
|
|
)
|
|
.toList()
|
|
..sort(TripSummary.compareByDateDesc);
|
|
} catch (e) {
|
|
debugPrint('Failed to fetch trip_map: $e');
|
|
_tripDetails = [];
|
|
_tripList = [];
|
|
} finally {
|
|
_isTripDetailsLoading = false;
|
|
_notifyAsync();
|
|
}
|
|
}
|
|
|
|
Future<List<TripLocoStat>> fetchTripLocoStats(int tripId) async {
|
|
try {
|
|
final json = await api.get('/trips/stats/$tripId');
|
|
return TripLocoStat.listFromJson(json);
|
|
} catch (e) {
|
|
debugPrint('Failed to fetch trip loco stats: $e');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<void> fetchTrips() async {
|
|
try {
|
|
final json = await api.get('/trips/info');
|
|
final raw = _extractTrips(json);
|
|
if (raw != null) {
|
|
final tripMap = raw
|
|
.whereType<Map<String, dynamic>>()
|
|
.map((e) => TripSummary.fromJson(e))
|
|
.toList();
|
|
|
|
_tripList = [...tripMap]..sort(TripSummary.compareByDateDesc);
|
|
} else {
|
|
debugPrint('Unexpected trip list response: $json');
|
|
_tripList = [];
|
|
}
|
|
} catch (e) {
|
|
debugPrint('Failed to fetch trip list: $e');
|
|
_tripList = [];
|
|
} finally {
|
|
_notifyAsync();
|
|
}
|
|
}
|
|
|
|
Future<void> fetchTripOptions() async {
|
|
try {
|
|
final json = await api.get('/trips');
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
if (raw != null) {
|
|
final tripMap = raw
|
|
.whereType<Map<String, dynamic>>()
|
|
.map((e) => TripSummary.fromJson(e))
|
|
.toList();
|
|
|
|
_tripList = [...tripMap]..sort(TripSummary.compareByDateDesc);
|
|
} else {
|
|
debugPrint('Unexpected trip list response: $json');
|
|
_tripList = [];
|
|
}
|
|
} catch (e) {
|
|
debugPrint('Failed to fetch trip list: $e');
|
|
_tripList = [];
|
|
} finally {
|
|
_notifyAsync();
|
|
}
|
|
}
|
|
|
|
void upsertTripSummary(TripSummary trip) {
|
|
final existingIndex = _tripList.indexWhere(
|
|
(element) => element.tripId == trip.tripId,
|
|
);
|
|
if (existingIndex >= 0) {
|
|
_tripList[existingIndex] = trip;
|
|
} else {
|
|
_tripList = [trip, ..._tripList];
|
|
}
|
|
_tripList.sort(TripSummary.compareByDateDesc);
|
|
_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();
|
|
}
|
|
}
|