]>
Commit | Line | Data |
---|---|---|
75a0e450 | 1 | import Combine |
5e8ff485 RBR |
2 | import CoreData |
3 | import SwiftUI | |
4 | ||
fdb4633d | 5 | struct MapDetailScreen: View { |
5e8ff485 | 6 | @Environment(\.managedObjectContext) private var viewContext |
5e8ff485 RBR |
7 | |
8 | @EnvironmentObject var store: AppStore | |
9 | ||
10 | @ObservedObject var map: Map | |
11 | ||
75a0e450 RBR |
12 | @State var title: String |
13 | @State var content: String | |
5e8ff485 | 14 | |
5e8ff485 RBR |
15 | var body: some View { |
16 | if map.uuid != nil { | |
17 | VSplitView { | |
18 | VStack { | |
19 | HStack { | |
20 | TextField( | |
fdb4633d | 21 | "Title", text: $title, onCommit: saveModel |
5e8ff485 | 22 | ).font(.title2).textFieldStyle(PlainTextFieldStyle()).padding(.vertical, 4.0).padding( |
fdb4633d | 23 | .leading, 4.0) |
5e8ff485 RBR |
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 | } | |
77d0155b | 31 | EvolutionPicker() |
5e8ff485 RBR |
32 | |
33 | ZStack(alignment: .topLeading) { | |
fdb4633d RBR |
34 | MapTextEditor(text: $content, onChange: saveModel) |
35 | .background(Color.ui.background) | |
36 | .foregroundColor(Color.ui.foreground) | |
75a0e450 | 37 | .frame(minHeight: 96.0) |
fdb4633d | 38 | }.padding(.top, 8.0).padding(.leading, 8.0).background(Color.ui.background).cornerRadius( |
5e8ff485 RBR |
39 | 5.0) |
40 | }.padding(.horizontal, 8.0) | |
41 | ScrollView([.horizontal, .vertical]) { | |
fdb4633d RBR |
42 | MapRenderView(content: $content, evolution: .constant(store.state.selectedEvolution)) |
43 | } | |
75a0e450 RBR |
44 | }.onDisappear { |
45 | saveModel() | |
5e8ff485 RBR |
46 | } |
47 | } else { | |
fdb4633d | 48 | EmptyMapDetailScreen() |
5e8ff485 RBR |
49 | } |
50 | } | |
51 | ||
75a0e450 RBR |
52 | private func saveModel() { |
53 | map.content = content | |
54 | map.title = title | |
55 | try? viewContext.save() | |
56 | } | |
57 | ||
5e8ff485 RBR |
58 | private func saveText() { |
59 | store.send(.exportMapAsText(map: map)) | |
60 | } | |
61 | ||
62 | private func saveImage() { | |
77d0155b | 63 | store.send(.exportMapAsImage(map: map)) |
5e8ff485 RBR |
64 | } |
65 | } | |
66 | ||
67 | struct MapDetailView_Previews: PreviewProvider { | |
68 | static var previews: some View { | |
fdb4633d | 69 | MapDetailScreen(map: Map(), title: "", content: "").environment( |
5e8ff485 RBR |
70 | \.managedObjectContext, PersistenceController.preview.container.viewContext) |
71 | } | |
72 | } |