Skip to content

Commit 25d993e

Browse files
committed
Sync document in ContentView
1 parent 650ee4b commit 25d993e

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

MiniSlice.swiftpm/Sources/MiniSliceApp.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ private var editor = EditorViewModel(preview: preview)
77
struct MiniSliceApp: App {
88
var body: some Scene {
99
DocumentGroup(newDocument: RecipeDocument()) { configuration in
10-
ContentView()
11-
.onAppear {
12-
editor.rawRecipe = configuration.document.raw
13-
}
10+
ContentView(document: configuration.$document)
1411
.environmentObject(preview)
1512
.environmentObject(editor)
1613
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import SwiftUI
22

33
struct ContentView: View {
4+
@Binding var document: RecipeDocument
5+
@EnvironmentObject private var editor: EditorViewModel
6+
47
var body: some View {
58
HStack {
69
EditorView()
710
PreviewView()
811
}
12+
.onAppear {
13+
// TODO: Is this sufficient? The user should be able to open documents, which might not cause the ContentView to 're-appear' but only update? We don't listen to document changes directly to prevent a listener cycle.
14+
editor.rawRecipe = document.raw
15+
}
16+
.onChange(of: editor.rawRecipe) { raw in
17+
document.raw = raw
18+
}
919
}
1020
}

0 commit comments

Comments
 (0)