]> git.r.bdr.sh - rbdr/map/blobdiff - Map/State/AppState.swift
Make delete a context action
[rbdr/map] / Map / State / AppState.swift
index e10efea00ab5ec1eeed8989fae02345d436c3789..f0620512c342a062761979b8f545e90e0b96a544 100644 (file)
@@ -3,31 +3,24 @@ import Foundation
 import SwiftUI
 
 struct AppState {
-  var selectedMap: Map? = nil
-  var mapBeingDeleted: Map? = nil
+  var selectedEvolution: StageType = .general
 }
 
 enum AppAction {
-  case selectMap(map: Map?)
-  case deleteMap(map: Map)
-  case exportMapAsImage(map: Map, evolution: StageType)
+  case selectEvolution(evolution: StageType)
+  case exportMapAsImage(map: Map)
   case exportMapAsText(map: Map)
+  case deleteMap(map: Map)
 }
 
 func appStateReducer(state: inout AppState, action: AppAction) {
 
   switch action {
 
-  case .selectMap(let map):
-    state.selectedMap = map
-
-  case .deleteMap(let map):
-    let context = PersistenceController.shared.container.viewContext
+  case .selectEvolution(let evolution):
+    state.selectedEvolution = evolution
 
-    context.delete(map)
-    try? context.save()
-
-  case .exportMapAsImage(let map, let evolution):
+  case .exportMapAsImage(let map):
     let window = NSWindow(
       contentRect: .init(
         origin: .zero,
@@ -44,7 +37,8 @@ func appStateReducer(state: inout AppState, action: AppAction) {
     window.isMovableByWindowBackground = true
     window.makeKeyAndOrderFront(nil)
 
-    let renderView = MapRenderView(map: map, evolution: Stage.stages(evolution))
+    let renderView = MapRenderView(
+      content: map.content ?? "", evolution: Stage.stages(state.selectedEvolution))
 
     let view = NSHostingView(rootView: renderView)
     window.contentView = view
@@ -97,6 +91,11 @@ func appStateReducer(state: inout AppState, action: AppAction) {
         print("Cancel")
       }
     }
+  case .deleteMap(map: let map):
+    let context = PersistenceController.shared.container.viewContext
+    context.delete(map)
+
+    try? context.save()
   }
 }