diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-05-07 15:22:54 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-05-07 15:22:54 +0200 |
| commit | fdb4633d3e9158e457d57e820df6e1efb4df39c2 (patch) | |
| tree | 176cad99cb9befc0a65a7af02561019e811814de /Map/Data/AppState.swift | |
| parent | 75a0e4509a70055851b085f3f7293ae1cf48164c (diff) | |
Update to support notes + new style2.0.0
Diffstat (limited to 'Map/Data/AppState.swift')
| -rw-r--r-- | Map/Data/AppState.swift | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/Map/Data/AppState.swift b/Map/Data/AppState.swift new file mode 100644 index 0000000..d0d5670 --- /dev/null +++ b/Map/Data/AppState.swift @@ -0,0 +1,103 @@ +import Cocoa +import Foundation +import SwiftUI + +struct AppState { + var selectedEvolution: StageType = .general +} + +enum AppAction { + 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 .selectEvolution(let evolution): + state.selectedEvolution = evolution + + case .exportMapAsImage(let map): + let window = NSWindow( + contentRect: .init( + origin: .zero, + size: .init( + width: NSScreen.main!.frame.width, + height: NSScreen.main!.frame.height)), + styleMask: [.closable], + backing: .buffered, + defer: false) + + window.title = map.title ?? "Untitled Map" + window.isOpaque = true + window.center() + window.isMovableByWindowBackground = true + window.makeKeyAndOrderFront(nil) + + let renderView = MapRenderView( + content: Binding.constant(map.content ?? ""), + evolution: Binding.constant(state.selectedEvolution)) + + let view = NSHostingView(rootView: renderView) + window.contentView = view + + let imageRepresentation = view.bitmapImageRepForCachingDisplay(in: view.bounds)! + view.cacheDisplay(in: view.bounds, to: imageRepresentation) + let image = NSImage(cgImage: imageRepresentation.cgImage!, size: view.bounds.size) + + let dialog = NSSavePanel() + + dialog.title = "Save Map" + dialog.showsResizeIndicator = false + dialog.canCreateDirectories = true + dialog.showsHiddenFiles = false + dialog.allowedContentTypes = [.png] + dialog.nameFieldStringValue = map.title ?? "Untitled Map" + + if dialog.runModal() == NSApplication.ModalResponse.OK { + let result = dialog.url + + if result != nil { + + image.writePNG(toURL: result!) + print("saved at \(result!)") + } + } else { + print("Cancel") + } + window.orderOut(nil) + + case .exportMapAsText(let map): + let dialog = NSSavePanel() + + dialog.title = "Save Map Text" + dialog.showsResizeIndicator = false + dialog.canCreateDirectories = true + dialog.showsHiddenFiles = false + dialog.allowedContentTypes = [.text] + dialog.nameFieldStringValue = map.title ?? "Untitled Map" + + if let content = map.content { + + if dialog.runModal() == NSApplication.ModalResponse.OK { + let result = dialog.url + + if let result = result { + try? content.write(to: result, atomically: true, encoding: String.Encoding.utf8) + } + } else { + print("Cancel") + } + } + case .deleteMap(let map): + let context = PersistenceController.shared.container.viewContext + context.delete(map) + + try? context.save() + } +} + +typealias AppStore = Store<AppState, AppAction> |