Spotlight Manager

mail@pastecode.io avatar
unknown
swift
a month ago
2.9 kB
2
Indexable
Never
import Foundation
import CoreSpotlight
import UIKit
import CoreData

public protocol HasSpotlightManager {
    var spotlightManager: SpotlightManagerInterface { get }
}

public protocol SpotlightManagerInterface: AnyObject {
    func indexItem(identifier: String, title: String, description: String?, image: UIImage?)
    func deIndexItem(with identifier: String)
}

public protocol SpotlightDependency {
    associatedtype Dependencies
    var dependencies: Dependencies { get }
    var spotlightManager: SpotlightManagerInterface { get }
}

public extension SpotlightDependency {
    private var _dependencies: HasSpotlightManager {
        guard let _dependencies = dependencies as? HasSpotlightManager else {
            fatalError("Presenter `Dependencies` do not conform to `HasSpotlightManager`")
        }
        return _dependencies
    }

    var spotlightManager: SpotlightManagerInterface {
        _dependencies.spotlightManager
    }
}

public class SpotlightManager: SpotlightManagerInterface {
    public static let shared = SpotlightManager()
        
    public func indexItem(identifier: String, title: String, description: String?, image: UIImage?) {
        
        let attributeSet: CSSearchableItemAttributeSet
        
        if let image { // If we have an image add it to hte attribute set
            // We should investigate how to make sure that each data is within the size range
            let jpegData = image.jpegData(compressionQuality: 0.8)
            attributeSet = CSSearchableItemAttributeSet(contentType: .image)
            attributeSet.thumbnailData = jpegData
            // We can also add thumbnail URL instead of Data
            // attributeSet.thumbnailURL = "url.com"
            
        } else { // If we do not have an image, use only text attributes
            attributeSet = CSSearchableItemAttributeSet(contentType: .text)
        }
        
        attributeSet.title = title
        attributeSet.contentDescription = description
        attributeSet.identifier = identifier
        
        // Creating an searchable item that represents e.g. an Article
        let item = CSSearchableItem(uniqueIdentifier: identifier, domainIdentifier: nil, attributeSet: attributeSet)
        
        // We can add an expiration date to the searchable Item
        item.expirationDate = Date.distantFuture
        // Adding the item to the index
        let searchableIndex = CSSearchableIndex.default()
        searchableIndex.indexSearchableItems([item])
    }
    
    public func deIndexItem(with identifier: String) {
        let searchableIndex = CSSearchableIndex.default()
        searchableIndex.deleteSearchableItems(withIdentifiers: [identifier])
    }

}

// MARK: Core Data
class SpotlightManagerCoreData: NSCoreDataCoreSpotlightDelegate {
    
    override func domainIdentifier() -> String {
        ""
    }
    
    override func indexName() -> String? {
        ""
    }
}
Leave a Comment