SPB Git

Repository Public

ListingCard.swift 14.5 KB 322 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Immo-Ka iOS9// File             : ImmoKa/Design/ListingCard.swift10// Description      : carte de propriété — photo, verdict du vrai prix, prix11//                    demandé, faits clés ; variantes grille et ligne ; ligne12//                    d'instantané pour les favoris et les fiches récentes.13// Module           : ImmoKa14// Repository       : https://git.spboucher.ai/immo-ka-ios-v315// Project Website  : https://www.immo-ka.com16// Created          : 2026-11-2517// Last Modified    : 2026-11-3018//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 FavoriteButton: View {94    let listing: Listing95    var size: CGFloat = 3896    @Environment(Library.self) private var library97    @Environment(AppModel.self) private var model9899    var body: some View {100        let fav = library.isFavorite(listing.uid)101        GlassIconButton(system: fav ? "heart.fill" : "heart", tint: fav ? Theme.heart : Theme.ink, size: size,102                        label: fav ? "Retirer des favoris" : "Ajouter aux favoris") {103            let added = library.toggleFavorite(listing)104            if added { Haptics.success() } else { Haptics.tap() }105            model.showToast(added ? "Ajoutée à vos favoris" : "Retirée des favoris")106        }107        .heartBurst(fav)108        .accessibilityIdentifier("favorite.button")109    }110}111112/// Prix demandé en grand, ou « Prix sur demande » quand la source n'en publie pas de plausible.113struct PriceText: View {114    let price: Double?115    var size: CGFloat = 24116117    var body: some View {118        if let p = price, Listing.priceIsPlausible(p) {119            Text(Fmt.money(p)).font(.display(size, weight: .semibold)).tracking(-0.6).num().foregroundStyle(Theme.ink)120                .lineLimit(1).minimumScaleFactor(0.7)121                .accessibilityLabel("\(Fmt.money(p))")122        } else {123            Text("Prix sur demande").font(.subheadline.weight(.medium)).foregroundStyle(Theme.ink2)124        }125    }126}127128/// Faits clés d'une propriété : chambres, salles de bain, superficie (ou terrain).129struct ListingFacts: View {130    let listing: Listing131    var limit = 3132133    static func items(_ l: Listing) -> [(icon: String, text: String, label: String)] {134        var out: [(String, String, String)] = []135        if let b = l.bedrooms, b > 0 { out.append(("bed.double", "\(b) ch.", "\(b) chambre\(b > 1 ? "s" : "")")) }136        if let b = l.bathrooms, b > 0 {137            let p = l.powderRooms.flatMap { $0 > 0 ? " + \($0)" : nil } ?? ""138            out.append(("shower", "\(b)\(p) sdb", "\(b) salle\(b > 1 ? "s" : "") de bain"))139        }140        if let a = l.areaSqft { out.append(("ruler", "\(Fmt.int(a)) pi²", "\(Fmt.int(a)) pieds carrés habitables")) }141        if l.areaSqft == nil || l.isLand, let lot = Fmt.area(l.lotSqft) { out.append(("square.dashed", "terrain \(lot)", "terrain de \(lot)")) }142        if out.count < 3, let y = l.yearBuilt { out.append(("calendar", String(y), "construite en \(y)")) }143        return out144    }145146    var body: some View {147        let items = ListingFacts.items(listing)148        if !items.isEmpty {149            HStack(spacing: 12) {150                ForEach(items.prefix(limit), id: \.text) { item in151                    Label(item.text, systemImage: item.icon).labelStyle(.titleAndIcon).lineLimit(1)152                        .accessibilityLabel(item.label)153                }154            }155            .font(.caption)156            .foregroundStyle(Theme.ink3)157        }158    }159}160161struct ListingCard: View {162    enum Layout { case grid, row }163    let listing: Listing164    var layout: Layout = .grid165    @Environment(Library.self) private var library166    @Environment(AppModel.self) private var model167    @Environment(\.cardPressed) private var pressed168    @Environment(\.accessibilityReduceMotion) private var reduceMotion169170    var body: some View {171        Group {172            switch layout {173            case .grid: grid174            case .row: row175            }176        }177        .background(Theme.surface, in: RoundedRectangle(cornerRadius: Theme.radius, style: .continuous))178        .overlay(RoundedRectangle(cornerRadius: Theme.radius, style: .continuous).strokeBorder(Theme.line, lineWidth: 1))179        .clipShape(RoundedRectangle(cornerRadius: Theme.radius, style: .continuous))180        .contentShape(RoundedRectangle(cornerRadius: Theme.radius, style: .continuous))181        .accessibilityElement(children: .combine)182        .accessibilityHint("Ouvre la fiche de la propriété")183    }184185    private var photo: some View {186        RemoteImage(source: listing.images.first, width: .small, placeholder: PropertyKind.icon(listing.kind))187            // sous le doigt : la photo se rapproche de 3,5 % et la petite maison de la marque glisse dans le188            // coin, porte à 46° — l'annonce de la transition qui suit189            .scaleEffect(pressed && !reduceMotion ? 1.035 : 1)190            .overlay(alignment: .bottomLeading) {191                BrandMark(angle: pressed ? BrandGeometry.cardAngle : 0, onDark: true)192                    .frame(width: 28, height: 28)193                    .padding(6)194                    .background(Theme.charcoal.opacity(0.82), in: RoundedRectangle(cornerRadius: Theme.radiusTag, style: .continuous))195                    .padding(10)196                    .opacity(pressed ? 1 : 0)197                    .offset(y: pressed || reduceMotion ? 0 : 8)198                    .accessibilityHidden(true)199            }200            .animation(.spring(response: 0.3, dampingFraction: 0.7), value: pressed)201            .overlay(alignment: .topLeading) {202                HStack(spacing: 6) {203                    VerdictPill(verdict: listing.verdict, deviation: layout == .grid ? listing.deviation : nil, onPhoto: true, glyphOnly: layout != .grid)204                    if library.seenIDs.contains(listing.uid) {205                        Text("Vue").font(.system(size: 11.5, weight: .semibold)).foregroundStyle(Theme.ink2)206                            .padding(.horizontal, 9).padding(.vertical, 5).background(Theme.surface, in: RoundedRectangle(cornerRadius: Theme.radiusTag, style: .continuous))207                    }208                }209                .padding(10)210            }211            .overlay(alignment: .topTrailing) { FavoriteButton(listing: listing, size: 36).padding(8) }212    }213214    private var info: some View {215        VStack(alignment: .leading, spacing: 6) {216            HStack(alignment: .firstTextBaseline) {217                PriceText(price: listing.price, size: layout == .row ? 20 : 23)218                Spacer(minLength: 6)219                if !listing.kind.isEmpty {220                    Text(listing.kind).font(.caption.weight(.semibold)).foregroundStyle(Theme.ink2).lineLimit(1)221                }222            }223            Text(listing.displayTitle).font(.subheadline.weight(.medium)).foregroundStyle(Theme.ink).lineLimit(1)224            Text(listing.place).font(.footnote).foregroundStyle(Theme.ink3).lineLimit(1)225            ListingFacts(listing: listing)226            if layout == .grid {227                Divider().overlay(Theme.line).padding(.top, 4)228                HStack {229                    Text(listing.agency ?? model.sourceName(listing.source)).lineLimit(1)230                    Spacer()231                    if let fs = listing.firstSeen { Text(Fmt.ago(fs)) }232                }233                .font(.caption2).foregroundStyle(Theme.ink3)234            }235        }236        .padding(layout == .row ? 12 : 14)237    }238239    private var grid: some View {240        VStack(spacing: 0) {241            // le cadre 4:3 fixe la hauteur ; la photo le remplit sans jamais l'agrandir242            Color.clear.aspectRatio(4 / 3, contentMode: .fit).frame(maxWidth: .infinity).overlay { photo }.clipped()243            info244        }245    }246247    private var row: some View {248        HStack(spacing: 0) {249            photo.frame(width: 132).frame(minHeight: 118).clipped()250            info.frame(maxWidth: .infinity, alignment: .leading)251        }252        .fixedSize(horizontal: false, vertical: true)253    }254}255256struct ListingCardSkeleton: View {257    var body: some View {258        VStack(alignment: .leading, spacing: 10) {259            Skeleton(height: 170, radius: 0)260            VStack(alignment: .leading, spacing: 9) {261                Skeleton(height: 22).frame(width: 130)262                Skeleton(height: 14).frame(maxWidth: 220)263                Skeleton(height: 12).frame(width: 150)264            }265            .padding(.horizontal, 14).padding(.bottom, 14)266        }267        .background(Theme.surface, in: RoundedRectangle(cornerRadius: Theme.radius, style: .continuous))268        .overlay(RoundedRectangle(cornerRadius: Theme.radius, style: .continuous).strokeBorder(Theme.line, lineWidth: 1))269        .clipShape(RoundedRectangle(cornerRadius: Theme.radius, style: .continuous))270    }271}272273/// Ligne d'instantané (favori, fiche récente) : affichable hors ligne, ouvre la fiche par la porte.274struct SnapshotRow<Trailing: View>: View {275    let item: any ListingSnapshot276    var caption: String?277    var origin: DoorOrigin = .favorite278    @ViewBuilder var trailing: () -> Trailing279    @Environment(AppModel.self) private var app280    @Environment(DoorCenter.self) private var door281    @Environment(\.accessibilityReduceMotion) private var reduceMotion282    @Environment(\.dynamicTypeSize) private var typeSize283    @State private var frame: CGRect = .zero284285    var body: some View {286        HStack(spacing: 12) {287            Button {288                Haptics.tap()289                let uid = item.uid290                door.open(uid: uid, image: item.image, from: CGRect(x: frame.minX, y: frame.minY, width: 96, height: frame.height),291                          origin: origin, reduceMotion: reduceMotion) { app.open(.listing(uid)) }292            } label: {293                HStack(spacing: 12) {294                    RemoteImage(source: item.image, width: .small, placeholder: PropertyKind.icon(item.propertyType))295                        .frame(width: 96, height: 78)296                        .clipShape(RoundedRectangle(cornerRadius: Theme.radiusSmall, style: .continuous))297                    VStack(alignment: .leading, spacing: 3) {298                        PriceText(price: item.price, size: 18)299                        // aux grandes tailles de texte, l'adresse et les faits passent sur deux lignes plutôt que d'être coupés300                        Text(item.title.isEmpty ? "Propriété à vendre" : item.title).font(.subheadline.weight(.medium)).foregroundStyle(Theme.ink).lineLimit(typeSize.isAccessibilitySize ? 2 : 1)301                        Text(item.place).font(.footnote).foregroundStyle(Theme.ink3).lineLimit(typeSize.isAccessibilitySize ? 2 : 1)302                        if !item.facts.isEmpty { Text(item.facts).font(.caption).foregroundStyle(Theme.ink3).lineLimit(typeSize.isAccessibilitySize ? 2 : 1) }303                        if let caption { Text(caption).font(.caption2).foregroundStyle(Theme.ink3).lineLimit(1) }304                    }305                    Spacer(minLength: 0)306                }307                .contentShape(Rectangle())308            }309            .buttonStyle(PressScaleStyle())310            .onGeometryChange(for: CGRect.self) { $0.frame(in: .global) } action: { frame = $0 }311            trailing()312        }313        .accessibilityElement(children: .contain)314    }315}316317extension SnapshotRow where Trailing == EmptyView {318    init(item: any ListingSnapshot, caption: String? = nil, origin: DoorOrigin = .favorite) {319        self.init(item: item, caption: caption, origin: origin) { EmptyView() }320    }321}322
Files main
Menu

Theme