-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1140 from Automattic/charlie/1000/add-spotlight-s…
…upport Add spotlight support for Simplenote
- Loading branch information
Showing
12 changed files
with
227 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
2.21 | ||
----- | ||
- Added spotlight search support for notes | ||
- Added multiple window support for editing notes | ||
|
||
2.20 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// CSSearchableItem+Helpers.swift | ||
// Simplenote | ||
// | ||
// Created by Michal Kosmowski on 25/11/2016. | ||
// Copyright © 2016 Automattic. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import CoreSpotlight | ||
import UniformTypeIdentifiers | ||
|
||
extension CSSearchableItemAttributeSet { | ||
|
||
convenience init(note: Note) { | ||
self.init(contentType: UTType.data) | ||
note.ensurePreviewStringsAreAvailable() | ||
title = note.titlePreview | ||
contentDescription = note.bodyPreview | ||
} | ||
|
||
} | ||
|
||
extension CSSearchableItem { | ||
|
||
convenience init(note: Note) { | ||
let attributeSet = CSSearchableItemAttributeSet(note: note) | ||
self.init(uniqueIdentifier: note.simperiumKey, domainIdentifier: "notes", attributeSet: attributeSet) | ||
} | ||
|
||
} | ||
|
||
extension CSSearchableIndex { | ||
// MARK: - Index Notes | ||
@objc | ||
func indexSpotlightItems(in context: NSManagedObjectContext) { | ||
guard Options.shared.indexNotesForSpotlight else { | ||
return | ||
} | ||
|
||
context.perform { | ||
if let deleted = try? context.fetchObjects(for: "Note", withPredicate: NSPredicate(format: "deleted == YES")) as? [Note] { | ||
self.deleteSearchableNotes(deleted) | ||
} | ||
|
||
if let notes = try? context.fetchObjects(for: "Note", withPredicate: NSPredicate(format: "deleted == NO")) as? [Note] { | ||
self.indexSearchableNotes(notes) | ||
} | ||
} | ||
} | ||
|
||
@objc func indexSearchableNote(_ note: Note) { | ||
guard Options.shared.indexNotesForSpotlight else { | ||
return | ||
} | ||
|
||
let item = CSSearchableItem(note: note) | ||
indexSearchableItems([item]) { error in | ||
if let error = error { | ||
NSLog("Couldn't index note in spotlight: \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
|
||
@objc func indexSearchableNotes(_ notes: [Note]) { | ||
guard Options.shared.indexNotesForSpotlight else { | ||
return | ||
} | ||
|
||
let items = notes.map { | ||
return CSSearchableItem(note: $0) | ||
} | ||
|
||
indexSearchableItems(items) { error in | ||
if let error = error { | ||
NSLog("Couldn't index notes in spotlight: \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
|
||
@objc func deleteSearchableNote(_ note: Note) { | ||
deleteSearchableNotes([note]) | ||
} | ||
|
||
@objc func deleteSearchableNotes(_ notes: [Note]) { | ||
let ids = notes.compactMap({ $0.simperiumKey }) | ||
|
||
deleteSearchableItems(withIdentifiers: ids) { error in | ||
if let error = error { | ||
NSLog("Couldn't delete notes from spotlight index: \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// NSManagedObjectContext+Simplenote.swift | ||
// Simplenote | ||
// | ||
// Created by Charlie Scheer on 4/11/24. | ||
// Copyright © 2024 Simperium. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
|
||
extension NSManagedObjectContext { | ||
@objc(fetchObjectsForEntityName: withPredicate: error:) | ||
func fetchObjects(for entityName: String, withPredicate predicate: NSPredicate) throws -> Array<NSFetchRequestResult> { | ||
let fetchRequest = NSFetchRequest<NSFetchRequestResult>() | ||
let entityDescription = NSEntityDescription.entity(forEntityName: entityName, in: self) | ||
|
||
fetchRequest.entity = entityDescription | ||
|
||
return try fetch(fetchRequest) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.