]> git.r.bdr.sh - rbdr/map/blame - Map/Views/MapDetail.swift
Make delete a context action
[rbdr/map] / Map / Views / MapDetail.swift
CommitLineData
5e8ff485
RBR
1//
2// ContentView.swift
3// Map
4//
5// Created by Ruben Beltran del Rio on 2/1/21.
6//
7
8import CoreData
9import SwiftUI
10
11struct MapDetailView: View {
12 @Environment(\.managedObjectContext) private var viewContext
13 @Environment(\.colorScheme) var colorScheme
14
15 @EnvironmentObject var store: AppStore
16
17 @ObservedObject var map: Map
18
19 private var mapColor: MapColor {
20 MapColor.colorForScheme(colorScheme)
21 }
22
23 private var title: Binding<String> {
24 Binding(
25 get: { map.title ?? "" },
26 set: { title in
27 map.title = title
28 }
29 )
30 }
31
32 private var content: Binding<String> {
33 Binding(
34 get: { map.content ?? "" },
35 set: { content in
36 map.content = content
37 }
38 )
39 }
40
5e8ff485
RBR
41 var body: some View {
42 if map.uuid != nil {
43 VSplitView {
44 VStack {
45 HStack {
46 TextField(
47 "Title", text: title,
48 onCommit: {
49 try? viewContext.save()
50 }
51 ).font(.title2).textFieldStyle(PlainTextFieldStyle()).padding(.vertical, 4.0).padding(
52 .leading, 4.0)
53 Button(action: saveText) {
54 Image(systemName: "doc.text")
55 }.padding(.vertical, 4.0).padding(.leading, 4.0)
56 Button(action: saveImage) {
57 Image(systemName: "photo")
58 }.padding(.vertical, 4.0).padding(.leading, 4.0).padding(.trailing, 8.0)
59 }
77d0155b 60 EvolutionPicker()
5e8ff485
RBR
61
62 ZStack(alignment: .topLeading) {
77d0155b 63 MapTextEditor(text: content, colorScheme: colorScheme).onChange(of: map.content) { _ in
5e8ff485
RBR
64 try? viewContext.save()
65 }
66 .background(mapColor.background)
67 .foregroundColor(mapColor.foreground)
68 .frame(minHeight: 96.0)
69 }.padding(.top, 8.0).padding(.leading, 8.0).background(mapColor.background).cornerRadius(
70 5.0)
71 }.padding(.horizontal, 8.0)
72 ScrollView([.horizontal, .vertical]) {
77d0155b
RBR
73 MapRenderView(
74 content: content.wrappedValue, evolution: Stage.stages(store.state.selectedEvolution))
5e8ff485
RBR
75 }.background(mapColor.background)
76 }
77 } else {
78 DefaultMapView()
79 }
80 }
81
82 private func saveText() {
83 store.send(.exportMapAsText(map: map))
84 }
85
86 private func saveImage() {
77d0155b 87 store.send(.exportMapAsImage(map: map))
5e8ff485
RBR
88 }
89}
90
91struct MapDetailView_Previews: PreviewProvider {
92 static var previews: some View {
93 MapDetailView(map: Map()).environment(
94 \.managedObjectContext, PersistenceController.preview.container.viewContext)
95 }
96}