Here's how I initialize the SwiftData store using DocumentGroup. Please let me know if I need to provide any future info.
Bug logged as Feedback (FB15100795)
//
// Presents_2024App.swift
// Presents 2024
//
// Created by Benjamin Lee on 07/06/2024.
//
import RichTextKit
import SwiftData
import SwiftUI
import UniformTypeIdentifiers
@main
struct Presents_2024App: App {
// MARK: Properties
#if os(iOS)
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#elseif os(macOS)
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#endif
@Environment(\.modelContext) private var modelContext
@FocusState private var isFocused: Bool
@StateObject private var transientSettings = TransientSettings()
// MARK: Computed Properties
var body: some Scene {
#if os(iOS)
DocumentGroupLaunchScene("Present's 2024") {
NewDocumentButton("New Activity...")
} background: {
Image(.background)
.resizable()
.scaledToFill()
.ignoresSafeArea()
} overlayAccessoryView: { _ in
AccessoryView()
}
#endif
DocumentGroup(editing: .presentsDocument,
migrationPlan: Presents_2024MigrationPlan.self,
editor: {
RootView()
.environmentObject(appDelegate)
.environmentObject(transientSettings)
.focused($isFocused)
.onAppear {
isFocused = true
DispatchQueue.main.async {
// Here, you might need to enforce the focus on the RootView
#if os(macOS)
if let window = NSApplication.shared.windows.first {
window.makeFirstResponder(window.contentView)
}
#endif
}
}
.onKeyPress(.leftArrow) {
DispatchQueue.main.async {
if transientSettings.selectedTab > 0 {
transientSettings.selectedTab -= 1
}
print("⬅️ pressed")
}
return .handled
}
.onKeyPress(.rightArrow) {
DispatchQueue.main.async {
if transientSettings.selectedTab < transientSettings.numberOfItems - 1 {
transientSettings.selectedTab += 1
}
print("➡️ pressed")
}
return .handled
}
},
prepareDocument: { context in
// Prepare the document before it is opened or edited.
// Insert example Bookend Item
let newBookend = Bookend(firstLine: NSAttributedString(""), secondLine: NSAttributedString(""), thirdLine: NSAttributedString(""), bookendType: .welcome)
let newItem = Item(itemText: "A Bookend Video", itemType: .bookend, order: 0)
newItem.bookends.append(newBookend)
context.insert(newItem)
// Insert example Question Item
let question = Question(questionText: NSAttributedString("An Example Question"))
let item = Item(itemText: "An Example Question", itemType: .question, order: 1)
item.questions.append(question)
context.insert(item)
context.insert(Brand.defaultBrand)
})
// .commands {
// SidebarCommands()
// #if os(macOS)
//// AboutCommand()
// #endif
// RichTextCommand.FormatMenu()
//
//// CommandMenu("RichTextKit") {
//// DemoUrl.github.link
//// DemoUrl.documentation.link
//// }
// }
}
}
extension UTType {
static var presentsDocument: UTType {
UTType(exportedAs: "eu.makeitso.presentsdocument")
}
}
struct Presents_2024MigrationPlan: SchemaMigrationPlan {
static var schemas: [VersionedSchema.Type] = [Presents_2024VersionedSchema.self]
static var stages: [MigrationStage] = []
}
struct Presents_2024VersionedSchema: VersionedSchema {
static var versionIdentifier = Schema.Version(1, 0, 0)
static var models: [any PersistentModel.Type] = [Item.self, UserSettings.self, Brand.self]
}
#Preview(traits: .sampleData) {
@Previewable @State var appDelete = AppDelegate()
@Previewable @State var transientSettings = TransientSettings()
RootView()
.environmentObject(appDelete)
.environmentObject(transientSettings)
}
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags: