]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Screens/MapDetailScreen.swift
Update to support notes + new style
[rbdr/map] / Map / Presentation / Screens / MapDetailScreen.swift
1 import Combine
2 import CoreData
3 import SwiftUI
4
5 struct MapDetailScreen: View {
6 @Environment(\.managedObjectContext) private var viewContext
7
8 @EnvironmentObject var store: AppStore
9
10 @ObservedObject var map: Map
11
12 @State var title: String
13 @State var content: String
14
15 var body: some View {
16 if map.uuid != nil {
17 VSplitView {
18 VStack {
19 HStack {
20 TextField(
21 "Title", text: $title, onCommit: saveModel
22 ).font(.title2).textFieldStyle(PlainTextFieldStyle()).padding(.vertical, 4.0).padding(
23 .leading, 4.0)
24 Button(action: saveText) {
25 Image(systemName: "doc.text")
26 }.padding(.vertical, 4.0).padding(.leading, 4.0)
27 Button(action: saveImage) {
28 Image(systemName: "photo")
29 }.padding(.vertical, 4.0).padding(.leading, 4.0).padding(.trailing, 8.0)
30 }
31 EvolutionPicker()
32
33 ZStack(alignment: .topLeading) {
34 MapTextEditor(text: $content, onChange: saveModel)
35 .background(Color.ui.background)
36 .foregroundColor(Color.ui.foreground)
37 .frame(minHeight: 96.0)
38 }.padding(.top, 8.0).padding(.leading, 8.0).background(Color.ui.background).cornerRadius(
39 5.0)
40 }.padding(.horizontal, 8.0)
41 ScrollView([.horizontal, .vertical]) {
42 MapRenderView(content: $content, evolution: .constant(store.state.selectedEvolution))
43 }
44 }.onDisappear {
45 saveModel()
46 }
47 } else {
48 EmptyMapDetailScreen()
49 }
50 }
51
52 private func saveModel() {
53 map.content = content
54 map.title = title
55 try? viewContext.save()
56 }
57
58 private func saveText() {
59 store.send(.exportMapAsText(map: map))
60 }
61
62 private func saveImage() {
63 store.send(.exportMapAsImage(map: map))
64 }
65 }
66
67 struct MapDetailView_Previews: PreviewProvider {
68 static var previews: some View {
69 MapDetailScreen(map: Map(), title: "", content: "").environment(
70 \.managedObjectContext, PersistenceController.preview.container.viewContext)
71 }
72 }