Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete note if the content is completely removed #1216

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Simplenote/NSTableView+Simplenote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,13 @@ extension NSTableView {
}
}
}

extension NSTableView {
static var currentRowSelectionUserInfoKey: String {
"NSTableViewCurrentRowSelectionUserInfoKey"
}

static var previousRowSelectionUserInfoKey: String {
"NSTableViewPreviousRowSelectionUserInfoKey"
}
}
19 changes: 19 additions & 0 deletions Simplenote/NoteEditorViewController+Swift.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import CoreSpotlight
import SimplenoteFoundation
import SimplenoteInterlinks
import SimplenoteSearch
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions Simplenote/NoteEditorViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
43 changes: 40 additions & 3 deletions Simplenote/NoteListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?[NSTableView.currentRowSelectionUserInfoKey] as? NSIndexSet else {
return false
}

return currentRowSelection.count == 1
}

private func previousSelectedIndex(from notification: Notification) -> Int? {
guard let previousSelectedIndexSet = notification.userInfo?[NSTableView.previousRowSelectionUserInfoKey] as? NSIndexSet,
previousSelectedIndexSet.count == 1 else {
return nil
}

return previousSelectedIndexSet.firstIndex
}
}

// MARK: - NSTableViewDataSource
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions Simplenote/NoteWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ class NoteWindow: NSWindow {
editor.displayNote(note)
title = note.titlePreview
}

override func close() {
super.close()
editor.deleteCurrentNoteIfEmpty()
}
}