SPB Git

Repository Public

Carnet.swift 6.2 KB 169 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Valo-Plex iOS9// File             : ValoPlex/Core/Carnet.swift10// Description      : immeubles consultés récemment et favoris, gardés sur11//                    l'appareil seulement (UserDefaults).12// Module           : ValoPlex13// Repository       : https://git.spboucher.ai/valo-plex-ios14// Project Website  : https://www.valo-plex.com15// 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 Foundation91import Observation9293/// Ce qu'il faut pour réafficher un immeuble dans une liste sans rappeler le serveur.94struct Repere: Codable, Hashable, Identifiable, Sendable {95    let id: String96    var adresse: String97    var municipalite: String98    var logements: Int99    var valeur: Double?100    var statut: String?101102    init(id: String, adresse: String, municipalite: String, logements: Int, valeur: Double?, statut: String?) {103        self.id = id104        self.adresse = adresse105        self.municipalite = municipalite106        self.logements = logements107        self.valeur = valeur108        self.statut = statut109    }110111    init(_ f: Fiche) {112        self.init(id: f.immeuble.id, adresse: Fmt.adresse(f.immeuble.adresse), municipalite: f.immeuble.municipalite ?? "",113                  logements: f.immeuble.logements, valeur: f.valeur?.regime == .abstention ? nil : f.valeur?.valeur, statut: f.valeur?.statut)114    }115116    var valeurCourte: String {117        guard let valeur else { return Fmt.vide }118        return (statut == "ordre" ? "≈ " : "") + Fmt.grand(valeur)119    }120}121122@MainActor123@Observable124final class Carnet {125    private(set) var recents: [Repere] = []126    private(set) var favoris: [Repere] = []127128    @ObservationIgnored private let depot: UserDefaults129    private static let cleRecents = "vx.recents.v1"130    private static let cleFavoris = "vx.favoris.v1"131    static let maxRecents = 12132133    init(depot: UserDefaults = .standard) {134        self.depot = depot135        recents = Self.lire(depot, Self.cleRecents)136        favoris = Self.lire(depot, Self.cleFavoris)137    }138139    private static func lire(_ d: UserDefaults, _ cle: String) -> [Repere] {140        guard let data = d.data(forKey: cle) else { return [] }141        return (try? JSONDecoder().decode([Repere].self, from: data)) ?? []142    }143144    private func ecrire() {145        depot.set(try? JSONEncoder().encode(recents), forKey: Self.cleRecents)146        depot.set(try? JSONEncoder().encode(favoris), forKey: Self.cleFavoris)147    }148149    func consulter(_ r: Repere) {150        recents.removeAll { $0.id == r.id }151        recents.insert(r, at: 0)152        if recents.count > Self.maxRecents { recents.removeLast(recents.count - Self.maxRecents) }153        if let i = favoris.firstIndex(where: { $0.id == r.id }) { favoris[i] = r }154        ecrire()155    }156157    func estFavori(_ id: String) -> Bool { favoris.contains { $0.id == id } }158159    func basculer(_ r: Repere) {160        if estFavori(r.id) { favoris.removeAll { $0.id == r.id } } else { favoris.insert(r, at: 0) }161        ecrire()162    }163164    func effacerRecents() {165        recents = []166        ecrire()167    }168}169
Files main
Menu

Theme