-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHardwareWalletScreen.swift
More file actions
207 lines (183 loc) · 7.69 KB
/
Copy pathHardwareWalletScreen.swift
File metadata and controls
207 lines (183 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import BitkitCore
import SwiftUI
/// Detail overview of a paired hardware wallet, tracked as a watch-only balance. Mirrors the
/// Savings/Spending screens: device name + blue Bitcoin icon in the top bar, balance header, the
/// device's on-chain activity grouped by date (blue hardware icons), a Transfer-To-Spending
/// placeholder on funded devices, and a Remove action. Ports bitkit-android's `HardwareWalletScreen`.
struct HardwareWalletScreen: View {
let deviceId: String
@EnvironmentObject var activity: ActivityListViewModel
@EnvironmentObject var app: AppViewModel
@EnvironmentObject var navigation: NavigationViewModel
@Environment(HwWalletManager.self) private var hwWalletManager
@Environment(TrezorManager.self) private var trezorManager
@State private var activities: [Activity] = []
@State private var showRemoveDialog = false
private var wallet: HwWallet? {
hwWalletManager.wallets.first { $0.deviceIds.contains(deviceId) }
}
var body: some View {
let wallet = wallet
return ZStack(alignment: .top) {
if let wallet {
NavigationBar(title: wallet.name, icon: "btc-circle-blue")
.padding(.horizontal, 16)
content(for: wallet)
bottomGradient
}
}
.navigationBarHidden(true)
.task(id: wallet?.walletId) {
await loadActivities()
}
.onReceive(activity.activitiesChangedPublisher) { _ in
Task { await loadActivities() }
}
// Leave the screen once the device is gone, whether removed here or forgotten elsewhere.
.onChange(of: wallet != nil) { _, stillPaired in
if hwWalletManager.walletsLoaded, !stillPaired {
navigation.navigateBack()
}
}
.alert(
t("hardware__remove_dialog_title", variables: ["name": wallet?.name ?? ""]),
isPresented: $showRemoveDialog
) {
Button(t("common__remove"), role: .destructive) {
Task { await removeWallet() }
}
Button(t("common__dialog_cancel"), role: .cancel) {}
} message: {
Text(t("hardware__remove_dialog_text"))
}
}
private func content(for wallet: HwWallet) -> some View {
let hasFunds = wallet.balanceSats > 0
let hasActivity = !activities.isEmpty
return VStack(spacing: 0) {
ScrollView(showsIndicators: false) {
MoneyStack(
sats: Int(clamping: wallet.balanceSats),
showSymbol: true,
enableSwipeGesture: true,
enableHide: true,
testIdPrefix: "TotalBalance"
)
if hasFunds {
transferButton
.padding(.top, 28)
}
if hasActivity {
HardwareWalletActivityList(activities: activities)
.padding(.top, 32)
}
removeButton(for: wallet)
.padding(.top, 16)
}
.contentMargins(.top, ScreenLayout.topPaddingWithoutSafeArea)
.contentMargins(.bottom, ScreenLayout.bottomPaddingWithSafeArea)
.frame(maxWidth: .infinity, minHeight: 400)
}
.padding(.horizontal)
.background(alignment: .topTrailing) {
trezorIllustration
// Align the device's top with the balance header and bleed off the trailing edge.
.offset(x: 118, y: ScreenLayout.topPaddingWithoutSafeArea)
}
}
/// The shared upright Trezor device, transformed to match the Figma "Wallet Overview" visual:
/// cover-filled into a square, rotated -15°, and clipped to a 256pt box that bleeds off the
/// screen's trailing edge. Reuses the generic `trezor-device` asset — no screen-specific crop is
/// baked in, so the same image can be adapted elsewhere with SwiftUI.
private var trezorIllustration: some View {
Image("trezor-device")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 268, height: 268)
.clipped()
.rotationEffect(.degrees(-15))
.offset(x: 9, y: 13)
.frame(width: 256, height: 256)
.clipped()
}
private var transferButton: some View {
CustomButton(
title: t("lightning__transfer_to_spending_button"),
variant: .secondary,
icon: Image("arrow-up-down")
.resizable()
.scaledToFit()
.frame(width: 16, height: 16)
.foregroundColor(.white80)
) {
app.toast(type: .warning, title: t("hardware__transfer_not_implemented"))
}
.accessibilityIdentifier("HwTransferToSpending")
}
private func removeButton(for wallet: HwWallet) -> some View {
CustomButton(
title: t("hardware__remove_button", variables: ["name": wallet.name]),
variant: .tertiary
) {
showRemoveDialog = true
}
.accessibilityIdentifier("RemoveHardwareWallet")
}
private var bottomGradient: some View {
VStack {
Spacer()
LinearGradient(
colors: [.black.opacity(0), .black],
startPoint: .top,
endPoint: .bottom
)
.frame(height: ScreenLayout.bottomPaddingWithSafeArea)
}
.ignoresSafeArea(edges: .bottom)
.allowsHitTesting(false)
}
@MainActor
private func loadActivities() async {
guard let walletId = wallet?.walletId else { return }
do {
activities = try await CoreService.shared.activity.get(filter: .all, walletId: walletId)
} catch {
Logger.error(error, context: "HardwareWalletScreen failed to load activities")
}
}
/// Stop watching and forget every entry for this wallet (the same device may be paired over
/// multiple transports). `removeDevice` stops the watchers and deletes the persisted activities;
/// `forgetDevice` clears credentials and drops the known-device entry, which re-pushes the device
/// snapshot and removes the tile. The reactive auto-pop above then leaves the screen.
private func removeWallet() async {
guard let wallet else { return }
hwWalletManager.removeDevice(id: wallet.id)
for id in wallet.deviceIds {
await trezorManager.forgetDevice(id: id)
}
}
}
/// The hardware wallet's on-chain activity, grouped by date and rendered with the shared activity
/// row. Hardware activities draw the blue icon automatically (derived from their `walletId`).
private struct HardwareWalletActivityList: View {
@EnvironmentObject var activity: ActivityListViewModel
@EnvironmentObject var feeEstimatesManager: FeeEstimatesManager
let activities: [Activity]
var body: some View {
let groupedItems = activity.groupActivities(activities)
LazyVStack(alignment: .leading, spacing: 16) {
ForEach(Array(zip(groupedItems.indices, groupedItems)), id: \.1) { index, groupItem in
switch groupItem {
case let .header(title):
CaptionMText(title)
.frame(height: 34, alignment: .bottom)
case let .activity(item):
NavigationLink(value: Route.activityDetail(item)) {
ActivityRow(item: item, feeEstimates: feeEstimatesManager.estimates)
}
.accessibilityIdentifier("Activity-\(index)")
}
}
}
}
}