diff --git a/NavigateTesting/NavigateTesting/ContentView.swift b/NavigateTesting/NavigateTesting/ContentView.swift index 5829573..11e56e9 100644 --- a/NavigateTesting/NavigateTesting/ContentView.swift +++ b/NavigateTesting/NavigateTesting/ContentView.swift @@ -43,66 +43,8 @@ struct ContentView: View { } } -extension View { - - /// SwiftUI navigation destination convenience - func myNavigtationDestinations() -> some View { - self.navigationDestination(for: MyDestination.self) { destination in - switch destination { - case .featureA: - FeatureAView() - case .featureB: - FeatureBView() - case .featureC: - FeatureCView() - case .featureD: - FeatureDView() - case .settings: - SettingsView() - case .profile: - ProfileView() - } - } - } - - /// All ModalDestinations wrapped in NavigationStack to support SwiftUI navigation and toolbar - func myModalDestinations() -> some View { - self.modalDestination(for: MyDestination.self) { destination in - switch destination { - case .featureA: - NavigationStack { - FeatureAView() - .myNavigtationDestinations() - } - case .featureB: - NavigationStack { - FeatureBView() - .myNavigtationDestinations() - } - case .featureC: - NavigationStack { - FeatureCView() - .myNavigtationDestinations() - } - case .featureD: - NavigationStack { - FeatureDView() - .myNavigtationDestinations() - } - case .settings: - NavigationStack { - SettingsView() - .myNavigtationDestinations() - } - case .profile: - NavigationStack { - ProfileView() - .myNavigtationDestinations() - } - } - } - } -} + + #Preview { ContentView() diff --git a/NavigateTesting/NavigateTesting/ResolvedDestinationView.swift b/NavigateTesting/NavigateTesting/ResolvedDestinationView.swift new file mode 100644 index 0000000..fa21098 --- /dev/null +++ b/NavigateTesting/NavigateTesting/ResolvedDestinationView.swift @@ -0,0 +1,50 @@ +// +// ResolvedDestinationView.swift +// NavigateTesting +// +// Created by Alexander Kauer on 25.08.25. +// + +import Navigate +import SwiftUI + +struct ResolvedDestinationView: View { + + let destination: MyDestination + + var body: some View { + switch destination { + case .featureA: + FeatureAView() + case .featureB: + FeatureBView() + case .featureC: + FeatureCView() + case .featureD: + FeatureDView() + case .settings: + SettingsView() + case .profile: + ProfileView() + } + } +} + +extension View { + /// SwiftUI navigation destination convenience + func myNavigtationDestinations() -> some View { + self.navigationDestination(for: MyDestination.self) { destination in + ResolvedDestinationView(destination: destination) + } + } + + /// All ModalDestinations wrapped in NavigationStack to support SwiftUI navigation and toolbar + func myModalDestinations() -> some View { + self.modalDestination(for: MyDestination.self) { destination in + NavigationStack { + ResolvedDestinationView(destination: destination) + .myNavigtationDestinations() + } + } + } +} diff --git a/README.md b/README.md index ef4b6ee..860f80e 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,10 @@ import Navigate enum MyDestination: NavigationDestination { case featureA case featureB + case featureC + case featureD case settings - case subSettings + case profile var id: Self { self } } @@ -87,43 +89,47 @@ var body: some View { #### Register destinations +For better code organization and maintainability, use a dedicated view to handle destination resolution: + ```swift import Navigate +import SwiftUI -// Import your corresponding views if needed -import FeatureHome -import FeatureCard -import FeatureSettings +struct ResolvedDestinationView: View { + let destination: MyDestination + + var body: some View { + switch destination { + case .featureA: + FeatureAView() + case .featureB: + FeatureBView() + case .featureC: + FeatureCView() + case .featureD: + FeatureDView() + case .settings: + SettingsView() + case .profile: + ProfileView() + } + } +} extension View { /// SwiftUI navigation destination convenience func myNavigtationDestinations() -> some View { self.navigationDestination(for: MyDestination.self) { destination in - switch destination { - case .featureA: - FeatureAView() - case .featureB: - FeatureBView() - ... - } + ResolvedDestinationView(destination: destination) } } /// All ModalDestinations wrapped in NavigationStack to support SwiftUI navigation and toolbar func myModalDestinations() -> some View { self.modalDestination(for: MyDestination.self) { destination in - switch destination { - case .featureA: - NavigationStack { - FeatureAView() - .myNavigtationDestinations() - } - case .featureB: - NavigationStack { - FeatureBView() - .myNavigtationDestinations() - } - ... + NavigationStack { + ResolvedDestinationView(destination: destination) + .myNavigtationDestinations() } } }