|
| 1 | +import BitkitCore |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +/// Row displaying a discovered Trezor device |
| 5 | +struct TrezorDeviceRow: View { |
| 6 | + let device: TrezorDeviceInfo |
| 7 | + let isConnecting: Bool |
| 8 | + let onConnect: () -> Void |
| 9 | + |
| 10 | + var body: some View { |
| 11 | + Button(action: { |
| 12 | + if !isConnecting { |
| 13 | + onConnect() |
| 14 | + } |
| 15 | + }) { |
| 16 | + HStack(spacing: 16) { |
| 17 | + // Device icon |
| 18 | + Image(systemName: transportIcon) |
| 19 | + .font(.system(size: 24)) |
| 20 | + .foregroundColor(.white) |
| 21 | + .frame(width: 48, height: 48) |
| 22 | + .background(Color.white.opacity(0.1)) |
| 23 | + .clipShape(RoundedRectangle(cornerRadius: 12)) |
| 24 | + |
| 25 | + // Device info |
| 26 | + VStack(alignment: .leading, spacing: 4) { |
| 27 | + Text(displayName) |
| 28 | + .font(.system(size: 16, weight: .semibold)) |
| 29 | + .foregroundColor(.white) |
| 30 | + |
| 31 | + Text(transportLabel) |
| 32 | + .font(.system(size: 14)) |
| 33 | + .foregroundColor(.white.opacity(0.6)) |
| 34 | + } |
| 35 | + |
| 36 | + Spacer() |
| 37 | + |
| 38 | + // Connect indicator or chevron |
| 39 | + if isConnecting { |
| 40 | + ProgressView() |
| 41 | + .progressViewStyle(CircularProgressViewStyle(tint: .white)) |
| 42 | + } else { |
| 43 | + Text("Connect") |
| 44 | + .font(.system(size: 14, weight: .medium)) |
| 45 | + .foregroundColor(.white) |
| 46 | + .padding(.horizontal, 16) |
| 47 | + .padding(.vertical, 8) |
| 48 | + .background(Color.white.opacity(0.15)) |
| 49 | + .clipShape(Capsule()) |
| 50 | + } |
| 51 | + } |
| 52 | + .padding(16) |
| 53 | + .background(Color.white.opacity(0.05)) |
| 54 | + .clipShape(RoundedRectangle(cornerRadius: 16)) |
| 55 | + } |
| 56 | + .buttonStyle(.plain) |
| 57 | + .disabled(isConnecting) |
| 58 | + } |
| 59 | + |
| 60 | + private var displayName: String { |
| 61 | + if let label = device.label, !label.isEmpty { |
| 62 | + return label |
| 63 | + } |
| 64 | + return modelName |
| 65 | + } |
| 66 | + |
| 67 | + private var modelName: String { |
| 68 | + if let model = device.model { |
| 69 | + return "Trezor \(model)" |
| 70 | + } |
| 71 | + return "Trezor" |
| 72 | + } |
| 73 | + |
| 74 | + private var transportIcon: String { |
| 75 | + switch device.transportType { |
| 76 | + case .bluetooth: |
| 77 | + return "wave.3.right" |
| 78 | + case .usb: |
| 79 | + return "cable.connector" |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private var transportLabel: String { |
| 84 | + switch device.transportType { |
| 85 | + case .bluetooth: |
| 86 | + return "Bluetooth" |
| 87 | + case .usb: |
| 88 | + return "USB" |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// MARK: - Known Device Row |
| 94 | + |
| 95 | +/// Row displaying a previously connected (known) Trezor device |
| 96 | +struct KnownDeviceRow: View { |
| 97 | + let device: TrezorKnownDevice |
| 98 | + let isConnecting: Bool |
| 99 | + let onConnect: () -> Void |
| 100 | + let onForget: () -> Void |
| 101 | + |
| 102 | + var body: some View { |
| 103 | + HStack(spacing: 16) { |
| 104 | + // Tap area for connect |
| 105 | + Button(action: { |
| 106 | + if !isConnecting { |
| 107 | + onConnect() |
| 108 | + } |
| 109 | + }) { |
| 110 | + HStack(spacing: 16) { |
| 111 | + // Device icon |
| 112 | + Image(systemName: device.transportType == "bluetooth" ? "wave.3.right" : "cable.connector") |
| 113 | + .font(.system(size: 24)) |
| 114 | + .foregroundColor(.white) |
| 115 | + .frame(width: 48, height: 48) |
| 116 | + .background(Color.white.opacity(0.1)) |
| 117 | + .clipShape(RoundedRectangle(cornerRadius: 12)) |
| 118 | + |
| 119 | + // Device info |
| 120 | + VStack(alignment: .leading, spacing: 4) { |
| 121 | + Text(device.label ?? device.name) |
| 122 | + .font(.system(size: 16, weight: .semibold)) |
| 123 | + .foregroundColor(.white) |
| 124 | + |
| 125 | + Text(device.lastConnectedAt.relativeDescription) |
| 126 | + .font(.system(size: 12)) |
| 127 | + .foregroundColor(.white.opacity(0.4)) |
| 128 | + } |
| 129 | + |
| 130 | + Spacer() |
| 131 | + |
| 132 | + if isConnecting { |
| 133 | + ProgressView() |
| 134 | + .progressViewStyle(CircularProgressViewStyle(tint: .white)) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + .buttonStyle(.plain) |
| 139 | + .disabled(isConnecting) |
| 140 | + |
| 141 | + // Forget button |
| 142 | + Button(action: onForget) { |
| 143 | + Image(systemName: "trash") |
| 144 | + .font(.system(size: 14)) |
| 145 | + .foregroundColor(.white.opacity(0.4)) |
| 146 | + .padding(10) |
| 147 | + } |
| 148 | + .buttonStyle(.plain) |
| 149 | + } |
| 150 | + .padding(16) |
| 151 | + .background(Color.white.opacity(0.05)) |
| 152 | + .clipShape(RoundedRectangle(cornerRadius: 16)) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// MARK: - Date Helper |
| 157 | + |
| 158 | +extension Date { |
| 159 | + private static let relativeDateFormatter: RelativeDateTimeFormatter = { |
| 160 | + let formatter = RelativeDateTimeFormatter() |
| 161 | + formatter.unitsStyle = .full |
| 162 | + return formatter |
| 163 | + }() |
| 164 | + |
| 165 | + /// Returns a relative description like "2 minutes ago" |
| 166 | + var relativeDescription: String { |
| 167 | + Self.relativeDateFormatter.localizedString(for: self, relativeTo: Date()) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// MARK: - Preview |
| 172 | + |
| 173 | +#if DEBUG |
| 174 | + struct TrezorDeviceRow_Previews: PreviewProvider { |
| 175 | + static var previews: some View { |
| 176 | + ZStack { |
| 177 | + Color.black.ignoresSafeArea() |
| 178 | + |
| 179 | + VStack(spacing: 16) { |
| 180 | + TrezorDeviceRow( |
| 181 | + device: TrezorDeviceInfo( |
| 182 | + id: "ble:12345", |
| 183 | + transportType: .bluetooth, |
| 184 | + name: "Trezor Safe 5", |
| 185 | + path: "ble:12345", |
| 186 | + label: "My Trezor", |
| 187 | + model: "Safe 5", |
| 188 | + isBootloader: false |
| 189 | + ), |
| 190 | + isConnecting: false, |
| 191 | + onConnect: {} |
| 192 | + ) |
| 193 | + |
| 194 | + TrezorDeviceRow( |
| 195 | + device: TrezorDeviceInfo( |
| 196 | + id: "usb:001", |
| 197 | + transportType: .usb, |
| 198 | + name: "Trezor Model T", |
| 199 | + path: "usb:001", |
| 200 | + label: nil, |
| 201 | + model: "Model T", |
| 202 | + isBootloader: false |
| 203 | + ), |
| 204 | + isConnecting: true, |
| 205 | + onConnect: {} |
| 206 | + ) |
| 207 | + } |
| 208 | + .padding() |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | +#endif |
0 commit comments