4 struct MapEditorWindow: View {
5 @Environment(\.managedObjectContext) private var viewContext
7 @EnvironmentObject var store: AppStore
10 sortDescriptors: [NSSortDescriptor(keyPath: \Map.createdAt, ascending: true)],
12 private var maps: FetchedResults<Map>
18 EmptyMapDetailScreen()
20 ForEach(maps) { map in
22 destination: MapDetailScreen(map: map, title: map.title ?? "", content: map.content ?? "")
25 Text(map.title ?? "Untitled Map")
27 Text(mapFormatter.string(from: (map.createdAt ?? Date())))
29 .padding(.vertical, 2.0)
30 .padding(.horizontal, 4.0)
31 .background(Color.accentColor)
32 .foregroundColor(Color.black)
34 }.padding(.leading, 8.0)
37 action: { store.send(.deleteMap(map: map)) },
39 Image(systemName: "trash")
44 .onDelete(perform: deleteMaps)
45 }.frame(minWidth: 250.0, alignment: .leading)
48 Button(action: toggleSidebar) {
49 Label("Toggle Sidebar", systemImage: "sidebar.left")
51 Button(action: addMap) {
52 Label("Add Map", systemImage: "plus")
56 EmptyMapDetailScreen()
60 private func toggleSidebar() {
61 NSApp.keyWindow?.firstResponder?.tryToPerform(
62 #selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
65 private func addMap() {
67 let newMap = Map(context: viewContext)
69 newMap.createdAt = Date()
70 newMap.title = "Map \(newMap.createdAt!.format())"
74 try viewContext.save()
76 let nsError = error as NSError
77 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
82 private func deleteMaps(offsets: IndexSet) {
85 offsets.map { maps[$0] }.forEach(viewContext.delete)
88 try viewContext.save()
90 let nsError = error as NSError
91 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
97 private let mapFormatter: DateFormatter = {
98 let formatter = DateFormatter()
99 formatter.dateStyle = .short
100 formatter.timeStyle = .none
104 struct ContentView_Previews: PreviewProvider {
105 static var previews: some View {
106 MapEditorWindow().environment(
107 \.managedObjectContext, PersistenceController.preview.container.viewContext)