Fix all server-client issues

This commit is contained in:
Levi Lesches 2025-05-04 21:14:06 -04:00
parent 1c5c6c9f11
commit 6d3011b706
5 changed files with 64 additions and 36 deletions

View file

@ -1,7 +1,6 @@
import "package:flutter/foundation.dart" show debugPrint, kDebugMode;
import "package:client/data.dart";
import "package:http/http.dart" as http;
import "package:shared/graph.dart";
import "api_client.dart";

View file

@ -5,6 +5,7 @@ import "package:client/services.dart";
import "package:flutter/widgets.dart" hide Path;
import "package:google_maps_flutter/google_maps_flutter.dart";
import "package:shared/graph.dart";
import "home_markers.dart";
import "view_model.dart";
@ -66,17 +67,76 @@ class HomeModel extends ViewModel with HomeMarkers {
}
String? pathText;
List<Coordinates> pathWaypoint = [];
List<List<Coordinates>> pathWalking = [];
List<List<Coordinates>> pathStops = [];
void explainPath(Iterable<StopState> path, void Function(String) printer) {
var index = 0;
for (final step in path) {
index++;
final stop = stops[step.stopID]!;
if (index == path.length) {
printer("$index. Get off at ${stop.name}");
} else {
printer("$index. ${explainStep(step)}");
}
}
}
String explainStep(StopState step) {
final route = routes[step.routeID]!;
final stop = stops[step.stopID]!;
return switch (step.method) {
SearchMethod.bus => "Go one stop to $stop",
SearchMethod.start => "Walk to $stop\n and board the $route line",
SearchMethod.transfer => "Transfer to the $route line",
SearchMethod.walk => "Walk to $stop\n and board the $route line"
};
}
Future<void> search() async {
final start = (lat: startLatitude, long: startLongitude);
final end = (lat: endLatitude, long: endLongitude);
pathText = null;
if (start.lat == null || start.long == null) return;
if (end.lat == null || end.long == null) return;
isSearching = true;
isLoading = true;
final result = await services.api.getPath(start: start as Coordinates, end: end as Coordinates);
pathText = result ?? "An error occurred";
final path = await services.api.getPath(start: start as Coordinates, end: end as Coordinates);
isLoading = false;
if (result == null) return;
if (path == null) {
errorText = "An error occcurred";
return;
}
pathWalking.clear();
pathWaypoint.clear();
pathStops.clear();
for (final (index, step) in path.enumerate) {
final stop = stops[step.stopID]!;
final position = stop.coordinates;
switch (step.method) {
case SearchMethod.start:
pathWalking.add([start, position]);
pathWaypoint.add(position);
pathStops.add([position]);
case SearchMethod.bus:
pathStops.last.add(position);
case SearchMethod.transfer:
pathWaypoint.add(position);
pathStops.add([]);
case SearchMethod.walk:
final prevStep = path[index - 1];
final prevStopID = prevStep.stopID;
final prevStop = stops[prevStopID]!;
pathWalking.add([prevStop.coordinates, position]);
pathWaypoint.add(stop.coordinates);
pathStops.add([position]);
}
}
final buffer = StringBuffer();
explainPath(path, buffer.writeln);
pathText = buffer.toString();
isSearching = false;
notifyListeners();
}

View file

@ -51,15 +51,8 @@ mixin HomeMarkers on ChangeNotifier {
("BC Transit", bcRouteNames),
];
int _parseBcNumber(String routeName) {
// eg, "53)" --> 53
final first = routeName.split(" ").first;
final withoutParen = first.substring(0, first.length - 1);
return int.parse(withoutParen);
}
int compareBcRoutes(Route a, Route b) =>
_parseBcNumber(a.shortName).compareTo(_parseBcNumber(b.shortName));
int.parse(a.shortName).compareTo(int.parse(b.shortName));
int compareOcctRoutes(Route a, Route b) =>
a.shortName.compareTo(b.shortName);

View file

@ -45,16 +45,3 @@ Iterable<StopState>? findPath(Coordinates start, Coordinates end) {
double getTotalDistance(Iterable<StopState> path) =>
path.sum((step) => step.distanceWalked);
void explainPath(Iterable<StopState> path, void Function(String) printer) {
var index = 0;
for (final step in path) {
index++;
final stop = StopState.stops[step.stopID]!;
if (index == path.length) {
printer("$index. Get off at ${stop.name}");
} else {
printer("$index. ${step.explanation}");
}
}
}

View file

@ -157,17 +157,6 @@ class StopState extends AStarState<StopState> with Encodable {
return result;
}
String get explanation {
final route = routes[routeID]!;
final stop = stops[stopID]!;
return switch (method) {
SearchMethod.bus => "Go one stop to $stop",
SearchMethod.start => "Walk to $stop\n and board the $route line",
SearchMethod.transfer => "Transfer to the $route line",
SearchMethod.walk => "Walk to $stop\n and board the $route line"
};
}
@override
Json toJson() => {
"stop_id": stopID,