Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Search iOS9// File : Core/Sources/SPSearchKit/FavoritesStore.swift10// Description : Saved results: a JSON file in the app's own folder, on11// this device only.12// Module : SPSearchKit13// Repository : https://git.spboucher.ai/sp-search-ios14// Project Website : https://search.spboucher.ai15// Created : 2026-12-0116// Last Modified : 2026-12-0117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author : Simon-Pierre Boucher23// Position : Professor of Finance24// Institution : Université du Québec en Outaouais (UQO)25// Department : Département des sciences administratives26// Organization : SP Boucher27//28// Website : https://spboucher.ai29// Git : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General : info@spboucher.ai36// Professional : contact@spboucher.ai37// Direct : simon@spboucher.ai38// Research : research@spboucher.ai39// Development : dev@spboucher.ai40// Support : support@spboucher.ai41// Security/Admin : admin@spboucher.ai42// Notifications : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification : Public69// Distribution : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License : None specified (no license file in this repository)83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation9192/// A result the reader chose to keep. It holds what the result card showed, nothing fetched afterwards.93public struct Favorite: Codable, Sendable, Hashable, Identifiable {94 public var url: String95 public var title: String96 public var domain: String97 public var siteName: String?98 public var displayURL: String?99 public var snippet: String100 public var faviconPath: String?101 public var kind: String102 public var savedAt: Date103 public var id: String { url }104 public var link: URL? { SearchResult.webLink(url) }105106 public init(url: String, title: String, domain: String, siteName: String? = nil, displayURL: String? = nil, snippet: String = "",107 faviconPath: String? = nil, kind: String = PageKind.page.rawValue, savedAt: Date) {108 self.url = url; self.title = title; self.domain = domain; self.siteName = siteName; self.displayURL = displayURL109 self.snippet = snippet; self.faviconPath = faviconPath; self.kind = kind; self.savedAt = savedAt110 }111112 public init(_ r: SearchResult, at date: Date = Date()) {113 self.init(url: r.url, title: r.title, domain: r.domain, siteName: r.siteName, displayURL: r.displayURL,114 snippet: String(r.snippet.prefix(400)), faviconPath: r.faviconPath, kind: r.kind.rawValue, savedAt: date)115 }116}117118/// Favorites never leave the device: one file, read and written whole. The newest comes first.119public struct FavoritesStore: Sendable {120 public static let limit = 500121 public let file: URL122123 public init(file: URL = FavoritesStore.defaultFile()) { self.file = file }124125 public static func defaultFile() -> URL {126 let dir = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first ?? FileManager.default.temporaryDirectory127 return dir.appendingPathComponent("SPSearch/favorites.json")128 }129130 public var all: [Favorite] {131 guard let data = try? Data(contentsOf: file) else { return [] }132 let decoder = JSONDecoder()133 decoder.dateDecodingStrategy = .secondsSince1970134 return (try? decoder.decode([Favorite].self, from: data)) ?? []135 }136137 public func contains(_ url: String) -> Bool { all.contains { $0.url == url } }138139 @discardableResult140 public func add(_ favorite: Favorite) -> [Favorite] {141 var list = all.filter { $0.url != favorite.url }142 list.insert(favorite, at: 0)143 return save(Array(list.prefix(Self.limit)))144 }145146 @discardableResult147 public func remove(_ url: String) -> [Favorite] { save(all.filter { $0.url != url }) }148149 @discardableResult150 public func clear() -> [Favorite] { try? FileManager.default.removeItem(at: file); return [] }151152 private func save(_ list: [Favorite]) -> [Favorite] {153 let encoder = JSONEncoder()154 encoder.dateEncodingStrategy = .secondsSince1970155 try? FileManager.default.createDirectory(at: file.deletingLastPathComponent(), withIntermediateDirectories: true)156 if let data = try? encoder.encode(list) { try? data.write(to: file, options: [.atomic, .completeFileProtectionUntilFirstUserAuthentication]) }157 return list158 }159}160