]>
Commit | Line | Data |
---|---|---|
5e8ff485 RBR |
1 | // |
2 | // ContentView.swift | |
3 | // Map | |
4 | // | |
5 | // Created by Ruben Beltran del Rio on 2/1/21. | |
6 | // | |
7 | ||
8 | import CoreData | |
9 | import SwiftUI | |
10 | ||
11 | struct ContentView: View { | |
12 | @Environment(\.managedObjectContext) private var viewContext | |
13 | ||
77d0155b RBR |
14 | @EnvironmentObject var store: AppStore |
15 | ||
5e8ff485 RBR |
16 | @FetchRequest( |
17 | sortDescriptors: [NSSortDescriptor(keyPath: \Map.createdAt, ascending: true)], | |
18 | animation: .default) | |
19 | private var maps: FetchedResults<Map> | |
20 | ||
21 | var body: some View { | |
22 | NavigationView { | |
23 | List { | |
24 | if maps.count == 0 { | |
25 | DefaultMapView() | |
26 | } | |
27 | ForEach(maps) { map in | |
28 | NavigationLink(destination: MapDetailView(map: map)) { | |
29 | HStack { | |
30 | Text(map.title ?? "Untitled Map") | |
31 | Spacer() | |
32 | Text(mapFormatter.string(from: (map.createdAt ?? Date()))) | |
33 | .font(.caption) | |
34 | .padding(.vertical, 2.0) | |
35 | .padding(.horizontal, 4.0) | |
36 | .background(Color.accentColor) | |
37 | .foregroundColor(Color.black) | |
38 | .cornerRadius(2.0) | |
39 | }.padding(.leading, 8.0) | |
40 | } | |
41 | } | |
42 | .onDelete(perform: deleteMaps) | |
43 | }.frame(minWidth: 250.0, alignment: .leading) | |
44 | .toolbar { | |
45 | HStack { | |
46 | Button(action: toggleSidebar) { | |
47 | Label("Toggle Sidebar", systemImage: "sidebar.left") | |
48 | } | |
49 | Button(action: addMap) { | |
50 | Label("Add Map", systemImage: "plus") | |
51 | } | |
52 | } | |
53 | } | |
54 | } | |
55 | } | |
56 | ||
57 | private func toggleSidebar() { | |
58 | NSApp.keyWindow?.firstResponder?.tryToPerform( | |
59 | #selector(NSSplitViewController.toggleSidebar(_:)), with: nil) | |
60 | } | |
61 | ||
62 | private func addMap() { | |
63 | withAnimation { | |
64 | let newMap = Map(context: viewContext) | |
65 | newMap.uuid = UUID() | |
66 | newMap.createdAt = Date() | |
67 | newMap.title = "Map \(newMap.createdAt!.format())" | |
68 | newMap.content = "" | |
69 | ||
70 | do { | |
71 | try viewContext.save() | |
72 | } catch { | |
73 | let nsError = error as NSError | |
74 | fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
75 | } | |
76 | } | |
77 | } | |
78 | ||
79 | private func deleteMaps(offsets: IndexSet) { | |
80 | ||
81 | withAnimation { | |
82 | offsets.map { maps[$0] }.forEach(viewContext.delete) | |
83 | ||
84 | do { | |
85 | try viewContext.save() | |
86 | } catch { | |
87 | let nsError = error as NSError | |
88 | fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
89 | } | |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | private let mapFormatter: DateFormatter = { | |
95 | let formatter = DateFormatter() | |
96 | formatter.dateStyle = .short | |
97 | formatter.timeStyle = .none | |
98 | return formatter | |
99 | }() | |
100 | ||
101 | struct ContentView_Previews: PreviewProvider { | |
102 | static var previews: some View { | |
103 | ContentView().environment( | |
104 | \.managedObjectContext, PersistenceController.preview.container.viewContext) | |
105 | } | |
106 | } |