]> git.r.bdr.sh - rbdr/map/blob - Map/Views/MapDetail.swift
Add code for first release
[rbdr/map] / Map / Views / MapDetail.swift
1 //
2 // ContentView.swift
3 // Map
4 //
5 // Created by Ruben Beltran del Rio on 2/1/21.
6 //
7
8 import CoreData
9 import SwiftUI
10
11 struct 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
41 @State private var selectedEvolution = StageType.general
42
43 var body: some View {
44 if map.uuid != nil {
45 VSplitView {
46 VStack {
47 HStack {
48 TextField(
49 "Title", text: title,
50 onCommit: {
51 try? viewContext.save()
52 }
53 ).font(.title2).textFieldStyle(PlainTextFieldStyle()).padding(.vertical, 4.0).padding(
54 .leading, 4.0)
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)
61 }
62 Picker("Evolution", selection: $selectedEvolution) {
63 ForEach(StageType.allCases) { stage in
64 Text(Stage.title(stage)).tag(stage).padding(4.0)
65 }
66 }.padding(.horizontal, 8.0).padding(.vertical, 4.0)
67
68 ZStack(alignment: .topLeading) {
69 MapTextEditor(text: content).onChange(of: map.content) { _ in
70 try? viewContext.save()
71 }
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(
76 5.0)
77 }.padding(.horizontal, 8.0)
78 ScrollView([.horizontal, .vertical]) {
79 MapRenderView(map: map, evolution: Stage.stages(selectedEvolution))
80 }.background(mapColor.background)
81 }
82 } else {
83 DefaultMapView()
84 }
85 }
86
87 private func saveText() {
88 store.send(.exportMapAsText(map: map))
89 }
90
91 private func saveImage() {
92 store.send(.exportMapAsImage(map: map, evolution: selectedEvolution))
93 }
94 }
95
96 struct MapDetailView_Previews: PreviewProvider {
97 static var previews: some View {
98 MapDetailView(map: Map()).environment(
99 \.managedObjectContext, PersistenceController.preview.container.viewContext)
100 }
101 }