I have a NavigationStack
in SwiftUI and an ObservedObject
and enum that handle the routing.
enum Route {
case location(location: Location)
}
extension Route: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(self.hashValue)
}
static func == (lhs: Route, rhs: Route) -> Bool {
switch (lhs, rhs) {
case (.location(let lhsLocation), .location(let rhsLocation)):
return lhsLocation.uid == rhsLocation.uid
default:
return true
}
}
}
extension Route {
@ViewBuilder
func view(vm: LocationViewModel, router: NavigationRouter) -> some View {
switch self {
case .location(let location):
LocationView(location: location)
.environmentObject(vm)
.environmentObject(router)
}
}
}
final class NavigationRouter: ObservableObject {
@Published var routes = [Route]()
@Published var didPressBack = false
func push (to screen: Route) {
guard !routes.contains(screen) else {
return
}
routes.append(screen)
}
func reset() {
routes = []
}
func removeLast() {
print("Route count \(routes.count)")
_ = routes.popLast()
}
}
var body: some View {
NavigationStack(path: $navigationRouter.routes) {
VStack(alignment: .leading, spacing: 0) {
ScrollViewReader { value in
List {
LazyVStack(spacing: 0) {
ForEach(locations, id: \.uid) { location in
NavigationLink(value: Route.location(location: location)) {
DummyListItem()
}
}
}
}
}
.listStyle(PlainListStyle())
}
}
.navigationDestination(for: Route.self) {
$0.view(vm: vm, router: navigationRouter)
}
}
I have three items in the List
and when I tap on a DummyListItem()
, all items in the list get added to the stack. Previously, I had them in a ScrollView
instead of a List
and only one item got appended, but now that I'm using List
they all are.
Any suggestions for why this change may be causing this?