Layout changes, fix bugs in new entry page

This commit is contained in:
2025-12-22 17:23:21 +00:00
parent 63b545c7a3
commit 45d543498f
20 changed files with 779 additions and 192 deletions

View File

@@ -44,6 +44,10 @@ class DataService extends ChangeNotifier {
bool get isTractionLoading => _isTractionLoading;
bool _tractionHasMore = false;
bool get tractionHasMore => _tractionHasMore;
List<LocoChange> _latestLocoChanges = [];
List<LocoChange> get latestLocoChanges => _latestLocoChanges;
bool _isLatestLocoChangesLoading = false;
bool get isLatestLocoChangesLoading => _isLatestLocoChangesLoading;
final Map<int, List<LocoAttrVersion>> _locoTimelines = {};
final Map<int, bool> _isLocoTimelineLoading = {};
List<LocoAttrVersion> timelineForLoco(int locoId) =>
@@ -148,7 +152,8 @@ class DataService extends ChangeNotifier {
if (json is List) {
final newLegs = json.map((e) => Leg.fromJson(e)).toList();
_legs = append ? [..._legs, ...newLegs] : newLegs;
_legsHasMore = newLegs.length >= limit;
// Keep "load more" available as long as the server returns items; hide only on empty.
_legsHasMore = newLegs.isNotEmpty;
} else {
throw Exception('Unexpected legs response: $json');
}
@@ -180,7 +185,7 @@ class DataService extends ChangeNotifier {
final params =
includeNonPowering ? '?include_non_powering=true' : '';
try {
final json = await api.get('/legs/$locoId$params');
final json = await api.get('/legs/by-loco/$locoId$params');
dynamic list = json;
if (json is Map) {
for (final key in ['legs', 'data', 'results']) {
@@ -340,6 +345,8 @@ class DataService extends ChangeNotifier {
_eventFields = [];
_locoTimelines.clear();
_isLocoTimelineLoading.clear();
_latestLocoChanges = [];
_isLatestLocoChangesLoading = false;
_notifyAsync();
}

View File

@@ -114,5 +114,40 @@ extension DataServiceTraction on DataService {
}
return _locoClasses;
}
}
Future<void> fetchLatestLocoChanges({int limit = 25, int offset = 0}) async {
_isLatestLocoChangesLoading = true;
_notifyAsync();
try {
final json =
await api.get('/loco/changes/latest?limit=$limit&offset=$offset');
dynamic results = json;
if (json is Map && json['data'] is List) {
results = json['data'];
}
if (results is List) {
final parsed = <LocoChange>[];
for (final item in results) {
if (item is Map<String, dynamic>) {
parsed.add(LocoChange.fromJson(item));
} else if (item is Map) {
parsed.add(
LocoChange.fromJson(
item.map((key, value) => MapEntry(key.toString(), value)),
),
);
}
}
_latestLocoChanges = parsed;
} else {
throw Exception('Unexpected latest loco changes response: $json');
}
} catch (e) {
debugPrint('Failed to fetch latest loco changes: $e');
_latestLocoChanges = [];
} finally {
_isLatestLocoChangesLoading = false;
_notifyAsync();
}
}
}

View File

@@ -83,5 +83,50 @@ extension DataServiceTrips on DataService {
_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((a, b) => b.tripId.compareTo(a.tripId));
} 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((a, b) => b.tripId.compareTo(a.tripId));
_notifyAsync();
}
}