Skip to content

Instantly share code, notes, and snippets.

@phillipcaudell
Created June 3, 2023 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipcaudell/5871cc1690bbdbe40b3d01cbf8bf85ee to your computer and use it in GitHub Desktop.
Save phillipcaudell/5871cc1690bbdbe40b3d01cbf8bf85ee to your computer and use it in GitHub Desktop.
class NavigationState: ObservableObject {
@Published var path = [Route]()
}
enum Route: Hashable {
case home
case content
case details
}
struct AppView: View {
@StateObject private var state = NavigationState()
var body: some View {
NavigationStack(path: $state.path) {
Sidebar(state: state)
.navigationDestination(for: Route.self) { route in
RouteView(route: route, state: state)
}
}
}
}
struct RouteView: View {
let route: Route
@ObservedObject var state: NavigationState
var body: some View {
switch route {
case .home:
Sidebar(state: state)
case .content:
ContentView(state: state)
case .details:
DetailView()
}
}
}
struct Sidebar: View {
@ObservedObject var state: NavigationState
var body: some View {
List {
NavigationLink(value: Route.content) {
Text("Content")
}
}
}
}
struct ContentView: View {
@ObservedObject var state: NavigationState
var body: some View {
List {
ForEach(0...10, id: \.self) { id in
NavigationLink(value: Route.details) {
Text("Detail \(id)")
}
}
}
.navigationTitle("Content")
}
}
struct DetailView: View {
var body: some View {
Text("Detail")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment