Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Search iOS9// File : Components/FaviconView.swift10// Description : Site icon relayed by the engine's server, with a monogram11// while it loads and when there is none.12// Module : Components13// 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 Observation91import SPSearchKit92import SwiftUI93import UIKit9495/// Decoded icons, kept in memory; the bytes come from `FaviconStore` (memory, disk, then the engine's `/v2/favicon`).96@MainActor @Observable97final class FaviconImages {98 @ObservationIgnored private let store: FaviconStore99 @ObservationIgnored private let decoded = NSCache<NSString, UIImage>()100101 init(store: FaviconStore) {102 self.store = store103 decoded.countLimit = 300104 }105106 func cached(_ path: String?) -> UIImage? { path.flatMap { decoded.object(forKey: $0 as NSString) } }107108 func image(_ path: String?) async -> UIImage? {109 guard let path else { return nil }110 if let hit = decoded.object(forKey: path as NSString) { return hit }111 guard let data = await store.data(path: path), let image = UIImage(data: data), image.size.width >= 1 else { return nil }112 decoded.setObject(image, forKey: path as NSString)113 return image114 }115116 func clear() async {117 decoded.removeAllObjects()118 await store.removeAll()119 }120}121122struct FaviconView: View {123 let path: String?124 /// Site name or domain: its first letter makes the monogram.125 let name: String126 var size: CGFloat = 20127 @Environment(FaviconImages.self) private var images128 @State private var image: UIImage?129130 var body: some View {131 ZStack {132 if let image = image ?? images.cached(path) {133 Image(uiImage: image).resizable().interpolation(.high).scaledToFit()134 .frame(width: size, height: size)135 .clipShape(RoundedRectangle(cornerRadius: size * 0.22, style: .continuous))136 } else {137 Monogram(name: name, size: size)138 }139 }140 .frame(width: size, height: size)141 .accessibilityHidden(true)142 .task(id: path) { image = await images.image(path) }143 }144}145146/// First letter of the site on a quiet tint chosen by the name: the same site always gets the same one.147struct Monogram: View {148 let name: String149 var size: CGFloat = 20150151 private static let tints: [(Color, Color)] = [152 (Color(light: 0xE6E2EE, dark: 0x29253D), Color(light: 0x40356E, dark: 0xCBC3EE)),153 (Color(light: 0xDDE6EE, dark: 0x1A2938), Color(light: 0x22456A, dark: 0xA9C6E6)),154 (Color(light: 0xF4DCCF, dark: 0x3A2219), Color(light: 0x8A2A12, dark: 0xF2A688)),155 (Color(light: 0xE1EBDD, dark: 0x1D2C1B), Color(light: 0x2D5728, dark: 0xB4D6AE)),156 (Color(light: 0xF3E4CC, dark: 0x38280F), Color(light: 0x774406, dark: 0xECC690)),157 (Color(light: 0xDBE8E7, dark: 0x152B2C), Color(light: 0x164D50, dark: 0xA4D2D3)),158 (Color(light: 0xF0DEE5, dark: 0x371A26), Color(light: 0x77294A, dark: 0xECB6CB)),159 ]160161 static func letter(_ name: String) -> String {162 var n = name.lowercased()163 if n.hasPrefix("www.") { n.removeFirst(4) }164 return n.first(where: { $0.isLetter || $0.isNumber }).map { String($0).uppercased() } ?? "·"165 }166167 /// Stable across launches (`hashValue` is not).168 static func tintIndex(_ name: String) -> Int { Int(name.lowercased().unicodeScalars.reduce(UInt32(5381)) { ($0 &* 33) &+ $1.value } % UInt32(tints.count)) }169170 var body: some View {171 let tint = Self.tints[Self.tintIndex(name)]172 Text(verbatim: Self.letter(name))173 .font(.system(size: size * 0.56, weight: .bold, design: .rounded))174 .foregroundStyle(tint.1)175 .frame(width: size, height: size)176 .background(tint.0, in: RoundedRectangle(cornerRadius: size * 0.22, style: .continuous))177 }178}179