Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : Valo-Plex iOS9// File : ValoPlex/Features/AccueilVue.swift10// Description : accueil : recherche d'adresse, chiffres clés, favoris,11// immeubles consultés récemment et prix demandés les plus12// bas face à la valeur.13// Module : ValoPlex14// Repository : https://git.spboucher.ai/valo-plex-ios15// Project Website : https://www.valo-plex.com16// Created : 2026-12-0117// Last Modified : 2026-12-0118//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 SwiftUI9293struct AccueilVue: View {94 @Environment(Navigation.self) private var navigation95 @Environment(Carnet.self) private var carnet9697 @State private var requete = ""98 @State private var resultats: [Resultat]?99 @State private var rechercheEnPanne = false100 @State private var modele: Modele?101 @State private var occasions: [Annonce] = []102 @FocusState private var saisie: Bool103104 private var cherche: Bool { requete.trimmingCharacters(in: .whitespaces).count >= 3 }105106 var body: some View {107 Ecran(racine: true, ancre: modele != nil ? navigation.ancre : nil) {108 VStack(alignment: .leading, spacing: 0) {109 Etiquette("Immeubles à revenus du Québec · 2 logements et plus").padding(.top, 28)110 (Text("Ce que vaut un plex, ").font(.serif(34, style: .largeTitle))111 + Text("porte par porte").font(.serifItalique(34, style: .largeTitle)).foregroundStyle(VX.brique)112 + Text(".").font(.serif(34, style: .largeTitle)))113 .foregroundStyle(VX.encre)114 .fixedSize(horizontal: false, vertical: true)115 .padding(.top, 12)116 .accessibilityAddTraits(.isHeader)117 champ.padding(.top, 22)118 if cherche {119 liste120 } else {121 apercu122 }123 }124 }125 .task {126 if !navigation.requeteInitiale.isEmpty, requete.isEmpty { requete = navigation.requeteInitiale }127 if modele == nil { modele = try? await API.modele() }128 if occasions.isEmpty { occasions = (try? await API.annonces(.init(), page: 1, parPage: 3).annonces) ?? [] }129 }130 .task(id: requete) { await chercher() }131 }132133 private var champ: some View {134 VStack(alignment: .leading, spacing: 6) {135 Etiquette("Adresse de l'immeuble")136 HStack(spacing: 8) {137 TextField("", text: $requete, prompt: Text("Ex. : 4521 rue Saint-Denis, Montréal").foregroundStyle(VX.encre3))138 .font(.sans(17))139 .foregroundStyle(VX.encre)140 .textInputAutocapitalization(.never)141 .autocorrectionDisabled()142 .submitLabel(.search)143 .focused($saisie)144 .accessibilityIdentifier("recherche")145 if !requete.isEmpty {146 Button {147 requete = ""148 resultats = nil149 } label: {150 Etiquette("Effacer").frame(minHeight: 30)151 }152 .buttonStyle(.plain)153 .accessibilityLabel("Effacer la recherche")154 .accessibilityIdentifier("effacer")155 }156 }157 .padding(.horizontal, 16)158 .padding(.vertical, 14)159 .background(VX.carton)160 .overlay(Rectangle().strokeBorder(VX.encre, lineWidth: 1.5))161 }162 }163164 @ViewBuilder165 private var liste: some View {166 VStack(alignment: .leading, spacing: 0) {167 if let resultats {168 if resultats.isEmpty {169 Text(rechercheEnPanne170 ? "La recherche n'a pas abouti. Vérifiez la connexion, puis réessayez."171 : "Aucun immeuble de 2 logements et plus à cette adresse. Essayez le numéro et le nom de la rue seulement.")172 .font(.sans(15, style: .subheadline))173 .foregroundStyle(VX.encre2)174 .padding(16)175 } else {176 ForEach(Array(resultats.enumerated()), id: \.element.id) { i, r in177 if i > 0 { Filet(couleur: VX.papier2) }178 NavigationLink(value: Route.immeuble(r.id)) {179 HStack(alignment: .firstTextBaseline, spacing: 12) {180 VStack(alignment: .leading, spacing: 3) {181 Text(Fmt.adresse(r.adresse)).font(.sans(16)).foregroundStyle(VX.encre).multilineTextAlignment(.leading)182 Text("\(r.municipalite ?? "") · \(Fmt.libelleClasse(r.classe, r.logements))")183 .font(.sans(13.5, style: .footnote)).foregroundStyle(VX.encre2)184 }185 Spacer(minLength: 8)186 Text(r.valeur.map { (r.statut == "ordre" ? "≈ " : "") + Fmt.grand($0) } ?? Fmt.vide)187 .font(.mono(14)).foregroundStyle(VX.encre)188 }189 .padding(.horizontal, 16)190 .padding(.vertical, 12)191 .contentShape(Rectangle())192 }193 .buttonStyle(.plain)194 }195 }196 } else {197 Attente(libelle: "Recherche au rôle").padding(.horizontal, 16)198 }199 }200 .frame(maxWidth: .infinity, alignment: .leading)201 .background(VX.carton)202 .overlay(Rectangle().strokeBorder(VX.encre, lineWidth: 1.5))203 .padding(.top, -1.5)204 }205206 @ViewBuilder207 private var apercu: some View {208 Text("Chaque duplex, triplex et immeuble locatif inscrit au rôle d'évaluation reçoit une valeur marchande, apprise sur les prix réellement payés au registre foncier, puis confrontée à ce que l'immeuble rapporte.")209 .font(.sans(16))210 .foregroundStyle(VX.encre2)211 .fixedSize(horizontal: false, vertical: true)212 .padding(.top, 18)213214 Facade(logements: 3, etages: 3, lien: "En rangée plus de 1 côté")215 .frame(maxWidth: 330)216 .frame(maxWidth: .infinity)217 .padding(.top, 22)218 .accessibilityHidden(true)219220 if let m = modele {221 GrilleChiffres(cases: [222 .init(valeur: Fmt.nombre(m.estimation?.immeubles), legende: "immeubles évalués, tous ceux du rôle"),223 .init(valeur: Fmt.nombre(m.estimation?.portes), legende: "logements dans ces immeubles"),224 .init(valeur: Fmt.pourcent(m.modele?.ensemble?.erreurMediane, 1), legende: "d'erreur médiane sur des ventes jamais vues"),225 .init(valeur: Fmt.grand(m.estimation?.valeurTotale), legende: "valeur du parc locatif privé estimée"),226 ])227 .padding(.top, 24)228 .id("chiffres")229 Text("Valeurs au \(Fmt.dateLongue(m.estimation?.dateValeur)), date de la dernière vente connue.")230 .font(.sans(13.5, style: .footnote)).foregroundStyle(VX.encre2).padding(.top, 10)231 }232233 if !carnet.favoris.isEmpty {234 TitreSection(etiquette: "Favoris · gardés sur cet appareil", titre: "Vos immeubles.")235 reperes(carnet.favoris)236 }237 if !carnet.recents.isEmpty {238 TitreSection(etiquette: "Consultés récemment", titre: "Reprendre où vous étiez.")239 reperes(carnet.recents)240 Button("Effacer l'historique") { carnet.effacerRecents() }241 .buttonStyle(.plain)242 .font(.sans(13.5, style: .footnote))243 .foregroundStyle(VX.encre2)244 .underline(true, color: VX.trait)245 .padding(.top, 12)246 }247248 if !occasions.isEmpty {249 TitreSection(etiquette: "Plex à vendre, comparés à leur valeur", titre: "Les prix demandés les plus bas face à la valeur estimée.")250 VStack(spacing: 14) {251 ForEach(occasions) { o in252 if let id = o.id {253 NavigationLink(value: Route.immeuble(id)) { CarteAnnonce(annonce: o, image: false) }.buttonStyle(.plain)254 }255 }256 }257 Button("Voir tous les plex à vendre") { navigation.choisir(.annonces) }258 .buttonStyle(BoutonEncre(clair: true))259 .padding(.top, 18)260 }261 }262263 private func reperes(_ liste: [Repere]) -> some View {264 VStack(spacing: 0) {265 ForEach(Array(liste.enumerated()), id: \.element.id) { i, r in266 if i > 0 { Filet(couleur: VX.papier2) }267 NavigationLink(value: Route.immeuble(r.id)) {268 HStack(alignment: .firstTextBaseline, spacing: 12) {269 VStack(alignment: .leading, spacing: 3) {270 Text(r.adresse).font(.sans(16)).foregroundStyle(VX.encre).multilineTextAlignment(.leading)271 Text("\(r.municipalite) · \(Fmt.logements(r.logements))").font(.sans(13.5, style: .footnote)).foregroundStyle(VX.encre2)272 }273 Spacer(minLength: 8)274 Text(r.valeurCourte).font(.mono(14)).foregroundStyle(VX.encre)275 }276 .padding(.horizontal, 16)277 .padding(.vertical, 12)278 .contentShape(Rectangle())279 }280 .buttonStyle(.plain)281 }282 }283 .background(VX.carton)284 .overlay(Rectangle().strokeBorder(VX.encre, lineWidth: 1))285 }286287 private func chercher() async {288 let q = requete.trimmingCharacters(in: .whitespaces)289 guard q.count >= 3 else {290 resultats = nil291 return292 }293 // courte pause de saisie : une requête par mot tapé, pas par lettre294 try? await Task.sleep(for: .milliseconds(220))295 guard !Task.isCancelled else { return }296 do {297 let r = try await API.recherche(q)298 guard !Task.isCancelled else { return }299 rechercheEnPanne = false300 resultats = r301 } catch is CancellationError {302 } catch {303 guard !Task.isCancelled else { return }304 rechercheEnPanne = true305 resultats = []306 }307 }308}309