From: Ruben Beltran del Rio Date: Thu, 4 Feb 2021 23:23:05 +0000 (+0100) Subject: Make delete a context action X-Git-Tag: 1.2.0~1 X-Git-Url: https://git.r.bdr.sh/rbdr/map/commitdiff_plain/91fd86189477e6c690c6487d51d80bc58c8ecb63?ds=inline Make delete a context action --- diff --git a/Map/State/AppState.swift b/Map/State/AppState.swift index cbc9ba6..f062051 100644 --- a/Map/State/AppState.swift +++ b/Map/State/AppState.swift @@ -4,13 +4,13 @@ import SwiftUI struct AppState { var selectedEvolution: StageType = .general - var mapBeingDeleted: Map? = nil } 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) { @@ -91,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() } } diff --git a/Map/Views/ContentView.swift b/Map/Views/ContentView.swift index cf1f145..f439026 100644 --- a/Map/Views/ContentView.swift +++ b/Map/Views/ContentView.swift @@ -37,6 +37,11 @@ struct ContentView: View { .foregroundColor(Color.black) .cornerRadius(2.0) }.padding(.leading, 8.0) + }.contextMenu { + Button(action: { store.send(.deleteMap(map: map))}) { + Image(systemName: "trash") + Text("Delete") + } } } .onDelete(perform: deleteMaps) @@ -51,6 +56,7 @@ struct ContentView: View { } } } + DefaultMapView() } }