SPB Git

Repository Public

PlumbChoreo.swift 12.5 KB 281 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Vrai-Prix iOS9// File             : VraiPrix/Design/Plumb/PlumbChoreo.swift10// Description      : le geste « d'aplomb » — chorégraphie (fonction pure du11//                    temps), niveaux, choix automatique. Le fil à plomb tombe,12//                    oscille lentement (deux balancements bien visibles),13//                    s'immobilise net ; sa pointe désigne la valeur sur la14//                    jauge ; le prix et l'écart n'apparaissent qu'ensuite.15// Module           : VraiPrix16// Repository       : https://git.spboucher.ai/vrai-prix-ios-v317// Project Website  : https://www.vrai-prix.com18// Created          : 2026-11-2619// Last Modified    : 2026-11-2820//21// =============================================================================22// AUTHOR & PROFESSIONAL AFFILIATION23// =============================================================================24//25// Author           : Simon-Pierre Boucher26// Position         : Professor of Finance27// Institution      : Université du Québec en Outaouais (UQO)28// Department       : Département des sciences administratives29// Organization     : SP Boucher30//31// Website          : https://spboucher.ai32// Git              : https://git.spboucher.ai33//34// =============================================================================35// OFFICIAL CONTACT DIRECTORY36// =============================================================================37//38// General          : info@spboucher.ai39// Professional     : contact@spboucher.ai40// Direct           : simon@spboucher.ai41// Research         : research@spboucher.ai42// Development      : dev@spboucher.ai43// Support          : support@spboucher.ai44// Security/Admin   : admin@spboucher.ai45// Notifications    : notifications@spboucher.ai46//47// Preferred Contact: support@spboucher.ai48//49// =============================================================================50// COPYRIGHT & INTELLECTUAL PROPERTY51// =============================================================================52//53// Copyright (c) 202654// Simon-Pierre Boucher55//56// All rights reserved where applicable.57//58// This file is governed by the applicable project license,59// copyright notices, institutional agreements and relevant law.60//61// Unless expressly authorized by the applicable license or62// rights holder, copying, redistribution, sublicensing,63// publication and commercial exploitation are not permitted.64//65// Existing third-party rights and notices remain unaffected.66//67// =============================================================================68// CONFIDENTIALITY & SECURITY69// =============================================================================70//71// Classification   : Public72// Distribution     : Public repository; use is governed by the project license73// Security Contact : admin@spboucher.ai74//75// Access to this source does not independently grant permission76// to redistribute or disclose confidential materials.77//78// Do not embed credentials, private keys, access tokens,79// certificates or confidential personal information.80//81// =============================================================================82// LICENSE & MAINTENANCE83// =============================================================================84//85// License          : None specified (no license file in this repository)86// Maintainer       : Simon-Pierre Boucher87// Technical Contact: dev@spboucher.ai88//89// =============================================================================90// END OF SP BOUCHER SOURCE HEADER91// =============================================================================9293import Foundation94import Observation95import SwiftUI9697/// Table des temps du geste, en millisecondes depuis l'ouverture. Version native : plus lente et plus ample que98/// le prototype web (1 440 ms), à la demande du propriétaire (≈ 1,85 s, plomb et fil plus visibles).99struct PlumbChoreo: Sendable, Equatable {100    struct Span: Sendable, Equatable {101        var a: Double102        var b: Double103        /// progression linéaire bornée à [0, 1]104        func at(_ t: Double) -> Double { b <= a ? (t >= b ? 1 : 0) : min(1, max(0, (t - a) / (b - a))) }105    }106107    var hero: Span        // la photo (ou le pignon filaire) prend place108    var descent: Span     // le fil se déroule, le plomb au bout109    var sol: Span         // la ligne de base se trace du centre vers les bords110    var ticks: Span       // graduations111    var range: Span       // fourchette112    var price: Span       // losange du prix demandé ou vendu113    var value: Span       // le chiffre se pose114    var cote: Span        // la cote d'écart se tire de l'aplomb vers le prix115    var rows: Span        // lignes sous le chiffre116    var retract: Span     // le fil s'efface de la photo117    // pendule : θ(t) = A · exp(−(t − t0) / τ) · cos(2π (t − t0) / T) · (1 − lissage(arrêt))118    var amplitude: Double119    var t0: Double120    var period: Double121    var tau: Double122    var stop: Span123    var total: Double124125    /// Instant où le plomb s'immobilise (retour haptique).126    var settle: Double { stop.b }127128    static let signature = PlumbChoreo(129        hero: .init(a: 0, b: 380), descent: .init(a: 260, b: 760), sol: .init(a: 620, b: 1040), ticks: .init(a: 840, b: 1140),130        range: .init(a: 920, b: 1260), price: .init(a: 1500, b: 1680), value: .init(a: 1600, b: 1850), cote: .init(a: 1640, b: 1850),131        rows: .init(a: 1700, b: 1850), retract: .init(a: 1700, b: 1850),132        amplitude: 9, t0: 340, period: 600, tau: 650, stop: .init(a: 1440, b: 1660), total: 1850)133134    static let short = PlumbChoreo(135        hero: .init(a: 0, b: 220), descent: .init(a: 100, b: 380), sol: .init(a: 240, b: 500), ticks: .init(a: 360, b: 560),136        range: .init(a: 380, b: 600), price: .init(a: 640, b: 780), value: .init(a: 700, b: 900), cote: .init(a: 720, b: 900),137        rows: .init(a: 760, b: 900), retract: .init(a: 780, b: 900),138        amplitude: 4.5, t0: 150, period: 420, tau: 260, stop: .init(a: 560, b: 720), total: 900)139140    // --- Geste 3.0 (système de mouvement V3) : « la réponse d'abord ». Le chiffre s'écrit à l'instant où le plomb141    // touche l'axe ; le plomb finit de s'immobiliser ensuite, rien dans le mouvement ne retient la valeur. Même ordre des142    // pistes que le site (photo, fil, axe, bande, chiffre, graduations, prix, cote), temps allongés d'un quart : sur un143    // téléphone le geste occupe tout l'écran, et le propriétaire le veut lent et lisible. 1 440 ms contre 1 150 ms.144    static let signature3 = PlumbChoreo(145        hero: .init(a: 0, b: 300), descent: .init(a: 240, b: 640), sol: .init(a: 450, b: 700), ticks: .init(a: 700, b: 980),146        range: .init(a: 600, b: 900), price: .init(a: 880, b: 1080), value: .init(a: 640, b: 820), cote: .init(a: 1000, b: 1280),147        rows: .init(a: 640, b: 860), retract: .init(a: 1200, b: 1440),148        amplitude: 7, t0: 300, period: 520, tau: 620, stop: .init(a: 1140, b: 1340), total: 1440)149150    static let short3 = PlumbChoreo(151        hero: .init(a: 0, b: 180), descent: .init(a: 120, b: 360), sol: .init(a: 270, b: 420), ticks: .init(a: 400, b: 580),152        range: .init(a: 350, b: 550), price: .init(a: 500, b: 630), value: .init(a: 360, b: 470), cote: .init(a: 550, b: 730),153        rows: .init(a: 360, b: 500), retract: .init(a: 650, b: 780),154        amplitude: 3.5, t0: 160, period: 420, tau: 280, stop: .init(a: 560, b: 690), total: 780)155156    /// Angle du pendule en degrés ; positif = écarté vers la droite. Nul, exactement, à partir de `stop.b`.157    func angle(_ t: Double) -> Double {158        guard t >= t0 else { return amplitude }159        let u = stop.at(t)160        let s = u * u * (3 - 2 * u)161        return amplitude * exp(-(t - t0) / tau) * cos(2 * .pi * (t - t0) / period) * (1 - s)162    }163164    /// Nombre de passages par la verticale avant l'arrêt (deux par balancement complet).165    var crossings: Int {166        var n = 0167        var t = t0 + period / 4168        while t < stop.b { n += 1; t += period / 2 }169        return n170    }171172    // courbes : cubic-bezier(0.5, 0, 0.2, 1) pour la descente, (0.2, 0.7, 0.2, 1) pour ce qui se pose173    static func easePlumb(_ x: Double) -> Double { bezier(x, 0.5, 0, 0.2, 1) }174    static func easeOut(_ x: Double) -> Double { bezier(x, 0.2, 0.7, 0.2, 1) }175176    /// cubic-bezier(x1, y1, x2, y2) évaluée en x (Newton, puis dichotomie).177    static func bezier(_ x: Double, _ x1: Double, _ y1: Double, _ x2: Double, _ y2: Double) -> Double {178        if x <= 0 { return 0 }179        if x >= 1 { return 1 }180        func f(_ t: Double, _ a: Double, _ b: Double) -> Double { 3 * a * t * (1 - t) * (1 - t) + 3 * b * t * t * (1 - t) + t * t * t }181        var lo = 0.0, hi = 1.0, t = x182        for _ in 0..<24 {183            let v = f(t, x1, x2)184            if abs(v - x) < 1e-5 { break }185            if v < x { lo = t } else { hi = t }186            t = (lo + hi) / 2187        }188        return f(t, y1, y2)189    }190}191192enum PlumbLevel: String, Sendable {193    case signature, short, none194    var choreo: PlumbChoreo? {195        switch self {196        case .signature: .signature197        case .short: .short198        case .none: nil199        }200    }201}202203/// D'où vient l'ouverture d'une fiche : décide du niveau du geste.204enum PlumbTrigger: Sendable {205    /// depuis une carte de propriété (liste, carte, favoris)206    case card207    /// révélation d'une estimation demandée (recherche d'adresse) : le moment du produit208    case reveal209    /// lien direct : jamais d'animation210    case direct211}212213/// Réglage « Animations » de l'app et compteur de la session.214@MainActor215@Observable216final class MotionCenter {217    enum Preference: String, CaseIterable, Identifiable, Sendable {218        case auto, discrete, off219        var id: String { rawValue }220        var label: String {221            switch self {222            case .auto: "Automatiques"223            case .discrete: "Discrètes"224            case .off: "Aucune"225            }226        }227    }228229    var preference: Preference {230        didSet { UserDefaults.standard.set(preference.rawValue, forKey: "motion") }231    }232233    /// ouvertures de fiche jouées en version signature pendant la session234    private(set) var signaturePlayed = 0235    /// instant figé du geste, en ms (captures : `-VPPlumbT 900`)236    let frozenT: Double?237    private let forced: PlumbLevel?238    /// ce que la prochaine fiche ouverte doit jouer (posé au toucher d'une carte ou d'un résultat de recherche)239    private var pending: [String: PlumbTrigger] = [:]240241    init() {242        preference = Preference(rawValue: UserDefaults.standard.string(forKey: "motion") ?? "") ?? .auto243        let d = UserDefaults.standard244        frozenT = d.object(forKey: "VPPlumbT") == nil ? nil : d.double(forKey: "VPPlumbT")245        forced = switch d.string(forKey: "VPPlumb") {246        case "signature": .signature247        case "courte": .short248        case "aucune": PlumbLevel.none249        default: nil250        }251    }252253    func arm(_ key: String, _ trigger: PlumbTrigger) { pending[key] = trigger }254255    /// Déclencheur d'une fiche qui s'ouvre (lien direct par défaut) ; consommé à la lecture.256    func consume(_ key: String) -> PlumbTrigger { pending.removeValue(forKey: key) ?? .direct }257258    /// Niveau à jouer. Pur : `MotionCenter.level(...)` est testé seul.259    nonisolated static func level(trigger: PlumbTrigger, preference: Preference, reduceMotion: Bool, signaturePlayed: Int) -> PlumbLevel {260        if reduceMotion || preference == .off { return .none }261        switch trigger {262        case .direct: return .none263        case .reveal: return preference == .discrete ? .short : .signature264        case .card:265            if preference == .discrete { return .short }266            return signaturePlayed < 3 ? .signature : .short267        }268    }269270    func level(for trigger: PlumbTrigger, reduceMotion: Bool) -> PlumbLevel {271        if let forced { return reduceMotion ? .none : forced }272        if frozenT != nil { return reduceMotion ? .none : .signature }273        let l = MotionCenter.level(trigger: trigger, preference: preference, reduceMotion: reduceMotion, signaturePlayed: signaturePlayed)274        if l == .signature, trigger == .card { signaturePlayed += 1 }275        return l276    }277278    /// Le mouvement d'interface (entrées, tracés) est-il permis ?279    func allowed(reduceMotion: Bool) -> Bool { !reduceMotion && preference != .off }280}281
Files main
Menu

Theme