-
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 #1168 from Automattic/charlie/1145/find-note-with-tag
Shortcuts: Find note with tag
- Loading branch information
Showing
14 changed files
with
446 additions
and
50 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
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,19 @@ | ||
// | ||
// IntentTag+Helpers.swift | ||
// IntentsExtension | ||
// | ||
// Created by Charlie Scheer on 5/31/24. | ||
// Copyright © 2024 Simperium. All rights reserved. | ||
// | ||
|
||
import Intents | ||
|
||
extension IntentTag { | ||
static func allTags(in coreDataWrapper: ExtensionCoreDataWrapper) throws -> [IntentTag] { | ||
guard let tags = coreDataWrapper.resultsController?.tags() else { | ||
throw IntentsError.couldNotFetchTags | ||
} | ||
|
||
return tags.map({ IntentTag(identifier: $0.simperiumKey, display: $0.name ?? String()) }) | ||
} | ||
} |
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,20 @@ | ||
// | ||
// NSString+Intents.swift | ||
// IntentsExtension | ||
// | ||
// Created by Charlie Scheer on 5/31/24. | ||
// Copyright © 2024 Simperium. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension NSString { | ||
/// Encodes the receiver as a `Tag Hash` | ||
/// | ||
@objc | ||
var byEncodingAsTagHash: String { | ||
precomposedStringWithCanonicalMapping | ||
.lowercased() | ||
.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? self as String | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
IntentsExtension/IntentHandlers/FindNoteWithTagIntentHandler.swift
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,39 @@ | ||
import Intents | ||
|
||
class FindNoteWithTagIntentHandler: NSObject, FindNoteWithTagIntentHandling { | ||
let coreDataWrapper = ExtensionCoreDataWrapper() | ||
|
||
func resolveNote(for intent: FindNoteWithTagIntent, with completion: @escaping (IntentNoteResolutionResult) -> Void) { | ||
if let selectedNote = intent.note { | ||
completion(IntentNoteResolutionResult.success(with: selectedNote)) | ||
return | ||
} | ||
|
||
guard let selectedTag = intent.tag else { | ||
completion(IntentNoteResolutionResult.needsValue()) | ||
return | ||
} | ||
|
||
completion(IntentNoteResolutionResult.resolveIntentNote(forTag: selectedTag, in: coreDataWrapper)) | ||
} | ||
|
||
func provideTagOptionsCollection(for intent: FindNoteWithTagIntent, with completion: @escaping (INObjectCollection<IntentTag>?, (any Error)?) -> Void) { | ||
do { | ||
let tags = try IntentTag.allTags(in: coreDataWrapper) | ||
completion(INObjectCollection(items: tags), nil) | ||
} catch { | ||
completion(nil, error) | ||
} | ||
} | ||
|
||
func handle(intent: FindNoteWithTagIntent, completion: @escaping (FindNoteWithTagIntentResponse) -> Void) { | ||
guard let note = intent.note else { | ||
completion(FindNoteWithTagIntentResponse(code: .failure, userActivity: nil)) | ||
return | ||
} | ||
|
||
let response = FindNoteWithTagIntentResponse(code: .success, userActivity: nil) | ||
response.note = note | ||
completion(response) | ||
} | ||
} |
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 @@ | ||
// This file contains the required class structure to be able to fetch and use core data files in widgets and intents | ||
// We have collapsed the auto generated core data files into a single file as it is unlikely that the files will need to | ||
// be regenerated. Contained in this file is the generated class files Tag+CoreDataClass.swift and Tag+CoreDataProperties.swift | ||
|
||
import Foundation | ||
import CoreData | ||
|
||
@objc(Tag) | ||
public class Tag: SPManagedObject { | ||
|
||
} | ||
|
||
extension Tag { | ||
|
||
@nonobjc public class func fetchRequest() -> NSFetchRequest<Tag> { | ||
return NSFetchRequest<Tag>(entityName: "Tag") | ||
} | ||
|
||
@NSManaged public var index: NSNumber? | ||
@NSManaged public var name: String? | ||
@NSManaged public var share: String? | ||
} |
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
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.