From 6d3011b706f6af3fc5ab3265bb0da900af2e1619 Mon Sep 17 00:00:00 2001 From: Levi Lesches Date: Sun, 4 May 2025 21:14:06 -0400 Subject: [PATCH] Fix all server-client issues --- src/client/lib/src/services/api.dart | 1 - src/client/lib/src/view_models/home.dart | 66 ++++++++++++++++++- .../lib/src/view_models/home_markers.dart | 9 +-- src/shared/lib/src/graph/algorithm.dart | 13 ---- src/shared/lib/src/graph/state.dart | 11 ---- 5 files changed, 64 insertions(+), 36 deletions(-) diff --git a/src/client/lib/src/services/api.dart b/src/client/lib/src/services/api.dart index 2909e77..bb980c4 100644 --- a/src/client/lib/src/services/api.dart +++ b/src/client/lib/src/services/api.dart @@ -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"; diff --git a/src/client/lib/src/view_models/home.dart b/src/client/lib/src/view_models/home.dart index 388d150..cd668ae 100644 --- a/src/client/lib/src/view_models/home.dart +++ b/src/client/lib/src/view_models/home.dart @@ -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 pathWaypoint = []; + List> pathWalking = []; + List> pathStops = []; + + void explainPath(Iterable 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 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(); } diff --git a/src/client/lib/src/view_models/home_markers.dart b/src/client/lib/src/view_models/home_markers.dart index 8f5585c..6b9fa12 100644 --- a/src/client/lib/src/view_models/home_markers.dart +++ b/src/client/lib/src/view_models/home_markers.dart @@ -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); diff --git a/src/shared/lib/src/graph/algorithm.dart b/src/shared/lib/src/graph/algorithm.dart index e65434c..741f854 100644 --- a/src/shared/lib/src/graph/algorithm.dart +++ b/src/shared/lib/src/graph/algorithm.dart @@ -45,16 +45,3 @@ Iterable? findPath(Coordinates start, Coordinates end) { double getTotalDistance(Iterable path) => path.sum((step) => step.distanceWalked); - -void explainPath(Iterable 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}"); - } - } -} diff --git a/src/shared/lib/src/graph/state.dart b/src/shared/lib/src/graph/state.dart index 0998ca4..5432f3c 100644 --- a/src/shared/lib/src/graph/state.dart +++ b/src/shared/lib/src/graph/state.dart @@ -157,17 +157,6 @@ class StopState extends AStarState 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,