5 // Created by Ruben Beltran del Rio on 2/1/21.
11 struct MapDetailView: View {
12 @Environment(\.managedObjectContext) private var viewContext
13 @Environment(\.colorScheme) var colorScheme
15 @EnvironmentObject var store: AppStore
17 @ObservedObject var map: Map
19 private var mapColor: MapColor {
20 MapColor.colorForScheme(colorScheme)
23 private var title: Binding<String> {
25 get: { map.title ?? "" },
32 private var content: Binding<String> {
34 get: { map.content ?? "" },
41 @State private var selectedEvolution = StageType.general
51 try? viewContext.save()
53 ).font(.title2).textFieldStyle(PlainTextFieldStyle()).padding(.vertical, 4.0).padding(
55 Button(action: saveText) {
56 Image(systemName: "doc.text")
57 }.padding(.vertical, 4.0).padding(.leading, 4.0)
58 Button(action: saveImage) {
59 Image(systemName: "photo")
60 }.padding(.vertical, 4.0).padding(.leading, 4.0).padding(.trailing, 8.0)
62 Picker("Evolution", selection: $selectedEvolution) {
63 ForEach(StageType.allCases) { stage in
64 Text(Stage.title(stage)).tag(stage).padding(4.0)
66 }.padding(.horizontal, 8.0).padding(.vertical, 4.0)
68 ZStack(alignment: .topLeading) {
69 MapTextEditor(text: content).onChange(of: map.content) { _ in
70 try? viewContext.save()
72 .background(mapColor.background)
73 .foregroundColor(mapColor.foreground)
74 .frame(minHeight: 96.0)
75 }.padding(.top, 8.0).padding(.leading, 8.0).background(mapColor.background).cornerRadius(
77 }.padding(.horizontal, 8.0)
78 ScrollView([.horizontal, .vertical]) {
79 MapRenderView(map: map, evolution: Stage.stages(selectedEvolution))
80 }.background(mapColor.background)
87 private func saveText() {
88 store.send(.exportMapAsText(map: map))
91 private func saveImage() {
92 store.send(.exportMapAsImage(map: map, evolution: selectedEvolution))
96 struct MapDetailView_Previews: PreviewProvider {
97 static var previews: some View {
98 MapDetailView(map: Map()).environment(
99 \.managedObjectContext, PersistenceController.preview.container.viewContext)