import SwiftUI
import SwiftData
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query private var folders: [Folder]
@State private var currentFolder: Folder?
@State private var currentPath: [Item] = []
var body: some View {
NavigationSplitView {
List(selection: $currentFolder) {
ForEach(folders) { folder in
NavigationLink(value: folder) {
Text(folder.name)
}
}
}
} detail: {
NavigationStack(path: $currentPath) {
if let currentFolder = currentFolder {
List(currentFolder.items) { item in
NavigationLink(value: item) {
Text(item.timestamp.ISO8601Format())
}
}
.navigationDestination(for: Item.self) { item in
Text(item.title)
.background(Color.blue)
}
}
}
}.onOpenURL(perform: { url in // Triggers by clicking on a widget
let descriptor = FetchDescriptor<Folder>()
// Randomly selecting a folder
// In real app the url contains the folder and the item
let folder = try? modelContext.fetch(descriptor).last
if let folder = folder {
currentFolder = folder // Triggering NavigationSplitView navigation works
currentPath = [folder.items.last!] // Triggering NavigationStack navigation doesn't work
}
})
}
private func addFolder() {
// ..
}
}
How to make programmatic navigation work?