Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/VQuantUI/Components/VQProvenanceChip.swift3// Description: The chip every data surface carries: freshness kind · provider · as-of. Never the word "live".4import SwiftUI5import VQuantKit67/// The glyph of a freshness kind, readable without colour: ● polled, ◐ delayed, ○ close, ▫ historical, ≈ estimated.8struct VQFreshnessGlyph: View {9 let kind: VQFreshness.Kind?10 var size: CGFloat = 91112 var body: some View {13 Canvas { context, s in14 let u = min(s.width, s.height) / 1015 let t = CGAffineTransform(scaleX: u, y: u)16 let ink = GraphicsContext.Shading.foreground17 switch kind {18 case .polled?: context.fill(Path(ellipseIn: CGRect(x: 1, y: 1, width: 8, height: 8)).applying(t), with: ink)19 case .delayed?:20 context.stroke(Path(ellipseIn: CGRect(x: 1.5, y: 1.5, width: 7, height: 7)).applying(t), with: ink, lineWidth: 1.2 * u)21 context.fill(VQSVGPath.path("M5 1.5a3.5 3.5 0 0 1 0 7Z").applying(t), with: ink)22 case .eod?: context.stroke(Path(ellipseIn: CGRect(x: 1.5, y: 1.5, width: 7, height: 7)).applying(t), with: ink, lineWidth: 1.2 * u)23 case .historical?: context.stroke(Path(CGRect(x: 1.6, y: 1.6, width: 6.8, height: 6.8)).applying(t), with: ink, lineWidth: 1.2 * u)24 case .estimated?: context.stroke(VQSVGPath.path("M1 4c1.5-2 2.5-2 4 0s2.5 2 4 0M1 7.5c1.5-2 2.5-2 4 0s2.5 2 4 0").applying(t), with: ink, lineWidth: 1.1 * u)25 case nil: context.stroke(Path(ellipseIn: CGRect(x: 2, y: 2, width: 6, height: 6)).applying(t), with: ink, style: StrokeStyle(lineWidth: 1.1 * u, dash: [1.5 * u, 1.5 * u]))26 }27 }28 .frame(width: size, height: size)29 .accessibilityHidden(true)30 }31}3233public struct VQProvenanceChip: View {34 private let freshness: VQFreshness35 private let now: Date36 private let timeZone: TimeZone37 @Environment(\.vqTheme) private var theme3839 public init(_ freshness: VQFreshness, now: Date = Date(), timeZone: TimeZone = .current) {40 self.freshness = freshness; self.now = now; self.timeZone = timeZone41 }4243 /// For a surface whose provenance is a provider and a retrieval time (lab data sources).44 public init(kind: VQFreshness.Kind, provider: String?, asOf: Date?, now: Date = Date(), timeZone: TimeZone = .current) {45 self.init(VQFreshness(kind: kind, asOf: asOf, provider: provider), now: now, timeZone: timeZone)46 }4748 private var details: String {49 var parts: [String] = []50 if let k = freshness.kind.known { parts.append(k.explanation(theme.language)) }51 if let r = freshness.retrievedAt { parts.append(theme.l("Retrieved: ") + VQFormat.dateTime(r, language: theme.language, timeZone: timeZone)) }52 if let n = freshness.note, !n.isEmpty { parts.append(n) }53 return parts.joined(separator: " ")54 }5556 public var body: some View {57 let shape = Capsule(style: .continuous)58 HStack(spacing: 4) {59 VQFreshnessGlyph(kind: freshness.kind.known, size: 8)60 Text(freshness.kindLabel(theme.language)).font(.system(size: 10.5, weight: .medium))61 if let p = VQFreshness.providerName(freshness.provider) {62 Text("·").font(.system(size: 10.5)).foregroundStyle(VQP.text3)63 Text(p).font(.system(size: 10.5))64 }65 if let asOf = freshness.asOf {66 Text("·").font(.system(size: 10.5)).foregroundStyle(VQP.text3)67 Text(VQFormat.asOf(asOf, now: now, language: theme.language, timeZone: timeZone)).font(.system(size: 10.5, design: .monospaced).monospacedDigit())68 }69 }70 .foregroundStyle(VQP.text2)71 .padding(.horizontal, 7).padding(.vertical, 2.5)72 .overlay { shape.strokeBorder(VQP.lineStrong, style: StrokeStyle(lineWidth: 1, dash: freshness.kind == .estimated ? [3, 2] : [])) }73 .fixedSize()74 .help(details)75 .accessibilityElement(children: .ignore)76 .accessibilityLabel(freshness.displayText(theme.language, now: now, timeZone: timeZone))77 .accessibilityHint(details)78 }79}80