Branchwell

Jotley / app / Shared / Views / NotesTrashView.swift

1import CoreData
2import SwiftUI
3
4struct NotesTrashLink: View {
5 @FetchRequest(
6 sortDescriptors: [NSSortDescriptor(key: "trashedAt", ascending: false)],
7 predicate: SharedPersistenceController.trashedNotesPredicate
8 ) private var notes: FetchedResults<Note>
9 @Binding var showingTrash: Bool
10
11 var body: some View {
12 Button {
13 showingTrash = true
14 } label: {
15 HStack(spacing: 10) {
16 Image(systemName: "trash")
17 .foregroundStyle(Color.themePrimaryAccent)
18 Text("Trash")
19 .foregroundStyle(Color.themeTextPrimary)
20 Spacer()
21 if !notes.isEmpty {
22 Text(notes.count, format: .number)
23 .foregroundStyle(Color.themeTextSecondary)
24 }
25 Image(systemName: "chevron.right")
26 .font(.caption.weight(.semibold))
27 .foregroundStyle(Color.themeTextSecondary)
28 }
29 .padding(.horizontal, 12)
30 .frame(minHeight: 44)
31 .contentShape(Rectangle())
32 }
33 .buttonStyle(.plain)
34 .accessibilityIdentifier("notesTrashLink")
35 }
36}
37
38struct NotesTrashView: View {
39 let store: SharedPersistenceController
40 @Environment(\.dismiss) private var dismiss
41 @FetchRequest(
42 sortDescriptors: [NSSortDescriptor(key: "trashedAt", ascending: false)],
43 predicate: SharedPersistenceController.trashedNotesPredicate
44 ) private var notes: FetchedResults<Note>
45
46 private enum Deletion {
47 case one(Note)
48 case all([Note])
49 }
50
51 @State private var deletion: Deletion?
52 @State private var errorMessage: String?
53
54 var body: some View {
55 VStack(spacing: 0) {
56 HStack(spacing: 12) {
57 Text("Trash")
58 .font(.title2.bold())
59 Spacer()
60 Button("Done") { dismiss() }
61 .jotleyDismissButtonStyle()
62 .accessibilityIdentifier("notesTrashDone")
63 }
64 .padding(20)
65 Divider()
66 if notes.isEmpty {
67 ContentUnavailableView("Trash Is Empty", systemImage: "trash")
68 .frame(maxWidth: .infinity, maxHeight: .infinity)
69 } else {
70 ScrollView {
71 LazyVStack(alignment: .leading, spacing: 0) {
72 Text("Restore notes to put them back in your library.")
73 .font(.subheadline)
74 .foregroundStyle(Color.themeTextSecondary)
75 .padding(.bottom, 12)
76 ForEach(notes, id: \.objectID) { note in
77 trashRow(for: note)
78 Divider()
79 }
80 }
81 .padding(20)
82 }
83 Divider()
84 Button(role: .destructive) {
85 deletion = .all(Array(notes))
86 } label: {
87 Label("Empty Trash", systemImage: "trash")
88 .foregroundStyle(.red)
89 }
90 .buttonStyle(JotleyGlassButtonStyle())
91 .accessibilityIdentifier("notesEmptyTrash")
92 .padding(16)
93 }
94 }
95 .foregroundStyle(Color.themeTextPrimary)
96 .background(Color.themeBackground)
97 .presentationBackground(Color.themeBackground)
98 #if os(iOS)
99 .presentationDragIndicator(.visible)
100 #endif
101 #if os(macOS)
102 .frame(minWidth: 440, idealWidth: 520, minHeight: 320, idealHeight: 440)
103 #endif
104 .alert(
105 isEmptyingTrash ? LocalizedStringKey("Empty Trash?") : LocalizedStringKey("Delete Note Permanently?"),
106 isPresented: Binding(get: { deletion != nil }, set: { if !$0 { deletion = nil } }),
107 presenting: deletion
108 ) { pending in
109 Button("Cancel", role: .cancel) { deletion = nil }
110 Button(role: .destructive) {
111 switch pending {
112 case .one(let note): perform { try store.permanentlyDeleteNotes([note]) }
113 case .all(let notes): perform { try store.permanentlyDeleteNotes(notes) }
114 }
115 deletion = nil
116 } label: {
117 if case .all = pending { Text("Empty Trash") }
118 else { Text("Delete Permanently") }
119 }
120 } message: { _ in
121 Text("Permanently deleted notes can’t be restored.")
122 }
123 .alert("Jotley", isPresented: Binding(get: { errorMessage != nil }, set: { if !$0 { errorMessage = nil } })) {
124 Button("OK") { errorMessage = nil }
125 } message: {
126 if let errorMessage { Text(errorMessage) }
127 }
128 }
129
130 private var isEmptyingTrash: Bool {
131 if case .all = deletion { return true }
132 return false
133 }
134
135 private func trashRow(for note: Note) -> some View {
136 HStack(spacing: 12) {
137 VStack(alignment: .leading, spacing: 4) {
138 Text(note.titleOrUntitled)
139 .font(.headline)
140 if let date = note.timestamp {
141 Text(date, format: .dateTime.month(.abbreviated).day().year())
142 .font(.caption)
143 .foregroundStyle(Color.themeTextSecondary)
144 }
145 }
146 .frame(maxWidth: .infinity, alignment: .leading)
147 Menu {
148 Button {
149 perform { try store.restoreNote(note) }
150 } label: {
151 Label("Restore", systemImage: "arrow.uturn.backward")
152 }
153 Button(role: .destructive) {
154 deletion = .one(note)
155 } label: {
156 Label("Delete Permanently", systemImage: "trash")
157 }
158 } label: {
159 Image(systemName: "ellipsis")
160 .font(.body.weight(.semibold))
161 }
162 .buttonStyle(JotleyGlassButtonStyle(circular: true, diameter: 36))
163 .accessibilityLabel(note.titleOrUntitled)
164 .accessibilityIdentifier("notesTrashActions")
165 }
166 .padding(.vertical, 10)
167 }
168
169 private func perform(_ action: () throws -> Void) {
170 do { try action() }
171 catch { errorMessage = error.localizedDescription }
172 }
173}