Skip to content

Commit

Permalink
Fixed opening Spotlight item
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Mar 5, 2024
1 parent bbf29d9 commit 2037100
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Xcode/BluetoothAccessory/TabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import SwiftUI
import CoreSpotlight
import SFSafeSymbols
import BluetoothAccessoryKit

Expand Down Expand Up @@ -74,6 +75,7 @@ struct AccessoryTabView: View {
try? await store.wait(for: .poweredOn)
}
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb, perform: handleUserActivity)
.onContinueUserActivity(CSSearchableItemActionType, perform: handleSpotlight)
.onOpenURL(perform: openURL)
.sheet(isPresented: $setupSheet, onDismiss: { self.url = nil }) {
NavigationView {
Expand All @@ -90,6 +92,16 @@ struct AccessoryTabView: View {

private extension AccessoryTabView {

func handleSpotlight(_ userActivity: NSUserActivity) {
guard let activityIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String else {
return
}
guard let accessoryURL = AccessoryURL(rawValue: activityIdentifier) else {
return
}
openURL(accessoryURL)
}

func handleUserActivity(_ userActivity: NSUserActivity) {
guard let url = userActivity.webpageURL else {
return
Expand All @@ -99,6 +111,7 @@ private extension AccessoryTabView {

func openURL(_ url: URL) {
guard let accessoryURL = AccessoryURL(web: url) else {
store.log("Invalid URL \(url)")
return
}
openURL(accessoryURL)
Expand Down
2 changes: 1 addition & 1 deletion Xcode/BluetoothAccessoryKit/Model/AccessoryManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public final class AccessoryManager: ObservableObject {
public lazy var cloudContainer = loadCloudContainer()

#if canImport(CoreSpotlight) && os(iOS) || os(macOS)
internal lazy var spotlight = SpotlightController(index: .default())
internal lazy var spotlight = loadSpotlight()
#endif

#if os(iOS) && !APPCLIP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import CoreSpotlight
#if canImport(CoreSpotlight) && os(iOS) || os(macOS)
internal extension AccessoryManager {

func loadSpotlight() -> SpotlightController {
let spotlight = SpotlightController(index: .default())
spotlight.log = { [unowned self] in self.log("🔦 Spotlight: " + $0) }
return spotlight
}

func updateSpotlight() async {
guard SpotlightController.isSupported else { return }
do { try await spotlight.reindexAll(Array(cache.values)) }
Expand Down

0 comments on commit 2037100

Please sign in to comment.