aboutsummaryrefslogtreecommitdiff
path: root/Map/Views/MapDetail.swift
blob: bebe3a12414a5e9be8f5a9ca46de107d4442c530 (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
102
103
//
//  ContentView.swift
//  Map
//
//  Created by Ruben Beltran del Rio on 2/1/21.
//

import Combine
import CoreData
import SwiftUI

class SaveTimer {
  let currentTimePublisher = Timer.TimerPublisher(interval: 1, runLoop: .main, mode: .default)
  let cancellable: AnyCancellable?

  init() {
    self.cancellable = currentTimePublisher.connect() as? AnyCancellable
  }

  deinit {
    self.cancellable?.cancel()
  }
}

let timer = SaveTimer()

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)
  }

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

  var body: some View {
    if map.uuid != nil {
      VSplitView {
        VStack {
          HStack {
            TextField(
              "Title", text: $title
            ).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, colorScheme: colorScheme)
              .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]) {
          SlowMapRender(
            content: content, evolution: Stage.stages(store.state.selectedEvolution),
            colorScheme: colorScheme)
        }.background(mapColor.background)
      }.onReceive(timer.currentTimePublisher) { _ in
        saveModel()
      }.onDisappear {
        saveModel()
      }
    } else {
      DefaultMapView()
    }
  }

  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 {
    MapDetailView(map: Map(), title: "", content: "").environment(
      \.managedObjectContext, PersistenceController.preview.container.viewContext)
  }
}