|
| 1 | +// |
| 2 | +// UsernameProgressCard.swift |
| 3 | +// Flipcash |
| 4 | +// |
| 5 | + |
| 6 | +import SwiftUI |
| 7 | +import FlipcashUI |
| 8 | + |
| 9 | +/// The You-tab card offering a username, in the tip-card action group below |
| 10 | +/// Share/Download (Figma nodes 9536:4336 / 9537:1845). Superseded the |
| 11 | +/// settings-row entry point: `YouScreen` shows it only while |
| 12 | +/// `shouldPromptForUsername` holds, in place of the row it used to draw. |
| 13 | +/// |
| 14 | +/// Takes plain values rather than reading `Session`/`UsernameGate` itself, so |
| 15 | +/// it can be previewed and tested without a signed-in account — the caller |
| 16 | +/// (`YouScreen`) is the one place that has both. |
| 17 | +struct UsernameProgressCard: View { |
| 18 | + |
| 19 | + /// Which of the two states the card draws. The title never changes; the |
| 20 | + /// state governs the bar's colour, the chevron, and where the tap lands. |
| 21 | + enum State: Equatable { |
| 22 | + /// Below the server's minimum (node 9536:4336): white bar, the |
| 23 | + /// shortfall in place of a chevron. Tapping raises the balance gate. |
| 24 | + case incomplete |
| 25 | + /// At or above it (node 9537:1845): green bar, chevron. Tapping opens |
| 26 | + /// the claim screen. |
| 27 | + case complete |
| 28 | + } |
| 29 | + |
| 30 | + private static let title = "Get a custom @username" |
| 31 | + |
| 32 | + let state: State |
| 33 | + let subtitle: String |
| 34 | + /// The shortfall, shown only in `.incomplete`. `nil` in `.complete`. |
| 35 | + let trailing: String? |
| 36 | + /// The balance's progress toward the minimum, `0...1`. |
| 37 | + let fraction: Double |
| 38 | + /// Runs on tap. Both states are tappable: the card is the entry point to |
| 39 | + /// the flow either way, and below the minimum that means being told the |
| 40 | + /// rule rather than being left with an inert bar. |
| 41 | + let action: VoidAction |
| 42 | + |
| 43 | + var body: some View { |
| 44 | + Button(action: action) { |
| 45 | + chrome |
| 46 | + } |
| 47 | + .buttonStyle(.plain) |
| 48 | + .accessibilityLabel(Self.title) |
| 49 | + // The shortfall reads as part of the hint rather than as a value: it |
| 50 | + // is the same sentence the subtitle starts, not a separate reading. |
| 51 | + .accessibilityHint(trailing.map { "\(subtitle). \($0)" } ?? subtitle) |
| 52 | + } |
| 53 | + |
| 54 | + private var chrome: some View { |
| 55 | + VStack(alignment: .leading, spacing: 6) { |
| 56 | + HStack(alignment: .firstTextBaseline, spacing: 8) { |
| 57 | + Text(Self.title) |
| 58 | + .font(.default(size: 14, weight: .semibold)) |
| 59 | + .foregroundStyle(Color.textMain) |
| 60 | + |
| 61 | + if let trailing { |
| 62 | + Spacer(minLength: 8) |
| 63 | + Text(trailing) |
| 64 | + .font(.default(size: 14, weight: .semibold)) |
| 65 | + .foregroundStyle(Color.textMain.opacity(0.5)) |
| 66 | + } |
| 67 | + |
| 68 | + if state == .complete { |
| 69 | + Spacer(minLength: 8) |
| 70 | + Image.system(.chevronRight) |
| 71 | + .renderingMode(.template) |
| 72 | + .frame(width: 20, height: 20) |
| 73 | + .foregroundStyle(Color.textMain) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + Text(subtitle) |
| 78 | + .font(.default(size: 12, weight: .medium)) |
| 79 | + .foregroundStyle(Color.textMain.opacity(0.5)) |
| 80 | + |
| 81 | + Spacer(minLength: 0) |
| 82 | + |
| 83 | + ProgressTrack(fraction: fraction, fill: state == .complete ? .success : Color.white) |
| 84 | + } |
| 85 | + .padding(.top, 12) |
| 86 | + .padding(.bottom, 15) |
| 87 | + .padding(.horizontal, 15) |
| 88 | + .frame(height: 88) |
| 89 | + .background(Color.backgroundRow, in: RoundedRectangle(cornerRadius: 6, style: .continuous)) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/// The card's progress bar: a 10%-white track with a fill sized to `fraction`. |
| 94 | +private struct ProgressTrack: View { |
| 95 | + |
| 96 | + let fraction: Double |
| 97 | + let fill: Color |
| 98 | + |
| 99 | + var body: some View { |
| 100 | + GeometryReader { proxy in |
| 101 | + ZStack(alignment: .leading) { |
| 102 | + RoundedRectangle(cornerRadius: 2, style: .continuous) |
| 103 | + .fill(Color.white.opacity(0.1)) |
| 104 | + |
| 105 | + RoundedRectangle(cornerRadius: 2, style: .continuous) |
| 106 | + .fill(fill) |
| 107 | + .frame(width: proxy.size.width * CGFloat(max(0, min(1, fraction)))) |
| 108 | + } |
| 109 | + } |
| 110 | + .frame(height: 6) |
| 111 | + } |
| 112 | +} |
0 commit comments