Fix all server-client issues
This commit is contained in:
parent
1c5c6c9f11
commit
6d3011b706
5 changed files with 64 additions and 36 deletions
|
@ -1,7 +1,6 @@
|
||||||
import "package:flutter/foundation.dart" show debugPrint, kDebugMode;
|
import "package:flutter/foundation.dart" show debugPrint, kDebugMode;
|
||||||
|
|
||||||
import "package:client/data.dart";
|
import "package:client/data.dart";
|
||||||
import "package:http/http.dart" as http;
|
|
||||||
import "package:shared/graph.dart";
|
import "package:shared/graph.dart";
|
||||||
|
|
||||||
import "api_client.dart";
|
import "api_client.dart";
|
||||||
|
|
|
@ -5,6 +5,7 @@ import "package:client/services.dart";
|
||||||
import "package:flutter/widgets.dart" hide Path;
|
import "package:flutter/widgets.dart" hide Path;
|
||||||
|
|
||||||
import "package:google_maps_flutter/google_maps_flutter.dart";
|
import "package:google_maps_flutter/google_maps_flutter.dart";
|
||||||
|
import "package:shared/graph.dart";
|
||||||
|
|
||||||
import "home_markers.dart";
|
import "home_markers.dart";
|
||||||
import "view_model.dart";
|
import "view_model.dart";
|
||||||
|
@ -66,17 +67,76 @@ class HomeModel extends ViewModel with HomeMarkers {
|
||||||
}
|
}
|
||||||
|
|
||||||
String? pathText;
|
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 {
|
Future<void> search() async {
|
||||||
final start = (lat: startLatitude, long: startLongitude);
|
final start = (lat: startLatitude, long: startLongitude);
|
||||||
final end = (lat: endLatitude, long: endLongitude);
|
final end = (lat: endLatitude, long: endLongitude);
|
||||||
|
pathText = null;
|
||||||
if (start.lat == null || start.long == null) return;
|
if (start.lat == null || start.long == null) return;
|
||||||
if (end.lat == null || end.long == null) return;
|
if (end.lat == null || end.long == null) return;
|
||||||
isSearching = true;
|
isSearching = true;
|
||||||
isLoading = true;
|
isLoading = true;
|
||||||
final result = await services.api.getPath(start: start as Coordinates, end: end as Coordinates);
|
final path = await services.api.getPath(start: start as Coordinates, end: end as Coordinates);
|
||||||
pathText = result ?? "An error occurred";
|
|
||||||
isLoading = false;
|
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;
|
isSearching = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,15 +51,8 @@ mixin HomeMarkers on ChangeNotifier {
|
||||||
("BC Transit", bcRouteNames),
|
("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) =>
|
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) =>
|
int compareOcctRoutes(Route a, Route b) =>
|
||||||
a.shortName.compareTo(b.shortName);
|
a.shortName.compareTo(b.shortName);
|
||||||
|
|
|
@ -45,16 +45,3 @@ Iterable<StopState>? findPath(Coordinates start, Coordinates end) {
|
||||||
|
|
||||||
double getTotalDistance(Iterable<StopState> path) =>
|
double getTotalDistance(Iterable<StopState> path) =>
|
||||||
path.sum((step) => step.distanceWalked);
|
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}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -157,17 +157,6 @@ class StopState extends AStarState<StopState> with Encodable {
|
||||||
return result;
|
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
|
@override
|
||||||
Json toJson() => {
|
Json toJson() => {
|
||||||
"stop_id": stopID,
|
"stop_id": stopID,
|
||||||
|
|
Loading…
Reference in a new issue