SPB Git

Repository Public

SpotlightIndex.swift 6.9 KB 151 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Lou-Ka iOS9// File             : LouKa/Core/System/SpotlightIndex.swift10// Description      : recherche Spotlight — les logements consultés et les11//                    favoris sont indexés sur l'appareil ; toucher un résultat12//                    ouvre la fiche. Rien n'est envoyé à un serveur.13// Module           : LouKa14// Repository       : https://git.spboucher.ai/lou-ka-ios-v315// Project Website  : https://www.lou-ka.com16// Created          : 2026-11-2817// Last Modified    : 2026-11-3018//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author           : Simon-Pierre Boucher24// Position         : Professor of Finance25// Institution      : Université du Québec en Outaouais (UQO)26// Department       : Département des sciences administratives27// Organization     : SP Boucher28//29// Website          : https://spboucher.ai30// Git              : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General          : info@spboucher.ai37// Professional     : contact@spboucher.ai38// Direct           : simon@spboucher.ai39// Research         : research@spboucher.ai40// Development      : dev@spboucher.ai41// Support          : support@spboucher.ai42// Security/Admin   : admin@spboucher.ai43// Notifications    : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification   : Public70// Distribution     : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License          : None specified (no license file in this repository)84// Maintainer       : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import CoreSpotlight92import Foundation93import UniformTypeIdentifiers9495enum SpotlightIndex {96    static let domain = "com.spboucher.louka.v2.logements"97    static let favoritesDomain = "com.spboucher.louka.v2.favoris"9899    /// Attributs d'un logement pour Spotlight (logique pure, testée).100    static func attributes(title: String, place: String, unitType: String, price: Double?, source: String) -> CSSearchableItemAttributeSet {101        let a = CSSearchableItemAttributeSet(contentType: .content)102        a.title = title103        var parts: [String] = []104        if let price, Listing.priceIsPlausible(price) { parts.append("\(Fmt.money(price)) par mois") }105        if !unitType.isEmpty { parts.append(unitType) }106        if !place.isEmpty { parts.append(place) }107        a.contentDescription = parts.joined(separator: " · ")108        a.keywords = ["logement", "à louer", "loyer", "Lou-Ka", unitType, place, source].filter { !$0.isEmpty }109        return a110    }111112    static func item(_ l: Listing, favorite: Bool) -> CSSearchableItem {113        let a = attributes(title: l.displayTitle, place: l.place, unitType: l.unitType, price: l.plausiblePrice, source: l.source)114        a.contentURL = l.publicURL115        let item = CSSearchableItem(uniqueIdentifier: l.uid, domainIdentifier: favorite ? favoritesDomain : domain, attributeSet: a)116        // un logement consulté reste trouvable 30 jours ; un favori, tant qu'il est favori117        item.expirationDate = favorite ? .distantFuture : Date().addingTimeInterval(30 * 86400)118        return item119    }120121    static func index(_ l: Listing, favorite: Bool) {122        guard CSSearchableIndex.isIndexingAvailable() else { return }123        CSSearchableIndex.default().indexSearchableItems([item(l, favorite: favorite)]) { _ in }124    }125126    /// Favori reçu d'un autre appareil : indexé à partir de son aperçu.127    static func index(favorite f: FavoriteItem) {128        guard CSSearchableIndex.isIndexingAvailable() else { return }129        let a = attributes(title: f.displayTitle, place: f.place, unitType: f.unitType, price: f.lastPrice ?? f.price, source: f.source)130        var allowed = CharacterSet.urlPathAllowed; allowed.remove(charactersIn: "/?#")131        a.contentURL = URL(string: "https://www.lou-ka.com/logement/" + (f.uid.addingPercentEncoding(withAllowedCharacters: allowed) ?? f.uid))132        let item = CSSearchableItem(uniqueIdentifier: f.uid, domainIdentifier: favoritesDomain, attributeSet: a)133        item.expirationDate = .distantFuture134        CSSearchableIndex.default().indexSearchableItems([item]) { _ in }135    }136137    static func remove(uid: String) {138        CSSearchableIndex.default().deleteSearchableItems(withIdentifiers: [uid]) { _ in }139    }140141    static func removeAll() {142        CSSearchableIndex.default().deleteSearchableItems(withDomainIdentifiers: [domain, favoritesDomain]) { _ in }143    }144145    /// Identifiant du logement porté par l'activité « résultat Spotlight ouvert ».146    static func uid(from activity: NSUserActivity) -> String? {147        guard activity.activityType == CSSearchableItemActionType else { return nil }148        return activity.userInfo?[CSSearchableItemActivityIdentifier] as? String149    }150}151
Files main
Menu

Theme