]>
Commit | Line | Data |
---|---|---|
1b85f723 RBR |
1 | // |
2 | // ContentView.swift | |
3 | // Map | |
4 | // | |
5 | // Created by Ruben Beltran del Rio on 2/1/21. | |
6 | // | |
7 | ||
8 | import SwiftUI | |
9 | import CoreData | |
10 | ||
11 | struct ContentView: View { | |
12 | @Environment(\.managedObjectContext) private var viewContext | |
13 | ||
14 | @FetchRequest( | |
15 | sortDescriptors: [NSSortDescriptor(keyPath: \Map.createdAt, ascending: true)], | |
16 | animation: .default) | |
17 | private var maps: FetchedResults<Map> | |
18 | ||
19 | var body: some View { | |
20 | NavigationView { | |
21 | List { | |
22 | ForEach(maps) { map in | |
23 | NavigationLink(destination: MapDetailView(map: map)) { | |
24 | HStack { | |
25 | Text(map.title ?? "Untitled Map") | |
26 | Text(map.createdAt!.format()) | |
27 | .font(.footnote) | |
28 | .foregroundColor(Color.accentColor) | |
29 | } | |
30 | } | |
31 | } | |
32 | .onDelete(perform: deleteMaps) | |
33 | } | |
34 | .toolbar { | |
35 | HStack { | |
36 | Button(action: toggleSidebar) { | |
37 | Label("Toggle Sidebar", systemImage: "sidebar.left") | |
38 | } | |
39 | Button(action: addMap) { | |
40 | Label("Add Map", systemImage: "plus") | |
41 | } | |
42 | } | |
43 | } | |
44 | } | |
45 | } | |
46 | ||
47 | private func toggleSidebar() { | |
48 | NSApp.keyWindow?.firstResponder?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil) | |
49 | } | |
50 | ||
51 | private func addMap() { | |
52 | withAnimation { | |
53 | let newMap = Map(context: viewContext) | |
54 | newMap.uuid = UUID() | |
55 | newMap.createdAt = Date() | |
56 | newMap.title = "Map \(newMap.createdAt!.format())" | |
57 | newMap.content = "" | |
58 | ||
59 | do { | |
60 | try viewContext.save() | |
61 | } catch { | |
62 | // Replace this implementation with code to handle the error appropriately. | |
63 | // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
64 | let nsError = error as NSError | |
65 | fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
66 | } | |
67 | } | |
68 | } | |
69 | ||
70 | private func deleteMaps(offsets: IndexSet) { | |
71 | withAnimation { | |
72 | offsets.map { maps[$0] }.forEach(viewContext.delete) | |
73 | ||
74 | do { | |
75 | try viewContext.save() | |
76 | } catch { | |
77 | // Replace this implementation with code to handle the error appropriately. | |
78 | // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
79 | let nsError = error as NSError | |
80 | fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
81 | } | |
82 | } | |
83 | } | |
84 | } | |
85 | ||
86 | private let mapFormatter: DateFormatter = { | |
87 | let formatter = DateFormatter() | |
88 | formatter.dateStyle = .short | |
89 | formatter.timeStyle = .medium | |
90 | return formatter | |
91 | }() | |
92 | ||
93 | struct ContentView_Previews: PreviewProvider { | |
94 | static var previews: some View { | |
95 | ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) | |
96 | } | |
97 | } |