-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac9fc97
commit bbf29d9
Showing
5 changed files
with
164 additions
and
0 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
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
55 changes: 55 additions & 0 deletions
55
Xcode/BluetoothAccessoryKit/Model/AccessoryManagerSpotlight.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,55 @@ | ||
// | ||
// AccessoryManagerSpotlight.swift | ||
// BluetoothAccessoryKit | ||
// | ||
// Created by Alsey Coleman Miller on 3/4/24. | ||
// | ||
|
||
import Foundation | ||
import BluetoothAccessory | ||
import CoreSpotlight | ||
|
||
#if canImport(CoreSpotlight) && os(iOS) || os(macOS) | ||
internal extension AccessoryManager { | ||
|
||
func updateSpotlight() async { | ||
guard SpotlightController.isSupported else { return } | ||
do { try await spotlight.reindexAll(Array(cache.values)) } | ||
catch { log("⚠️ Unable to update Spotlight: \(error.localizedDescription)") } | ||
} | ||
} | ||
|
||
// MARK: - CoreSpotlightSearchable | ||
|
||
extension PairedAccessory: CoreSpotlightSearchable { | ||
|
||
public static var searchDomain: String { return "com.colemancda.BluetoothAccessory.Spotlight.Accessory" } | ||
|
||
public var searchIdentifier: String { | ||
return type(of: self).searchIdentifier(for: id) | ||
} | ||
|
||
public static func searchIdentifier(for accessory: UUID) -> String { | ||
AccessoryURL.accessory(accessory).rawValue | ||
} | ||
|
||
public func searchableAttributeSet() -> CSSearchableItemAttributeSet { | ||
let attributeSet = CSSearchableItemAttributeSet(itemContentType: Swift.type(of: self).itemContentType) | ||
attributeSet.displayName = name | ||
attributeSet.contentDescription = information.type.description | ||
attributeSet.version = information.softwareVersion.description | ||
//attributeSet.thumbnailData = Data() | ||
attributeSet.keywords = [ | ||
information.id.uuidString, | ||
information.service.description, | ||
information.name, | ||
information.manufacturer, | ||
information.model, | ||
key.permission.type.localizedText | ||
] | ||
return attributeSet | ||
} | ||
} | ||
|
||
#endif | ||
|
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,87 @@ | ||
// | ||
// CoreSpotlight.swift | ||
// BluetoothAccessoryKit | ||
// | ||
// Created by Alsey Coleman Miller on 4/4/24. | ||
// Copyright © 2024 ColemanCDA. All rights reserved. | ||
// | ||
|
||
#if canImport(CoreSpotlight) && os(iOS) || os(macOS) | ||
import Foundation | ||
import CoreSpotlight | ||
|
||
#if canImport(MobileCoreServices) | ||
import MobileCoreServices | ||
#endif | ||
|
||
#if canImport(UIKit) | ||
import UIKit | ||
#elseif canImport(AppKit) | ||
import AppKit | ||
#endif | ||
|
||
/// Manage the Spotlight index. | ||
public final class SpotlightController { | ||
|
||
// MARK: - Initialization | ||
|
||
public init(index: CSSearchableIndex = .default()) { | ||
self.index = index | ||
} | ||
|
||
// MARK: - Properties | ||
|
||
internal let index: CSSearchableIndex | ||
|
||
public var log: ((String) -> ())? | ||
|
||
/// Returns a Boolean value that indicates whether indexing is available on the current device. | ||
public static var isSupported: Bool { | ||
return CSSearchableIndex.isIndexingAvailable() | ||
} | ||
|
||
// MARK: - Methods | ||
|
||
public func reindexAll<T: CoreSpotlightSearchable>( | ||
_ items: [T] | ||
) async throws { | ||
|
||
let searchableItems = items | ||
.map { $0.searchableItem() } | ||
|
||
try await index.deleteSearchableItems(withDomainIdentifiers: [T.searchDomain]) | ||
log?("Deleted all old items") | ||
try await index.indexSearchableItems(Array(searchableItems)) | ||
log?("Indexed \(searchableItems.count) items") | ||
} | ||
} | ||
|
||
// MARK: - Supporting Types | ||
|
||
public protocol CoreSpotlightSearchable { | ||
|
||
static var itemContentType: String { get } | ||
|
||
static var searchDomain: String { get } | ||
|
||
var searchIdentifier: String { get } | ||
|
||
func searchableItem() -> CSSearchableItem | ||
|
||
func searchableAttributeSet() -> CSSearchableItemAttributeSet | ||
} | ||
|
||
public extension CoreSpotlightSearchable { | ||
|
||
static var itemContentType: String { return UTType.text.identifier } | ||
|
||
func searchableItem() -> CSSearchableItem { | ||
let attributeSet = searchableAttributeSet() | ||
return CSSearchableItem( | ||
uniqueIdentifier: searchIdentifier, | ||
domainIdentifier: type(of: self).searchDomain, | ||
attributeSet: attributeSet | ||
) | ||
} | ||
} | ||
#endif |