From 09f7f5d849333a40d4bcc718aa4f7d4a73410963 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Thu, 4 Apr 2024 15:55:44 -0600 Subject: [PATCH] feat: use managed navigation path (#113) * feat: use managed navigation path * fix test --- iosApp/iosApp.xcodeproj/project.pbxproj | 4 ++++ iosApp/iosApp/ContentView.swift | 10 ++++++++-- .../Pages/NearbyTransit/NearbyStopView.swift | 4 +--- .../iosApp/Pages/StopDetails/StopDetailsPage.swift | 4 ++-- .../iosApp/Utils/SheetNavigationStackEntry.swift | 14 ++++++++++++++ .../iosAppTests/Views/NearbyTransitViewTests.swift | 4 +--- 6 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 iosApp/iosApp/Utils/SheetNavigationStackEntry.swift diff --git a/iosApp/iosApp.xcodeproj/project.pbxproj b/iosApp/iosApp.xcodeproj/project.pbxproj index a99bd855e..ad4a7f540 100644 --- a/iosApp/iosApp.xcodeproj/project.pbxproj +++ b/iosApp/iosApp.xcodeproj/project.pbxproj @@ -35,6 +35,7 @@ 8C84D33E2B5AEE0200192C0A /* NearbyTransitView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C84D33D2B5AEE0200192C0A /* NearbyTransitView.swift */; }; 8CC1BB402B59D1F6005386FE /* LocationDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CC1BB3F2B59D1F6005386FE /* LocationDataManager.swift */; }; 8CD1F8CD2B7164C100F419D4 /* PredictionsFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CD1F8CC2B7164C100F419D4 /* PredictionsFetcher.swift */; }; + 8CE0141B2BBF059B00918FAE /* SheetNavigationStackEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CE0141A2BBF059A00918FAE /* SheetNavigationStackEntry.swift */; }; 8CEA10232BA0F3C6001C6EB9 /* ScheduleFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CEA10222BA0F3C6001C6EB9 /* ScheduleFetcher.swift */; }; 8CEA10252BA4B179001C6EB9 /* AlertsFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CEA10242BA4B179001C6EB9 /* AlertsFetcher.swift */; }; 8CEA10272BA4C83D001C6EB9 /* AlertsFetcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CEA10262BA4C83D001C6EB9 /* AlertsFetcherTests.swift */; }; @@ -158,6 +159,7 @@ 8C84D33D2B5AEE0200192C0A /* NearbyTransitView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NearbyTransitView.swift; sourceTree = ""; }; 8CC1BB3F2B59D1F6005386FE /* LocationDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationDataManager.swift; sourceTree = ""; }; 8CD1F8CC2B7164C100F419D4 /* PredictionsFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PredictionsFetcher.swift; sourceTree = ""; }; + 8CE0141A2BBF059A00918FAE /* SheetNavigationStackEntry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SheetNavigationStackEntry.swift; sourceTree = ""; }; 8CEA10222BA0F3C6001C6EB9 /* ScheduleFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScheduleFetcher.swift; sourceTree = ""; }; 8CEA10242BA4B179001C6EB9 /* AlertsFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertsFetcher.swift; sourceTree = ""; }; 8CEA10262BA4C83D001C6EB9 /* AlertsFetcherTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertsFetcherTests.swift; sourceTree = ""; }; @@ -509,6 +511,7 @@ 9A37F3062BACCCA5001714FE /* CoordinateExtension.swift */, 9ADB849C2BAD05BC006581CE /* Inspection.swift */, 9ADB84A12BAE37C0006581CE /* StopExtension.swift */, + 8CE0141A2BBF059A00918FAE /* SheetNavigationStackEntry.swift */, ); path = Utils; sourceTree = ""; @@ -860,6 +863,7 @@ 9A2005C92B97B65900F562E1 /* NearbyStopRoutePatternView.swift in Sources */, 9A4E8E592B7EC4B90066B936 /* RoutePill.swift in Sources */, 9A5B27562BB221C1009A6FC6 /* RouteLayerGenerator.swift in Sources */, + 8CE0141B2BBF059B00918FAE /* SheetNavigationStackEntry.swift in Sources */, 8C5054582BB5EB6C00C6A51C /* StopDetailsPage.swift in Sources */, 9A9E05F42B6D6DEA0086B437 /* SearchResultFetcher.swift in Sources */, ED3581682BB470C4005DDC34 /* PartialSheetDetent.swift in Sources */, diff --git a/iosApp/iosApp/ContentView.swift b/iosApp/iosApp/ContentView.swift index c48ead27c..96275c5da 100644 --- a/iosApp/iosApp/ContentView.swift +++ b/iosApp/iosApp/ContentView.swift @@ -19,6 +19,7 @@ struct ContentView: View { @EnvironmentObject var socketProvider: SocketProvider @EnvironmentObject var viewportProvider: ViewportProvider @State private var sheetHeight: CGFloat = .zero + @State private var navigationStack: [SheetNavigationStackEntry] = [] private var sheetDetents: Set { if #available(iOS 16, *) { @@ -57,11 +58,16 @@ struct ContentView: View { ) .ignoresSafeArea(edges: .bottom) .sheet(isPresented: .constant(true)) { - NavigationStack { + NavigationStack(path: $navigationStack) { nearbyTransit .navigationBarHidden(true) + .navigationDestination(for: SheetNavigationStackEntry.self) { entry in + switch entry { + case let .stopDetails(stop, route): + StopDetailsPage(stop: stop, route: route) + } + } } - .navigationViewStyle(.stack) .partialSheetDetents( sheetDetents, largestUndimmedDetent: .medium diff --git a/iosApp/iosApp/Pages/NearbyTransit/NearbyStopView.swift b/iosApp/iosApp/Pages/NearbyTransit/NearbyStopView.swift index faf702942..9ff09957f 100644 --- a/iosApp/iosApp/Pages/NearbyTransit/NearbyStopView.swift +++ b/iosApp/iosApp/Pages/NearbyTransit/NearbyStopView.swift @@ -15,9 +15,7 @@ struct NearbyStopView: View { var body: some View { VStack(alignment: .leading) { - NavigationLink { - StopDetailsPage(stop: patternsAtStop.stop, route: patternsAtStop.route) - } label: { + NavigationLink(value: SheetNavigationStackEntry.stopDetails(patternsAtStop.stop, patternsAtStop.route)) { Text(patternsAtStop.stop.name).fontWeight(.bold) } diff --git a/iosApp/iosApp/Pages/StopDetails/StopDetailsPage.swift b/iosApp/iosApp/Pages/StopDetails/StopDetailsPage.swift index b57a35d17..b72d87566 100644 --- a/iosApp/iosApp/Pages/StopDetails/StopDetailsPage.swift +++ b/iosApp/iosApp/Pages/StopDetails/StopDetailsPage.swift @@ -12,12 +12,12 @@ import SwiftUI struct StopDetailsPage: View { var stop: Stop - var route: Route + var route: Route? var body: some View { Text("Stop: \(stop.name)") .navigationTitle("Stop Details") - Text("Route: \(route.longName)") + Text("Route: \(route?.longName ?? "-")") } } diff --git a/iosApp/iosApp/Utils/SheetNavigationStackEntry.swift b/iosApp/iosApp/Utils/SheetNavigationStackEntry.swift new file mode 100644 index 000000000..0bee1a01e --- /dev/null +++ b/iosApp/iosApp/Utils/SheetNavigationStackEntry.swift @@ -0,0 +1,14 @@ +// +// SheetNavigationStackEntry.swift +// iosApp +// +// Created by Horn, Melody on 2024-04-04. +// Copyright © 2024 MBTA. All rights reserved. +// + +import Foundation +import shared + +enum SheetNavigationStackEntry: Hashable { + case stopDetails(Stop, Route?) +} diff --git a/iosApp/iosAppTests/Views/NearbyTransitViewTests.swift b/iosApp/iosAppTests/Views/NearbyTransitViewTests.swift index ef4a6ed3e..6a7110b08 100644 --- a/iosApp/iosAppTests/Views/NearbyTransitViewTests.swift +++ b/iosApp/iosAppTests/Views/NearbyTransitViewTests.swift @@ -748,8 +748,6 @@ final class NearbyTransitViewTests: XCTestCase { patternsByHeadsign: [PatternsByHeadsign(route: route, headsign: "Place", patterns: [], upcomingTrips: nil, alertsHere: nil)] ), now: Date.now.toKotlinInstant()) - let stopDetailsPage = try sut.inspect().find(navigationLink: "This Stop").view(StopDetailsPage.self).actualView() - XCTAssertEqual(stopDetailsPage.stop, stop) - XCTAssertEqual(stopDetailsPage.route, route) + XCTAssertEqual(try sut.inspect().find(navigationLink: "This Stop").value(SheetNavigationStackEntry.self), .stopDetails(stop, route)) } }