Skip to content

Instantly share code, notes, and snippets.

@phillipcaudell
Created April 18, 2024 13:22
Show Gist options
  • Save phillipcaudell/fda686d67bde2f87a9252fbd802fcc3a to your computer and use it in GitHub Desktop.
Save phillipcaudell/fda686d67bde2f87a9252fbd802fcc3a to your computer and use it in GitHub Desktop.
import SwiftUI
// Wrap PathOptions with something that can differentiate between screens
struct PathStep: Hashable {
var level: Int
var option: PathOptions
}
enum PathOptions: String {
case first
case second
case third
case fourth
case fifth
case pathless
}
struct LinkList: View {
var step: PathStep? = nil
@State private var selection = Set<PathStep?>()
var body: some View {
List(selection: $selection) {
// Differntiate the link value from the one that brought us here
NavigationLink("First", value: PathOptions.first.nextStep(step))
NavigationLink("Second", value: PathOptions.second.nextStep(step))
NavigationLink("Third", value: PathOptions.third.nextStep(step))
NavigationLink("Fourth", value: PathOptions.fourth.nextStep(step))
NavigationLink("Fifth", value: PathOptions.fifth.nextStep(step))
}
.navigationTitle("Screen \(step?.level ?? 0)")
}
}
extension PathOptions {
func nextStep(_ step: PathStep?) -> PathStep {
PathStep(level: (step?.level ?? 0) + 1, option: self)
}
}
struct ContentView: View {
var body: some View {
NavigationStack {
LinkList()
.navigationDestination(for: PathStep.self) { step in
LinkList(step: step)
}
}
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment