-
Notifications
You must be signed in to change notification settings - Fork 35
Description
I have such code that reacts when the app goes to the background and when the scenePhase becomes active again, if I use the syntax
navigator.send(values: [Route1, Route2]), then for some reason only the first one will open, and if navigator.send(Route1, Route2) then everything will be okay, why?
import SwiftUI
import NavigatorUI
// MARK: - Routes
enum TopUpRoutes {
case first
case second(params: SecondScreen.Params)
case third(params: ThirdScreen.Params)
}
extension TopUpRoutes: NavigationDestination {
var body: some View {
switch self {
case .first:
FirstScreen()
case .second(let params):
SecondScreen(params: params)
case .third(let params):
ThirdScreen(params: params)
}
}
var method: NavigationMethod { .push }
}
// MARK: - Root with ManagedNavigationStack + onNavigationReceive
struct ContentView: View {
var body: some View {
ManagedNavigationStack {
FirstScreen()
.navigationDestination(TopUpRoutes.self)
.onNavigationReceive { (destination: TopUpRoutes, navigator) in
navigator.navigate(to: destination)
return .auto
}
}
}
}
// MARK: - Screens
struct FirstScreen: View {
@Environment(.scenePhase) private var scenePhase
@Environment(.navigator) private var navigator
@State private var didHandleFirstActive = false
var body: some View {
VStack(spacing: 16) {
Text("First").font(.largeTitle)
Button("Send Second -> Third") {
navigator.send(
TopUpRoutes.second(params: .init(title: "from button")),
TopUpRoutes.third(params: .init(id: 1))
)
}
}
.padding()
.onChange(of: scenePhase, initial: false, { oldValue, newValue in
guard newValue == .active else { return }
guard didHandleFirstActive else {
didHandleFirstActive = true
return
}
navigator.send(
values: [
TopUpRoutes.second(params: .init(title: "from scenePhase")),
TopUpRoutes.third(params: .init(id: 999))
]
)
})
}
}
struct SecondScreen: View {
struct Params: Hashable { let title: String }
let params: Params
var body: some View {
VStack(spacing: 12) {
Text("Second").font(.largeTitle)
Text("title: \(params.title)")
}
.padding()
}
}
struct ThirdScreen: View {
struct Params: Hashable { let id: Int }
let params: Params
var body: some View {
VStack(spacing: 12) {
Text("Third").font(.largeTitle)
Text("id: \(params.id)")
}
.padding()
}
}
#Preview {
ContentView()
}