Skip to content

Instantly share code, notes, and snippets.

@garrettmurray
Created February 18, 2024 00:56
Show Gist options
  • Save garrettmurray/4e23b2478f89beae47913a78d9c194a2 to your computer and use it in GitHub Desktop.
Save garrettmurray/4e23b2478f89beae47913a78d9c194a2 to your computer and use it in GitHub Desktop.
Example of odd behavior of SwiftDown inside a NavigationSplitView
import SwiftUI
import SwiftData
@main
@MainActor
struct TestingNavigationSplitViewSwiftDown: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([Item.self])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true)
do {
let container = try ModelContainer(for: schema, configurations: [modelConfiguration])
// Make sure the persistent store is empty. If it's not, return the non-empty container.
var itemFetchDescriptor = FetchDescriptor<Item>()
itemFetchDescriptor.fetchLimit = 1
guard try container.mainContext.fetch(itemFetchDescriptor).count == 0 else {
return container
}
// This code will only run if the persistent store is empty.
container.mainContext.insert(Item(timestamp: .now, text: "test 1"))
container.mainContext.insert(Item(timestamp: .now, text: "test 2"))
container.mainContext.insert(Item(timestamp: .now, text: "test 3"))
return container
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(sharedModelContainer)
}
}
import SwiftUI
import SwiftData
import SwiftDown
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query(sort: \Item.timestamp) private var items: [Item]
var body: some View {
NavigationSplitView {
List {
ForEach(items, id: \.self) { item in
NavigationLink(item.text) {
SomeEditor(item: item)
}
}
}
} detail: {
Text("Pick an item")
}
}
}
struct SomeEditor: View {
@Bindable var item: Item
@FocusState private var focusedField: FocusField?
var body: some View {
SwiftDownEditor(text: $item.text, onTextChange: { text in
print("onTextChange")
}, onSelectionChange: { rng in
print("onSelectionChange", rng)
})
.debounceTime(0.3)
.focused($focusedField, equals: .field)
.onAppear {
self.focusedField = .field
}
TextEditor(text: $item.text)
}
}
extension SomeEditor {
enum FocusField: Hashable {
case field
}
}
import Foundation
import SwiftData
@Model
final class Item {
var timestamp: Date
var text: String
init(timestamp: Date, text: String = "") {
self.timestamp = timestamp
self.text = text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment