Skip to content

Commit

Permalink
Sync document in ContentView
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Feb 23, 2023
1 parent 9b1ab28 commit 70b5fd2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 1 addition & 4 deletions MiniSlice.swiftpm/Sources/MiniSliceApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ private var editor = EditorViewModel(preview: preview)
struct MiniSliceApp: App {
var body: some Scene {
DocumentGroup(newDocument: RecipeDocument()) { configuration in
ContentView()
.onAppear {
editor.rawRecipe = configuration.document.raw
}
ContentView(document: configuration.$document)
.environmentObject(preview)
.environmentObject(editor)
}
Expand Down
10 changes: 10 additions & 0 deletions MiniSlice.swiftpm/Sources/View/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import SwiftUI

struct ContentView: View {
@Binding var document: RecipeDocument
@EnvironmentObject private var editor: EditorViewModel

var body: some View {
HStack {
EditorView()
PreviewView()
}
.onAppear {
// 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.
editor.rawRecipe = document.raw
}
.onChange(of: editor.rawRecipe) { raw in
document.raw = raw
}
}
}

0 comments on commit 70b5fd2

Please sign in to comment.