From 658a9a579e04e91ff20e0aa9a620220ac771f1d1 Mon Sep 17 00:00:00 2001 From: Charlie Scheer Date: Fri, 18 Oct 2024 15:56:10 -1000 Subject: [PATCH 1/3] Delete notes if they are empty when a user deselects the note --- Simplenote/NoteListViewController.swift | 43 +++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/Simplenote/NoteListViewController.swift b/Simplenote/NoteListViewController.swift index 53d40af67..0ff6b5ea1 100644 --- a/Simplenote/NoteListViewController.swift +++ b/Simplenote/NoteListViewController.swift @@ -562,9 +562,42 @@ extension NoteListViewController: SPTableViewDelegate { } DispatchQueue.main.async { + self.deletePreviouslySelectedNoteIfEmpty(notification: notification) self.refreshPresentedNote() } } + + private func deletePreviouslySelectedNoteIfEmpty(notification: Notification) { + guard shouldCheckForEmptyNote(notification: notification), + let previousSelectedIndex = previousSelectedIndex(from: notification), + let previouslySelectedNote = listController.note(at: previousSelectedIndex), + shouldDeleteNoteAsEmpty(note: previouslySelectedNote) else { + return + } + + delete(previouslySelectedNote) + } + + private func shouldDeleteNoteAsEmpty(note: Note) -> Bool { + note.content == nil || note.content?.isEmpty == true + } + + private func shouldCheckForEmptyNote(notification: Notification) -> Bool { + guard let currentRowSelection = notification.userInfo?["NSTableViewCurrentRowSelectionUserInfoKey"] as? NSIndexSet else { + return false + } + + return currentRowSelection.count == 1 + } + + private func previousSelectedIndex(from notification: Notification) -> Int? { + guard let previousSelectedIndexSet = notification.userInfo?["NSTableViewPreviousRowSelectionUserInfoKey"] as? NSIndexSet, + previousSelectedIndexSet.count == 1 else { + return nil + } + + return previousSelectedIndexSet.firstIndex + } } // MARK: - NSTableViewDataSource @@ -812,14 +845,18 @@ extension NoteListViewController { @IBAction func deleteAction(_ sender: Any) { for note in selectedNotes { - SPTracker.trackListNoteDeleted() - note.deleted = true - CSSearchableIndex.default().deleteSearchableNote(note) + delete(note) } simperium.save() } + func delete(_ note: Note) { + SPTracker.trackListNoteDeleted() + note.deleted = true + CSSearchableIndex.default().deleteSearchableNote(note) + } + @IBAction func deleteFromTrashWasPressed(_ sender: Any) { guard let note = selectedNotes.first else { From d249b6fa6b6d4a131c597d92e065332da8b5e065 Mon Sep 17 00:00:00 2001 From: Charlie Scheer Date: Fri, 18 Oct 2024 16:38:12 -1000 Subject: [PATCH 2/3] Added deleting notes if empty from popout window --- .../NoteEditorViewController+Swift.swift | 19 +++++++++++++++++++ Simplenote/NoteEditorViewController.m | 7 ++----- Simplenote/NoteWindow.swift | 5 +++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Simplenote/NoteEditorViewController+Swift.swift b/Simplenote/NoteEditorViewController+Swift.swift index bdc148cbc..b6968bca3 100644 --- a/Simplenote/NoteEditorViewController+Swift.swift +++ b/Simplenote/NoteEditorViewController+Swift.swift @@ -1,4 +1,5 @@ import Foundation +import CoreSpotlight import SimplenoteFoundation import SimplenoteInterlinks import SimplenoteSearch @@ -74,6 +75,24 @@ extension NoteEditorViewController { clipView.contentInsets.top = SplitItemMetrics.editorContentTopInset scrollView.scrollerInsets.top = SplitItemMetrics.editorScrollerTopInset } + + public func deleteCurrentNoteIfEmpty() { + guard let note, + note.content == nil || note.content?.isEmpty == true else { + return + } + + delete(note: note) + } + + @objc(deleteNote:) + func delete(note noteToDelete: Note) { + + SPTracker.trackEditorNoteDeleted() + noteToDelete.deleted = true + noteActionsDelegate?.editorController(self, deletedNoteWithSimperiumKey: noteToDelete.simperiumKey) + CSSearchableIndex.default().deleteSearchableNote(noteToDelete) + } } // MARK: - Public diff --git a/Simplenote/NoteEditorViewController.m b/Simplenote/NoteEditorViewController.m index 6df290502..8f2d94a42 100644 --- a/Simplenote/NoteEditorViewController.m +++ b/Simplenote/NoteEditorViewController.m @@ -402,11 +402,8 @@ - (IBAction)deleteAction:(id)sender if (noteToDelete.deleted) { continue; } - - [SPTracker trackEditorNoteDeleted]; - noteToDelete.deleted = YES; - [self.noteActionsDelegate editorController:self deletedNoteWithSimperiumKey:noteToDelete.simperiumKey]; - [[CSSearchableIndex defaultSearchableIndex] deleteSearchableNote:noteToDelete]; + + [self deleteNote:noteToDelete]; } [self save]; diff --git a/Simplenote/NoteWindow.swift b/Simplenote/NoteWindow.swift index 4cf5532c2..0f678ad3b 100644 --- a/Simplenote/NoteWindow.swift +++ b/Simplenote/NoteWindow.swift @@ -42,4 +42,9 @@ class NoteWindow: NSWindow { editor.displayNote(note) title = note.titlePreview } + + override func close() { + super.close() + editor.deleteCurrentNoteIfEmpty() + } } From 90c7760201e6d7a034137cee9056a989946c9ff6 Mon Sep 17 00:00:00 2001 From: Charlie Scheer Date: Fri, 18 Oct 2024 18:06:23 -1000 Subject: [PATCH 3/3] Moved user info for tableview notifications to constants --- Simplenote/NSTableView+Simplenote.swift | 10 ++++++++++ Simplenote/NoteListViewController.swift | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Simplenote/NSTableView+Simplenote.swift b/Simplenote/NSTableView+Simplenote.swift index 0e9c9beee..202dceb95 100644 --- a/Simplenote/NSTableView+Simplenote.swift +++ b/Simplenote/NSTableView+Simplenote.swift @@ -76,3 +76,13 @@ extension NSTableView { } } } + +extension NSTableView { + static var currentRowSelectionUserInfoKey: String { + "NSTableViewCurrentRowSelectionUserInfoKey" + } + + static var previousRowSelectionUserInfoKey: String { + "NSTableViewPreviousRowSelectionUserInfoKey" + } +} diff --git a/Simplenote/NoteListViewController.swift b/Simplenote/NoteListViewController.swift index 0ff6b5ea1..db1e9de4a 100644 --- a/Simplenote/NoteListViewController.swift +++ b/Simplenote/NoteListViewController.swift @@ -583,7 +583,7 @@ extension NoteListViewController: SPTableViewDelegate { } private func shouldCheckForEmptyNote(notification: Notification) -> Bool { - guard let currentRowSelection = notification.userInfo?["NSTableViewCurrentRowSelectionUserInfoKey"] as? NSIndexSet else { + guard let currentRowSelection = notification.userInfo?[NSTableView.currentRowSelectionUserInfoKey] as? NSIndexSet else { return false } @@ -591,7 +591,7 @@ extension NoteListViewController: SPTableViewDelegate { } private func previousSelectedIndex(from notification: Notification) -> Int? { - guard let previousSelectedIndexSet = notification.userInfo?["NSTableViewPreviousRowSelectionUserInfoKey"] as? NSIndexSet, + guard let previousSelectedIndexSet = notification.userInfo?[NSTableView.previousRowSelectionUserInfoKey] as? NSIndexSet, previousSelectedIndexSet.count == 1 else { return nil }