Why this happening, and any solution would be appreciate. I'm using Coordinator pattern where all views pushed in a central place.
Coordinator
class Coordinator: ObservableObject {
@Published var path = NavigationPath()
func push(_ page: Page) {path.append(page) }
func pop() {
guard !path.isEmpty else {return}
path.removeLast()
}
func popToRoot() {
guard !path.isEmpty else {return}
path.removeLast(path.count)
}
}
extension Coordinator {
@ViewBuilder
func build(page: Page) -> some View {
switch page {
case .tenantInfo:
IntroView()
case .home:
Home()
}
}
}
CoordinatorView
struct CoordinatorView: View {
@StateObject private var coordinator = Coordinator()
@State var isLoggedIn = false
var body: some View {
NavigationStack(path: $coordinator.path) {
Group {
if isLoggedIn {
coordinator.build(page: .home)
} else {
coordinator.build(page: .tenantInfo)
}
}
.navigationBarTitleDisplayMode(.inline)
.navigationDestination(for: Page.self) { page in
coordinator.build(page: page)
}
}
.environmentObject(coordinator)
}
}
Root ContentView
struct ContentView: App {
var body: some Scene {
WindowGroup {
CoordinatorView()
}
}
}
Page
enum Page {
case tenantInfo
case home
}
extension Page: Identifiable {
var id: Self { self }
}
extension Page: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(self.hashValue)
}
static func == (lhs: Page, rhs: Page) -> Bool {
switch (lhs, rhs) {
case (.tenantInfo, .tenantInfo),
(.home, .home):
return true
default:
return false
}
}
}
struct IntroView: View {
var body: some View {
Text("Intro View")
}
}
struct Home: View {
var body: some View {
Text("Home View")
}
}
this is my app base structure for navigation. if I push one view, using coordinator.push() all the views in navigation path rebuild.