import Combine import CoreData import SwiftUI struct MapDetailScreen: View { @Environment(\.managedObjectContext) private var viewContext @EnvironmentObject var store: AppStore @ObservedObject var map: Map @State var title: String @State var content: String var body: some View { if map.uuid != nil { VSplitView { VStack { HStack { TextField( "Title", text: $title, onCommit: saveModel ).font(.title2).textFieldStyle(PlainTextFieldStyle()).padding(.vertical, 4.0).padding( .leading, 4.0) Button(action: saveText) { Image(systemName: "doc.text") }.padding(.vertical, 4.0).padding(.leading, 4.0) Button(action: saveImage) { Image(systemName: "photo") }.padding(.vertical, 4.0).padding(.leading, 4.0).padding(.trailing, 8.0) } EvolutionPicker() ZStack(alignment: .topLeading) { MapTextEditor(text: $content, onChange: saveModel) .background(Color.ui.background) .foregroundColor(Color.ui.foreground) .frame(minHeight: 96.0) }.padding(.top, 8.0).padding(.leading, 8.0).background(Color.ui.background).cornerRadius( 5.0) }.padding(.horizontal, 8.0) ScrollView([.horizontal, .vertical]) { MapRenderView(content: $content, evolution: .constant(store.state.selectedEvolution)) } }.onDisappear { saveModel() } } else { EmptyMapDetailScreen() } } private func saveModel() { map.content = content map.title = title try? viewContext.save() } private func saveText() { store.send(.exportMapAsText(map: map)) } private func saveImage() { store.send(.exportMapAsImage(map: map)) } } struct MapDetailView_Previews: PreviewProvider { static var previews: some View { MapDetailScreen(map: Map(), title: "", content: "").environment( \.managedObjectContext, PersistenceController.preview.container.viewContext) } }