Repository navigation
Expand file tree
/
Copy pathNotesView.swift
More file actions
231 lines (202 loc) · 7.09 KB
/
Copy pathNotesView.swift
File metadata and controls
231 lines (202 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import SwiftUI
import CoreData
struct NotesView: View {
@Environment(\.managedObjectContext) private var ctx
@EnvironmentObject private var persistence: PersistenceController
@EnvironmentObject private var loc: LocalizationManager
@State private var selectionID: NSManagedObjectID?
@State private var query: String = ""
@State private var showSettings = false
// Tri CoreData: createdAt desc + id desc (tie-breaker stable)
@FetchRequest private var notes: FetchedResults<NoteItem>
init() {
_notes = FetchRequest<NoteItem>(
sortDescriptors: [
NSSortDescriptor(key: "createdAt", ascending: false),
NSSortDescriptor(key: "id", ascending: false) // UUID
],
predicate: nil,
animation: nil
)
}
var body: some View {
NavigationSplitView {
List(selection: $selectionID) {
Section {
noteRows()
} header: {
Text(loc.tr("notes.section.title"))
}
}
.applyPlatformListStyle()
.navigationTitle(loc.tr("tab.notes"))
.searchable(text: $query, placement: .sidebar, prompt: Text(loc.tr("notes.search")))
.transaction { $0.animation = nil }
.toolbar {
ToolbarItemGroup(placement: .primaryAction) {
Button(action: createNoteSafe) { Image(systemName: "plus") }
.keyboardShortcut("n", modifiers: [.command])
.help(Text(loc.tr("notes.new")))
.disabled(!persistence.isLoaded)
Button { showSettings = true } label: { Image(systemName: "gearshape") }
.help(Text(loc.tr("app.settings")))
}
}
// ✅ important : stabilise l’ordre en rendant id/createdAt non-nil
.onAppear { backfillStableSortFieldsIfNeeded() }
.onChange(of: notes.count) { _, _ in
backfillStableSortFieldsIfNeeded()
}
} detail: {
if let selectionID,
let note = try? ctx.existingObject(with: selectionID) as? NoteItem {
NoteDetailView(note: note, selectionID: $selectionID)
.id(selectionID)
} else {
NotesEmptyView()
}
}
.sheet(isPresented: $showSettings) {
SettingsView()
.environmentObject(loc)
}
.tint(.primary)
}
// MARK: - Rows
@ViewBuilder
private func noteRows() -> some View {
let q = query.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
let filtered: [NoteItem] = notes.filter { note in
if q.isEmpty { return true }
let hay = [
effectiveTitle(for: note),
(note.noteText ?? "")
].joined(separator: " ").lowercased()
return hay.contains(q)
}
// ✅ IMPORTANT : identité stable = objectID
ForEach(filtered, id: \.objectID) { note in
NoteRow(note: note)
.tag(note.objectID)
.swipeDeleteActions {
delete(note)
}
}
}
private func effectiveTitle(for note: NoteItem) -> String {
let typed = (note.typedTitle ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
if !typed.isEmpty { return typed }
let recognized = (note.recognizedTitle ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
if !recognized.isEmpty { return recognized }
return ""
}
// MARK: - Backfill to stabilize sorting
private func backfillStableSortFieldsIfNeeded() {
guard persistence.isLoaded else { return }
// On “répare” uniquement si nil (ou createdAt nil), sans toucher au reste.
ctx.perform {
var changed = false
for n in notes {
if n.id == nil {
n.id = UUID()
changed = true
}
if n.createdAt == nil {
// on préfère garder la logique: createdAt = première date connue
n.createdAt = n.updatedAt ?? Date()
changed = true
}
}
guard changed else { return }
do {
try ctx.save()
} catch {
print("❌ backfillStableSortFieldsIfNeeded save error:", error)
}
}
}
// MARK: - CRUD
private func createNoteSafe() {
guard persistence.isLoaded else { return }
ctx.perform {
let n = NoteItem(context: ctx)
n.id = UUID()
n.createdAt = Date()
n.updatedAt = Date()
n.typedTitle = ""
n.recognizedTitle = ""
n.noteText = ""
n.titleDrawingData = nil
n.titleDrawingPreviewPNG = nil
n.bodyDrawingData = nil
n.bodyDrawingPreviewPNG = nil
do {
try ctx.save()
DispatchQueue.main.async { selectionID = n.objectID }
} catch {
print("❌ createNote save error:", error)
}
}
}
private func delete(_ note: NoteItem) {
guard persistence.isLoaded else { return }
ctx.perform {
if selectionID == note.objectID {
DispatchQueue.main.async { selectionID = nil }
}
ctx.delete(note)
do { try ctx.save() }
catch { print("❌ delete note error:", error) }
}
}
}
// MARK: - Empty view
private struct NotesEmptyView: View {
@EnvironmentObject private var loc: LocalizationManager
var body: some View {
VStack(spacing: 10) {
Image(systemName: "note.text")
.font(.system(size: 44, weight: .light))
Text(loc.tr("notes.select"))
.font(.title3)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.thinMaterial)
}
}
// MARK: - Helpers
private extension View {
@ViewBuilder
func swipeDeleteActions(onDelete: @escaping () -> Void) -> some View {
#if os(iOS)
self.swipeActions(edge: .trailing, allowsFullSwipe: true) {
Button(role: .destructive, action: onDelete) {
Image(systemName: "trash")
}
.tint(.red)
}
#elseif os(macOS)
if #available(macOS 13.0, *) {
self.swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button(role: .destructive, action: onDelete) {
Image(systemName: "trash")
}
.tint(.red)
}
} else {
self
}
#else
self
#endif
}
@ViewBuilder
func applyPlatformListStyle() -> some View {
#if os(macOS)
self.listStyle(.sidebar)
#else
self.listStyle(.insetGrouped)
#endif
}
}