Save note content when editor disappears

Extract persistNote() from saveNote() to allow saving without closing
  the editor. Call persistNote() from onDisappear (when notch retracts)
  and cancelEdit() so user changes are never silently discarded.
  Empty new notes (no title, no content, no image) are skipped.
This commit is contained in:
SSylvain1989 2026-03-03 21:27:46 +01:00
commit c385eae0c7

View file

@ -102,6 +102,9 @@ struct NotchNotesView: View {
updateLayoutState() updateLayoutState()
} }
.onDisappear { .onDisappear {
if isEditingNewNote || selectedNoteId != nil {
persistNote()
}
coordinator.notesLayoutState = .list coordinator.notesLayoutState = .list
} }
.onChange(of: isEditingNewNote) { _, _ in .onChange(of: isEditingNewNote) { _, _ in
@ -197,48 +200,55 @@ struct NotchNotesView: View {
isEditingNewNote = false isEditingNewNote = false
} }
private func saveNote() { private func persistNote() {
guard let id = editorNoteId else { return }
let isExistingNote = savedNotes.contains(where: { $0.id == id })
if !isExistingNote && editorTitle.isEmpty && editorContent.isEmpty && editorImageData == nil {
return
}
var notes = savedNotes var notes = savedNotes
let now = Date() let now = Date()
if let id = editorNoteId { var fileName: String? = nil
// Priority: Save image to disk if it exists if let data = editorImageData {
var fileName: String? = nil let name = "note_image_\(id.uuidString).png"
if let data = editorImageData { let fileURL = NoteItem.noteImageDataDirectory.appendingPathComponent(name)
let name = "note_image_\(id.uuidString).png" try? data.write(to: fileURL)
let fileURL = NoteItem.noteImageDataDirectory.appendingPathComponent(name) fileName = name
try? data.write(to: fileURL) }
fileName = name
if let index = notes.firstIndex(where: { $0.id == id }) {
// Update
if let oldFileName = notes[index].imageFileName, oldFileName != fileName {
let oldFileURL = NoteItem.noteImageDataDirectory.appendingPathComponent(oldFileName)
try? FileManager.default.removeItem(at: oldFileURL)
} }
if let index = notes.firstIndex(where: { $0.id == id }) { notes[index].title = editorTitle
// Update notes[index].content = editorContent
// If there was an old image and it's being replaced or removed, delete old file notes[index].imageFileName = fileName
if let oldFileName = notes[index].imageFileName, oldFileName != fileName { notes[index].colorIndex = editorColorIndex
let oldFileURL = NoteItem.noteImageDataDirectory.appendingPathComponent(oldFileName) } else {
try? FileManager.default.removeItem(at: oldFileURL) // Create
} let newNote = NoteItem(
id: id,
notes[index].title = editorTitle title: editorTitle.isEmpty ? "Untitled Note" : editorTitle,
notes[index].content = editorContent content: editorContent,
notes[index].imageFileName = fileName creationDate: now,
notes[index].colorIndex = editorColorIndex colorIndex: editorColorIndex,
} else { isPinned: false,
// Create imageFileName: fileName
let newNote = NoteItem( )
id: id, notes.insert(newNote, at: 0)
title: editorTitle.isEmpty ? "Untitled Note" : editorTitle,
content: editorContent,
creationDate: now,
colorIndex: editorColorIndex,
isPinned: false,
imageFileName: fileName
)
notes.insert(newNote, at: 0)
}
savedNotes = notes // Persistence fix: save back to @Default
} }
savedNotes = notes
}
private func saveNote() {
persistNote()
closeEditor() closeEditor()
} }
@ -311,6 +321,7 @@ struct NotchNotesView: View {
} }
private func cancelEdit() { private func cancelEdit() {
persistNote()
closeEditor() closeEditor()
} }