aboutsummaryrefslogtreecommitdiff
path: root/Map/Presentation/Screens/MapDetailScreen.swift
blob: a5c32fd16083f527c94ec2945a8aca44958c5fef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Combine
import CoreData
import SwiftUI

struct MapDetailScreen: View {
  @Environment(\.managedObjectContext) private var viewContext

  @EnvironmentObject var store: AppStore

  @ObservedObject var map: Map

  @State var title: String
  @State var content: String

  var body: some View {
    if map.uuid != nil {
      VSplitView {
        VStack {
          HStack {
            TextField(
                "Title", text: $title, onCommit: saveModel
            ).font(.title2).textFieldStyle(PlainTextFieldStyle()).padding(.vertical, 4.0).padding(
                .leading, 4.0)
            Button(action: saveText) {
              Image(systemName: "doc.text")
            }.padding(.vertical, 4.0).padding(.leading, 4.0)
            Button(action: saveImage) {
              Image(systemName: "photo")
            }.padding(.vertical, 4.0).padding(.leading, 4.0).padding(.trailing, 8.0)
          }
          EvolutionPicker()

          ZStack(alignment: .topLeading) {
            MapTextEditor(text: $content, onChange: saveModel)
              .background(Color.ui.background)
              .foregroundColor(Color.ui.foreground)
              .frame(minHeight: 96.0)
          }.padding(.top, 8.0).padding(.leading, 8.0).background(Color.ui.background).cornerRadius(
            5.0)
        }.padding(.horizontal, 8.0)
        ScrollView([.horizontal, .vertical]) {
          MapRenderView(content: $content, evolution:  .constant(store.state.selectedEvolution))
        }
      }.onDisappear {
        saveModel()
      }
    } else {
      EmptyMapDetailScreen()
    }
  }

  private func saveModel() {
    map.content = content
    map.title = title
    try? viewContext.save()
  }

  private func saveText() {
    store.send(.exportMapAsText(map: map))
  }

  private func saveImage() {
    store.send(.exportMapAsImage(map: map))
  }
}

struct MapDetailView_Previews: PreviewProvider {
  static var previews: some View {
    MapDetailScreen(map: Map(), title: "", content: "").environment(
      \.managedObjectContext, PersistenceController.preview.container.viewContext)
  }
}