Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP ID & Card Vault7// File : SPVault/Engines/VirtualCard/VirtualCardView.swift8// Module : SPVault9// Repository : https://git.spboucher.ai/sp-id-card-vault10// Project Website : Not specified11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI4243/// SP Virtual Card Engine — one face of a card.44///45/// The texture is the corrected photograph of the physical card, drawn at the46/// card's true aspect ratio with the corner radius of its format. Premium mode47/// adds light on top (specular sheen, faint holographic film, bevel, shadow); it48/// never alters the photograph. Privacy mode veils the face and shows only the49/// name, the issuer and the last four characters.50struct VirtualCardView: View {51 let card: Card52 var side: CardSide = .front53 /// Thumbnails for lists; the full photograph for detail and full screen.54 var thumbnail = true55 var mode: CardRenderMode = .premium56 var privacy = false57 /// Extra tilt (for instance from a drag), added to the device tilt.58 var manualTilt: CGSize = .zero59 var usesDeviceTilt = false60 var showsShadow = true6162 @Environment(Vault.self) private var vault63 @Environment(AppSettings.self) private var settings64 @Environment(\.accessibilityReduceMotion) private var reduceMotion65 @State private var image: UIImage?6667 private var tilt: CGSize {68 guard mode == .premium, !reduceMotion, settings.motionEffects else { return .zero }69 let device = usesDeviceTilt ? MotionTilt.shared.tilt : .zero70 return CGSize(width: max(-1, min(1, device.width + manualTilt.width)), height: max(-1, min(1, device.height + manualTilt.height)))71 }7273 var body: some View {74 GeometryReader { proxy in75 let size = proxy.size76 let radius = size.width * card.format.cornerRadiusFraction77 let shape = RoundedRectangle(cornerRadius: radius, style: .continuous)78 ZStack {79 face(size: size)80 if privacy { PrivacyVeil(card: card, size: size) }81 }82 .frame(width: size.width, height: size.height)83 .clipShape(shape)84 .modifier(SheenEffect(size: size, tilt: tilt, enabled: mode == .premium))85 .overlay {86 if mode == .premium {87 // Bevel: light catches the top edge, the bottom edge falls away.88 shape.strokeBorder(LinearGradient(colors: [.white.opacity(0.55), .white.opacity(0.08), .black.opacity(0.25)],89 startPoint: .top, endPoint: .bottom), lineWidth: max(0.75, size.width * 0.003))90 .blendMode(.overlay)91 } else {92 shape.strokeBorder(SP.Palette.hairlineStrong, lineWidth: 0.5)93 }94 }95 .compositingGroup()96 .shadow(color: .black.opacity(showsShadow ? (mode == .premium ? 0.38 : 0.2) : 0), radius: size.width * 0.05,97 x: -tilt.width * size.width * 0.02, y: size.width * 0.035 + tilt.height * size.width * 0.01)98 .shadow(color: .black.opacity(showsShadow ? 0.25 : 0), radius: 1.5, y: 1)99 }100 .aspectRatio(card.format.aspectRatio, contentMode: .fit)101 .task(id: LoadKey(id: card.id, side: side, thumbnail: thumbnail, revision: vault.imageRevision, unlocked: vault.isUnlocked)) {102 if let cached = vault.cachedImage(for: card.id, side: side, thumbnail: thumbnail) { image = cached; return }103 if !thumbnail, image == nil { image = vault.cachedImage(for: card.id, side: side, thumbnail: true) }104 let loaded = await vault.image(for: card.id, side: side, thumbnail: thumbnail)105 if let loaded { image = loaded } else if !vault.isUnlocked { image = nil }106 }107 .accessibilityElement(children: .ignore)108 .accessibilityLabel(accessibilityText)109 .accessibilityAddTraits(.isImage)110 }111112 private struct LoadKey: Hashable {113 var id: UUID, side: CardSide, thumbnail: Bool, revision: Int, unlocked: Bool114 }115116 private var accessibilityText: String {117 var parts = [card.name]118 if !card.issuer.isEmpty { parts.append(card.issuer) }119 if !card.last4.isEmpty { parts.append("ending in \(card.last4.map(String.init).joined(separator: " "))") }120 if side == .back { parts.append("back") }121 return parts.joined(separator: ", ")122 }123124 @ViewBuilder125 private func face(size: CGSize) -> some View {126 if let image {127 Image(uiImage: image)128 .resizable()129 .interpolation(.high)130 .aspectRatio(contentMode: .fill)131 .frame(width: size.width, height: size.height)132 .blur(radius: privacy ? size.width * 0.07 : 0, opaque: true)133 .transition(.opacity)134 } else {135 CardPlaceholderFace(card: card, size: size)136 }137 }138}139140/// Applies the Metal sheen shader. Kept as a modifier so that list thumbnails in141/// Original mode pay nothing for it.142struct SheenEffect: ViewModifier {143 var size: CGSize144 var tilt: CGSize145 var enabled: Bool146147 func body(content: Content) -> some View {148 if enabled, size.width > 0 {149 content.colorEffect(ShaderLibrary.cardSheen(.float2(size.width, size.height),150 .float2(tilt.width, tilt.height), .float(1)))151 } else {152 content153 }154 }155}156157/// Shown while the photograph decrypts, or when it is missing: a quiet face in158/// the card's dominant colour. It never imitates the real card.159struct CardPlaceholderFace: View {160 let card: Card161 var size: CGSize162163 var body: some View {164 let base = Color(card.dominantColor)165 let onDark = card.dominantColor.luminance < 0.6166 ZStack(alignment: .bottomLeading) {167 LinearGradient(colors: [base.opacity(0.95), base.mix(with: .black, by: 0.35)], startPoint: .topLeading, endPoint: .bottomTrailing)168 VStack(alignment: .leading, spacing: size.height * 0.02) {169 Image(systemName: card.kind.symbol).font(.system(size: size.height * 0.14, weight: .semibold))170 Spacer()171 Text(card.name).font(.system(size: size.height * 0.1, weight: .semibold)).lineLimit(1).minimumScaleFactor(0.5)172 }173 .foregroundStyle(onDark ? Color.white.opacity(0.9) : Color.black.opacity(0.75))174 .padding(size.height * 0.1)175 }176 }177}178179/// Privacy Mode overlay. Sits on a blurred face, so nothing printed on the card180/// can be read.181struct PrivacyVeil: View {182 let card: Card183 var size: CGSize184185 var body: some View {186 ZStack {187 Rectangle().fill(.black.opacity(0.35))188 VStack(alignment: .leading, spacing: size.height * 0.03) {189 // Name on the top band, so it stays readable in the stacked wallet.190 HStack(spacing: size.height * 0.05) {191 Image(systemName: card.kind.symbol)192 Text(card.name).lineLimit(1).minimumScaleFactor(0.6)193 Spacer(minLength: 0)194 Image(systemName: "eye.slash.fill").opacity(0.7)195 }196 .font(.system(size: size.height * 0.085, weight: .semibold))197 Spacer()198 HStack {199 Text(card.issuer.isEmpty ? card.kind.title : card.issuer).lineLimit(1)200 Spacer()201 if !card.last4.isEmpty { Text("•••• " + card.last4).monospaced() }202 }203 .font(.system(size: size.height * 0.075, weight: .medium))204 .opacity(0.85)205 }206 .foregroundStyle(.white)207 .padding(size.height * 0.1)208 }209 }210}211212/// A card that turns over in 3D. The front and the back are separate views; the213/// one facing away is hidden once the rotation passes 90°.214struct FlippableCardView: View {215 let card: Card216 @Binding var showsBack: Bool217 var thumbnail = false218 var mode: CardRenderMode = .premium219 var privacy = false220 var manualTilt: CGSize = .zero221 var usesDeviceTilt = true222223 @Environment(\.accessibilityReduceMotion) private var reduceMotion224225 var body: some View {226 let angle: Double = showsBack ? 180 : 0227 ZStack {228 VirtualCardView(card: card, side: .front, thumbnail: thumbnail, mode: mode, privacy: privacy,229 manualTilt: manualTilt, usesDeviceTilt: usesDeviceTilt)230 .modifier(FlipFace(angle: angle, isBack: false, reduceMotion: reduceMotion))231 if card.hasBack {232 VirtualCardView(card: card, side: .back, thumbnail: thumbnail, mode: mode, privacy: privacy,233 manualTilt: CGSize(width: -manualTilt.width, height: manualTilt.height), usesDeviceTilt: usesDeviceTilt)234 .modifier(FlipFace(angle: angle, isBack: true, reduceMotion: reduceMotion))235 }236 }237 .animation(reduceMotion ? SP.Motion.reduced : SP.Motion.flip, value: showsBack)238 .accessibilityAction(named: showsBack ? "Show front" : "Show back") { if card.hasBack { showsBack.toggle() } }239 }240}241242private struct FlipFace: ViewModifier, @MainActor Animatable {243 var angle: Double244 var isBack: Bool245 var reduceMotion: Bool246247 var animatableData: Double {248 get { angle }249 set { angle = newValue }250 }251252 func body(content: Content) -> some View {253 let visible = isBack ? angle >= 90 : angle < 90254 if reduceMotion {255 content.opacity(visible ? 1 : 0)256 } else {257 content258 .rotation3DEffect(.degrees(isBack ? angle - 180 : angle), axis: (x: 0, y: 1, z: 0), perspective: 0.55)259 // A card lifts slightly off the surface mid-turn.260 .scaleEffect(1 + 0.06 * sin(angle * .pi / 180))261 .opacity(visible ? 1 : 0)262 }263 }264}265266/// A card face drawn from an in-memory photograph that is not in the vault yet267/// (scanner review, preview before saving). Same geometry and light as268/// `VirtualCardView`.269struct RawCardFace: View {270 let image: CGImage271 var format: CardFormat272 var premium = true273 var tilt: CGSize = .zero274275 var body: some View {276 GeometryReader { proxy in277 let size = proxy.size278 let shape = RoundedRectangle(cornerRadius: size.width * format.cornerRadiusFraction, style: .continuous)279 Image(decorative: image, scale: 1)280 .resizable()281 .interpolation(.high)282 .aspectRatio(contentMode: .fill)283 .frame(width: size.width, height: size.height)284 .clipShape(shape)285 .modifier(SheenEffect(size: size, tilt: tilt, enabled: premium))286 .overlay {287 shape.strokeBorder(LinearGradient(colors: [.white.opacity(0.55), .white.opacity(0.08), .black.opacity(0.25)],288 startPoint: .top, endPoint: .bottom), lineWidth: max(0.75, size.width * 0.003))289 .blendMode(.overlay)290 }291 .compositingGroup()292 .shadow(color: .black.opacity(0.38), radius: size.width * 0.05, y: size.width * 0.035)293 }294 .aspectRatio(format.aspectRatio, contentMode: .fit)295 }296}297