aboutsummaryrefslogtreecommitdiff
path: root/Map/Views/MapDetail.swift
blob: a8ea3d17fa054611e46f4a5ae66c34299973e6f9 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
//  ContentView.swift
//  Map
//
//  Created by Ruben Beltran del Rio on 2/1/21.
//

import CoreData
import SwiftUI

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

  @EnvironmentObject var store: AppStore

  @ObservedObject var map: Map

  private var mapColor: MapColor {
    MapColor.colorForScheme(colorScheme)
  }

  private var title: Binding<String> {
    Binding(
      get: { map.title ?? "" },
      set: { title in
        map.title = title
      }
    )
  }

  private var content: Binding<String> {
    Binding(
      get: { map.content ?? "" },
      set: { content in
        map.content = content
      }
    )
  }

  @State private var selectedEvolution = StageType.general

  var body: some View {
    if map.uuid != nil {
      VSplitView {
        VStack {
          HStack {
            TextField(
              "Title", text: title,
              onCommit: {
                try? viewContext.save()
              }
            ).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)
          }
          Picker("Evolution", selection: $selectedEvolution) {
            ForEach(StageType.allCases) { stage in
              Text(Stage.title(stage)).tag(stage).padding(4.0)
            }
          }.padding(.horizontal, 8.0).padding(.vertical, 4.0)

          ZStack(alignment: .topLeading) {
            MapTextEditor(text: content).onChange(of: map.content) { _ in
              try? viewContext.save()
            }
            .background(mapColor.background)
            .foregroundColor(mapColor.foreground)
            .frame(minHeight: 96.0)
          }.padding(.top, 8.0).padding(.leading, 8.0).background(mapColor.background).cornerRadius(
            5.0)
        }.padding(.horizontal, 8.0)
        ScrollView([.horizontal, .vertical]) {
          MapRenderView(map: map, evolution: Stage.stages(selectedEvolution))
        }.background(mapColor.background)
      }
    } else {
      DefaultMapView()
    }
  }

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

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

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