// Auteur : Simon-Pierre Boucher — contact@spboucher.ai // Ecosystem.swift — la source de vérité des 13 univers Groupe-KA dans l'app : // identité (nom, accent, symbole, tagline) + configuration d'API (endpoint de // liste, clé des items, mapping JSON → KAItem). Ajouter un univers = ajouter // une entrée ici, rien d'autre (voir docs/ARCHITECTURE.md). import SwiftUI // MARK: - JSON générique (les API des univers ont chacune leur forme) enum JSONValue: Decodable { case string(String), number(Double), bool(Bool) case object([String: JSONValue]), array([JSONValue]), null init(from decoder: Decoder) throws { let c = try decoder.singleValueContainer() if c.decodeNil() { self = .null } else if let b = try? c.decode(Bool.self) { self = .bool(b) } else if let n = try? c.decode(Double.self) { self = .number(n) } else if let s = try? c.decode(String.self) { self = .string(s) } else if let a = try? c.decode([JSONValue].self) { self = .array(a) } else { self = .object(try c.decode([String: JSONValue].self)) } } var string: String? { if case .string(let s) = self { return s }; return nil } var number: Double? { if case .number(let n) = self { return n }; return nil } var array: [JSONValue]? { if case .array(let a) = self { return a }; return nil } var object: [String: JSONValue]? { if case .object(let o) = self { return o }; return nil } /// Texte « au mieux » (string, nombre formaté, liste jointe) var text: String? { switch self { case .string(let s): return s.isEmpty ? nil : s case .number(let n): return n == n.rounded() ? String(Int(n)) : String(n) case .array(let a): let parts = a.compactMap(\.text); return parts.isEmpty ? nil : parts.joined(separator: ", ") default: return nil } } } extension [String: JSONValue] { func str(_ keys: String...) -> String? { for k in keys { if let v = self[k]?.text { return v } } return nil } func num(_ keys: String...) -> Double? { for k in keys { if let v = self[k]?.number { return v } } return nil } } // MARK: - L'élément universel struct KAItem: Identifiable, Hashable, Codable { var id: String var universeID: String var title: String var subtitle: String? var priceLabel: String? var city: String? var url: URL? var imageURL: URL? /// Galerie complète (les sites en ont souvent plusieurs) var imageURLs: [URL] = [] var latitude: Double? var longitude: Double? /// Description longue (fiche) var detail: String? /// Paires libres affichées sur la fiche (« Année : 2021 », « Salaire : 65 000 $ »…) var facts: [Fact] /// Liens riches (boutons) — ex. les comptes d'un créateur var links: [Fact] = [] /// Étiquettes de la fiche complète (commodités, caractéristiques, cuisines…) /// — optionnelles : les favoris/historique sérialisés avant leur ajout /// doivent continuer à se décoder var tags: [String]? = nil /// Résumé « en bref » de la fiche (digest du site) var brief: String? = nil /// Prix NUMÉRIQUE (tri, gamme de prix du profil, hypothèque) — priceLabel /// reste l'affichage. Optionnel : les favoris sérialisés avant restent lisibles. var price: Double? = nil /// Date ISO « yyyy-MM-dd » (événements Sorti·Ka — regroupement temporel) var date: String? = nil struct Fact: Hashable, Codable { var label: String; var value: String } } // MARK: - Univers struct Universe: Identifiable { let id: String let wordmark: String // « Lou·Ka » let name: String // « Lou-KA » let tagline: String let accentHex: String let symbol: String // SF Symbol let domain: String let unit: String // « logements », « offres »… /// Chemin de la liste (nil = univers sans liste native, ex. groupe-ka) let listPath: String? let itemsKey: String let searchParam: String let statTotalKeys: [String] // clés du total dans /api/stats let map: (@Sendable ([String: JSONValue]) -> KAItem?)? var accent: Color { Color(hex: accentHex) } var baseURL: URL { URL(string: "https://\(domain)")! } } enum Ecosystem { static let hubURL = URL(string: "https://www.groupe-ka.com")! static let signupURL = URL(string: "https://www.groupe-ka.com/connexion")! static let statusURL = URL(string: "https://www.groupe-ka.com/status")! static let contacts: [(email: String, role: String)] = [ ("contact@groupe-ka.com", "Projets, partenariats & données"), ("info@groupe-ka.com", "Médias & questions générales"), ("admin@groupe-ka.com", "Légal, vie privée & Loi 25"), ] static let legal: [(label: String, path: String)] = [ ("Conditions d'utilisation", "/conditions"), ("Politique de confidentialité", "/confidentialite"), ("Renseignements personnels (Loi 25)", "/loi-25"), ("Transparence des robots", "/bots"), ] static let disclaimer = "Groupe KA est un agrégateur de contenu : nous ne vendons rien, ne louons rien et ne sommes partie à aucune transaction. Données lues à la source — rien d'inventé, tout est traçable." static func universe(_ id: String) -> Universe? { all.first { $0.id == id } } // Aides de mapping communes private static func base(_ o: [String: JSONValue], universe: String, idKeys: [String] = ["uid"]) -> KAItem { var id: String? for k in idKeys { if let v = o[k]?.text { id = v; break } } // galerie : champ `images` (liste) ou `image` — jusqu'à 10, http seulement var gallery: [URL] = (o["images"]?.array ?? []) .compactMap { $0.text } .filter { $0.hasPrefix("http") } .prefix(10) .compactMap(URL.init(string:)) if gallery.isEmpty, let single = o.str("image", "image_url", "photo", "thumbnail").flatMap(URL.init(string:)) { gallery = [single] } let image = gallery.first return KAItem( id: "\(universe):\(id ?? UUID().uuidString)", universeID: universe, title: o.str("title", "name", "display_name") ?? "Sans titre", subtitle: nil, priceLabel: nil, city: o.str("city"), url: o.str("url").flatMap(URL.init(string:)), imageURL: image, imageURLs: gallery, latitude: o.num("lat", "latitude"), longitude: o.num("lng", "lon", "longitude"), detail: o.str("description", "menu_summary", "bio").map { $0.strippingHTML.trimmingCharacters(in: .whitespacesAndNewlines) }, facts: [] ) } // MARK: les 12 univers (+ le hub, sans liste) static let all: [Universe] = [ Universe(id: "lou-ka", wordmark: "Lou·Ka", name: "Lou-KA", tagline: "Tous les logements à louer", accentHex: "#ff6a00", symbol: "key.fill", domain: "www.lou-ka.com", unit: "logements", listPath: "/api/listings", itemsKey: "listings", searchParam: "q", statTotalKeys: ["total"], map: { o in var it = base(o, universe: "lou-ka") it.subtitle = [o.str("unit_type"), o.str("address")].compactMap { $0 }.joined(separator: " · ") if let p = o.num("price"), p > 50 { it.priceLabel = p.money0 + "/mois"; it.price = p } it.facts = [ o.str("unit_type").map { .init(label: "Taille", value: $0) }, o.str("address").map { .init(label: "Adresse", value: $0) }, o.str("available_by").map { .init(label: "Disponible", value: $0) }, o.num("area_sqft").flatMap { $0 > 50 ? KAItem.Fact(label: "Superficie", value: "\(Int($0)) pi²") : nil }, o.str("source").map { .init(label: "Gestionnaire", value: $0) }, ].compactMap { $0 } return it }), Universe(id: "immo-ka", wordmark: "Immo·Ka", name: "Immo-KA", tagline: "Toutes les propriétés à vendre", accentHex: "#e23744", symbol: "house.fill", domain: "www.immo-ka.com", unit: "propriétés", listPath: "/api/listings", itemsKey: "listings", searchParam: "q", statTotalKeys: ["total"], map: { o in var it = base(o, universe: "immo-ka") it.subtitle = [o.str("property_type"), o.str("region")].compactMap { $0 }.joined(separator: " · ") if let p = o.num("price"), p > 1000 { it.priceLabel = p.money0; it.price = p } it.facts = [ o.num("bedrooms").map { .init(label: "Chambres", value: String(Int($0))) }, o.num("bathrooms").map { .init(label: "Salles de bain", value: String(Int($0))) }, o.num("area_sqft").flatMap { $0 > 50 ? KAItem.Fact(label: "Superficie", value: "\(Int($0)) pi²") : nil }, o.str("address").map { .init(label: "Adresse", value: $0) }, o.str("source").map { .init(label: "Source", value: $0) }, ].compactMap { $0 } return it }), Universe(id: "vrai-prix", wordmark: "Vrai-Prix", name: "Vrai-Prix", tagline: "La valeur réelle de chaque propriété", accentHex: "#ff5148", symbol: "chart.line.uptrend.xyaxis", domain: "www.vrai-prix.com", unit: "propriétés estimées", listPath: nil, itemsKey: "", searchParam: "q", statTotalKeys: ["units_total"], map: nil), Universe(id: "auto-ka", wordmark: "Auto·Ka", name: "Auto-KA", tagline: "Les voitures usagées du Québec", accentHex: "#ff5a2a", symbol: "car.fill", domain: "www.auto-ka.com", unit: "véhicules", listPath: "/api/vehicles", itemsKey: "vehicles", searchParam: "q", statTotalKeys: ["total"], map: { o in var it = base(o, universe: "auto-ka") it.subtitle = [o.num("year").map { String(Int($0)) }, o.str("mileage_label")].compactMap { $0 }.joined(separator: " · ") it.priceLabel = o.str("price_label") ?? o.num("price").map(\.money0) it.price = o.num("price") it.facts = [ o.str("make").map { .init(label: "Marque", value: $0) }, o.str("model").map { .init(label: "Modèle", value: $0) }, o.str("transmission").map { .init(label: "Boîte", value: $0) }, o.str("fuel").map { .init(label: "Carburant", value: $0) }, o.str("drivetrain").map { .init(label: "Rouage", value: $0) }, o.str("body_type").map { .init(label: "Carrosserie", value: $0) }, o.str("dealer_name").map { .init(label: "Concessionnaire", value: $0) }, o.str("mileage_label").map { .init(label: "Kilométrage", value: $0) }, ].compactMap { $0 } return it }), Universe(id: "fabri-ka", wordmark: "Fabri·Ka", name: "Fabri-KA", tagline: "Les produits fabriqués au Québec", accentHex: "#c4532e", symbol: "shippingbox.fill", domain: "www.fabri-ka.com", unit: "produits", listPath: "/api/products", itemsKey: "items", searchParam: "q", statTotalKeys: ["totals.products", "total"], map: { o in var it = base(o, universe: "fabri-ka") it.subtitle = o.str("store_id", "store") it.priceLabel = o.str("price_label") ?? o.num("price").map(\.money2) it.price = o.num("price") it.facts = [o.str("category").map { .init(label: "Catégorie", value: $0) }].compactMap { $0 } return it }), Universe(id: "food-ka", wordmark: "Food·Ka", name: "Food-KA", tagline: "Les prix d'épicerie, suivis à la source", accentHex: "#1f9d55", symbol: "cart.fill", domain: "www.food-ka.com", unit: "produits", listPath: "/api/products", itemsKey: "products", searchParam: "q", statTotalKeys: ["total"], map: { o in var it = base(o, universe: "food-ka") it.subtitle = [o.str("brand"), o.str("size_label")].compactMap { $0 }.joined(separator: " · ") let onSale: Bool = { if case .bool(true) = o["on_sale"] ?? .null { return true }; return false }() if let p = o.num("price"), p > 0.2 { if onSale, let reg = o.num("regular_price"), reg > p { it.priceLabel = "\(p.money2) 🏷️ (rég. \(reg.money2))" } else { it.priceLabel = p.money2 } it.price = p } it.facts = [ o.str("category").map { .init(label: "Catégorie", value: $0) }, o.num("unit_price").flatMap { $0 > 0.001 ? KAItem.Fact(label: "Prix unitaire", value: $0.money2) : nil }, onSale ? KAItem.Fact(label: "Solde", value: "Oui 🏷️") : nil, ].compactMap { $0 } return it }), Universe(id: "resto-ka", wordmark: "Resto·Ka", name: "Resto-KA", tagline: "Chaque resto, chaque plat, chaque prix", accentHex: "#f08c00", symbol: "fork.knife", domain: "www.resto-ka.com", unit: "restaurants", listPath: "/api/restaurants", itemsKey: "restaurants", searchParam: "q", statTotalKeys: ["restaurants", "total"], map: { o in var it = base(o, universe: "resto-ka") let cuisines = o["cuisines"]?.text it.subtitle = [cuisines, o.str("price_range")].compactMap { $0 }.joined(separator: " · ") it.facts = [ o.str("address").map { .init(label: "Adresse", value: $0) }, o.str("chain").map { .init(label: "Chaîne", value: $0) }, ].compactMap { $0 } return it }), Universe(id: "sorti-ka", wordmark: "Sorti·Ka", name: "Sorti-KA", tagline: "Toutes les sorties, dans les 17 régions", accentHex: "#d6336c", symbol: "ticket.fill", domain: "www.sorti-ka.com", unit: "événements", listPath: "/api/events?upcoming=true", itemsKey: "events", searchParam: "q", statTotalKeys: ["total_active", "total"], map: { o in var it = base(o, universe: "sorti-ka") it.date = o.str("start_date") it.subtitle = [o.str("start_date"), o.str("venue")].compactMap { $0 }.joined(separator: " · ") it.facts = [ o.str("start_date").map { .init(label: "Début", value: $0) }, o.str("end_date").map { .init(label: "Fin", value: $0) }, o.str("venue").map { .init(label: "Lieu", value: $0) }, o.str("region").map { .init(label: "Région", value: $0) }, (o["is_free"].flatMap { if case .bool(true) = $0 { return KAItem.Fact(label: "Entrée", value: "Gratuite 🎉") } ; return nil }), o.num("price_min").flatMap { $0 > 1 ? KAItem.Fact(label: "Billets dès", value: $0.money0) : nil }, ].compactMap { $0 } return it }), Universe(id: "crea-ka", wordmark: "Créa·Ka", name: "Créa-KA", tagline: "Les créateurs d'ici, tous leurs liens", accentHex: "#7048e8", symbol: "sparkles", domain: "www.crea-ka.com", unit: "créateurs", listPath: "/api/creators", itemsKey: "items", searchParam: "q", statTotalKeys: ["creators", "total"], map: { o in var it = base(o, universe: "crea-ka", idKeys: ["cid", "uid", "display_name"]) let plats = o["platforms"]?.array?.compactMap(\.object) ?? [] let totalFollowers = plats.compactMap { $0.num("followers") }.reduce(0, +) it.subtitle = [o["niches"]?.text, totalFollowers > 0 ? totalFollowers.compact + " abonnés" : nil] .compactMap { $0 }.joined(separator: " · ") if it.url == nil { it.url = plats.first?.str("url").flatMap(URL.init(string:)) } it.facts = plats.prefix(6).compactMap { pl in guard let name = pl.str("platform") else { return nil } let f = pl.num("followers").map { $0.compact } ?? "" return KAItem.Fact(label: name.capitalized, value: f.isEmpty ? "@" + (pl.str("handle") ?? "") : f + " abonnés") } it.links = plats.prefix(6).compactMap { pl in guard let name = pl.str("platform"), let url = pl.str("url") else { return nil } return KAItem.Fact(label: name.capitalized, value: url) } return it }), Universe(id: "job-ka", wordmark: "Job·Ka", name: "Job-KA", tagline: "Tous les emplois des employeurs québécois", accentHex: "#0c8599", symbol: "briefcase.fill", domain: "www.job-ka.com", unit: "offres d'emploi", listPath: "/api/jobs", itemsKey: "jobs", searchParam: "q", statTotalKeys: ["total"], map: { o in var it = base(o, universe: "job-ka") it.subtitle = [o.str("employer"), o.str("city")].compactMap { $0 }.joined(separator: " · ") let sMin = o.num("salary_year_min"), sMax = o.num("salary_year_max") if let a = sMin, a > 20000 { it.priceLabel = (sMax != nil && sMax! > a) ? "\(a.money0)–\(sMax!.money0)/an" : a.money0 + "/an" it.price = a } else if let h = o.num("salary_hour_min"), h > 10 { it.priceLabel = h.money2 + "/h" } it.facts = [ o.str("employer").map { .init(label: "Employeur", value: $0) }, o.str("work_mode").map { .init(label: "Mode de travail", value: $0) }, o.str("employment_type").map { .init(label: "Type", value: $0) }, o.str("ats").map { .init(label: "Plateforme carrière", value: $0) }, o.str("date_posted").map { .init(label: "Publiée le", value: String($0.prefix(10))) }, ].compactMap { $0 } return it }), Universe(id: "trouve-ka", wordmark: "Trouve·Ka", name: "Trouve-KA", tagline: "Le moteur de recherche du web québécois", accentHex: "#1c7ed6", symbol: "magnifyingglass", domain: "www.trouve-ka.com", unit: "pages indexées", listPath: "/api/search", itemsKey: "results", searchParam: "q", statTotalKeys: ["pages_indexed"], map: { o in var it = base(o, universe: "trouve-ka", idKeys: ["url"]) it.title = (o.str("title") ?? "Page web").strippingHTML it.subtitle = o.str("snippet")?.strippingHTML it.facts = [o.str("domain").map { .init(label: "Domaine", value: $0) }].compactMap { $0 } return it }), Universe(id: "api-ka", wordmark: "API·Ka", name: "API-KA", tagline: "La donnée de l'écosystème, par API", accentHex: "#3b5bdb", symbol: "terminal.fill", domain: "www.api-ka.com", unit: "appels API", listPath: nil, itemsKey: "", searchParam: "q", statTotalKeys: ["total"], map: nil), ] } // MARK: - petites extensions extension Double { /// 17 100 000 → « 17,1 M », 5 200 → « 5,2 k » var compact: String { if self >= 1_000_000 { return String(format: "%.1f M", self / 1_000_000).replacingOccurrences(of: ".", with: ",") } if self >= 10_000 { return String(format: "%.0f k", self / 1_000) } if self >= 1_000 { return String(format: "%.1f k", self / 1_000).replacingOccurrences(of: ".", with: ",") } return String(Int(self)) } var money0: String { let f = NumberFormatter(); f.numberStyle = .currency f.locale = Locale(identifier: "fr_CA"); f.maximumFractionDigits = 0 return f.string(from: NSNumber(value: self)) ?? "\(Int(self)) $" } var money2: String { let f = NumberFormatter(); f.numberStyle = .currency f.locale = Locale(identifier: "fr_CA"); f.maximumFractionDigits = 2 return f.string(from: NSNumber(value: self)) ?? "\(self) $" } } extension String { var strippingHTML: String { replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression) .replacingOccurrences(of: "&", with: "&") .replacingOccurrences(of: " ", with: " ") } } extension Color { init(hex: String) { var h = hex.trimmingCharacters(in: .alphanumerics.inverted) if h.count == 3 { h = h.map { "\($0)\($0)" }.joined() } let v = UInt64(h, radix: 16) ?? 0 self.init(.sRGB, red: Double((v >> 16) & 0xFF) / 255, green: Double((v >> 8) & 0xFF) / 255, blue: Double(v & 0xFF) / 255) } }