-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransactionView.swift
More file actions
315 lines (282 loc) · 10.4 KB
/
Copy pathTransactionView.swift
File metadata and controls
315 lines (282 loc) · 10.4 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//
// Created by Roman Chornyi
// Copyright © 2026 Dash Core Group. All rights reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
// MARK: - TransactionView
/// Transaction row: a leading icon (with an optional status badge in the bottom-trailing corner),
/// a title / time / optional detail line, and a trailing Dash amount (+ optional fiat). Pass
/// `action` to make the whole row a tappable button; omit it for a static row.
///
/// The amount is a raw duff value (`Int64`, 10⁸ per Dash) rendered via `DashAmount`, mirroring
/// `ConverterCardItem.dashBalance`. Icons are `DashIconSource` (asset in the DashUIKit bundle, an
/// SF Symbol, or a runtime `UIImage`); pass `iconView` for a custom / remotely-loaded main icon.
@available(iOS 14, macOS 11, *)
public struct TransactionView: View {
private enum Layout {
static let rowSpacing: CGFloat = 16
static let iconSize: CGFloat = 30
static let iconCornerRadius: CGFloat = 12
static let badgeSize: CGFloat = 14
static let badgeRing: CGFloat = 2
static let vPadding: CGFloat = 12
static let hPadding: CGFloat = 10
}
// Leading
/// Static leading icon. Ignored when `iconView` is non-nil.
public var icon: DashIconSource?
/// Custom leading view (e.g. a remotely-loaded coin icon). Takes precedence over `icon`.
public var iconView: AnyView?
/// Small status badge pinned to the icon's bottom-trailing corner (e.g. processing / locked).
public var secondaryIcon: DashIconSource?
// Central
/// Optional header above the title, e.g. a grouped-set count like "3 transactions".
public var topText: String?
public var title: String
/// Secondary line, typically the time ("8:34 AM").
public var subtitle: String?
/// Optional extra state/detail line shown next to the subtitle.
public var details: String?
// Trailing
/// Dash amount in duffs (10⁸ per Dash). Negative = outgoing.
public var dashAmount: Int64
public var amountSign: DashAmountSign
/// Optional SF Symbol rendered immediately before the amount, in the secondary tone.
/// Meant for rows whose amount carries no direction — e.g. a circulating-arrows glyph on a
/// wallet-internal transfer shown with `amountSign: .none`.
public var amountAccessorySystemImage: String?
public var fiat: String?
/// Short status shown immediately before the amount, e.g. "Locked" for a coinbase reward that
/// has not matured yet. Rendered in the warning tone, since it qualifies the amount.
public var trailingStatusText: String?
/// When set, the whole row is a plain-styled button. `nil` renders a non-interactive row.
public var action: (() -> Void)?
public init(
icon: DashIconSource? = nil,
iconView: AnyView? = nil,
secondaryIcon: DashIconSource? = nil,
topText: String? = nil,
title: String,
subtitle: String? = nil,
details: String? = nil,
dashAmount: Int64 = 0,
amountSign: DashAmountSign = .negativeOnly,
amountAccessorySystemImage: String? = nil,
fiat: String? = nil,
trailingStatusText: String? = nil,
action: (() -> Void)? = nil
) {
self.icon = icon
self.iconView = iconView
self.secondaryIcon = secondaryIcon
self.topText = topText
self.title = title
self.subtitle = subtitle
self.details = details
self.dashAmount = dashAmount
self.amountSign = amountSign
self.amountAccessorySystemImage = amountAccessorySystemImage
self.fiat = fiat
self.trailingStatusText = trailingStatusText
self.action = action
}
public var body: some View {
if let action {
Button(action: action) { content }
.buttonStyle(.plain)
} else {
content
}
}
private var content: some View {
HStack(spacing: Layout.rowSpacing) {
iconWrap
infoWrap
}
.padding(.vertical, Layout.vPadding)
.padding(.horizontal, Layout.hPadding)
.contentShape(Rectangle())
}
// MARK: - Icon
private var iconWrap: some View {
// `.overlay(_:alignment:)` (iOS 13) rather than `.overlay(alignment:content:)` (iOS 15),
// so the component stays usable at the iOS 14 deployment target.
mainIcon
.frame(width: Layout.iconSize, height: Layout.iconSize)
.clipShape(RoundedRectangle(cornerRadius: Layout.iconCornerRadius, style: .continuous))
.overlay(
secondaryBadge
.offset(x: 3,y: 3),
alignment: .bottomTrailing
)
}
@ViewBuilder
private var secondaryBadge: some View {
if let secondaryIcon {
Image(dash: secondaryIcon)
.resizable()
.scaledToFit()
.frame(width: Layout.badgeSize, height: Layout.badgeSize)
.padding(Layout.badgeRing)
.background(Circle().fill(Color.dash.secondaryBackground))
// Nudge the badge to sit on the icon's corner.
.offset(x: Layout.badgeRing, y: Layout.badgeRing)
}
}
@ViewBuilder
private var mainIcon: some View {
if let iconView {
iconView
} else if let icon {
Image(dash: icon)
.resizable()
.scaledToFit()
} else {
Color.clear
}
}
// MARK: - Info
private var infoWrap: some View {
VStack(alignment: .leading, spacing: 0) {
if let topText {
Text(topText)
.dashFont(.footnote)
.foregroundColor(Color.dash.secondaryText)
}
HStack(alignment: .center, spacing: 8) {
VStack(alignment: .leading, spacing: 1) {
Text(title)
.dashFont(.footnoteMedium)
.foregroundColor(Color.dash.primaryText)
HStack(spacing: 8) {
if let subtitle {
Text(subtitle)
.dashFont(.footnote)
.foregroundColor(Color.dash.secondaryText)
}
// Badge
if let details {
HStack(spacing: 2) {
Text(details)
.dashFont(.caption1Medium)
.foregroundColor(Color.dash.blueText)
}
.padding(.horizontal, 4)
.background(Color.dash.blueAlpha10)
.clipShape(RoundedRectangle(cornerRadius: 7, style: .continuous))
}
}
}
.frame(maxWidth: .infinity, alignment: .leading)
trailingAmount
}
}
}
private var trailingAmount: some View {
VStack(alignment: .trailing, spacing: 1) {
HStack(spacing: 4) {
if let trailingStatusText {
Text(trailingStatusText)
.dashFont(.caption1Medium)
.foregroundColor(Color.dash.orange)
}
if let amountAccessorySystemImage {
Image(systemName: amountAccessorySystemImage)
.font(.system(size: 11, weight: .semibold))
.foregroundColor(Color.dash.secondaryText)
}
DashAmount(amount: dashAmount, sign: amountSign)
.foregroundColor(Color.dash.primaryText)
}
if let fiat {
Text(fiat)
.dashFont(.footnote)
.foregroundColor(Color.dash.secondaryText)
}
}
}
}
// MARK: - Previews
#if DEBUG
@available(iOS 17, macOS 14, *)
#Preview("Received") {
TransactionView(
icon: .system("arrow.down.circle.fill"),
title: "Received",
subtitle: "8:34 AM",
dashAmount: 6_791_000,
amountSign: .always,
fiat: "$ 1.87"
)
.padding(.horizontal)
.background(Color.dash.primaryBackground)
}
@available(iOS 17, macOS 14, *)
#Preview("Sent + badge") {
TransactionView(
icon: .system("arrow.up.circle.fill"),
secondaryIcon: .system("clock.fill"),
title: "Sent",
subtitle: "8:34 AM",
details: "Processing",
dashAmount: -1_250_000,
amountSign: .negativeOnly,
fiat: "$ 0.34"
)
.padding(.horizontal)
.background(Color.dash.primaryBackground)
}
@available(iOS 17, macOS 14, *)
#Preview("Grouped set") {
TransactionView(
icon: .system("circle.grid.2x2.fill"),
topText: "3 transactions",
title: "Mixing Transactions",
subtitle: "8:34 AM",
dashAmount: 50_000_000,
amountSign: .none
)
.padding(.horizontal)
.background(Color.dash.primaryBackground)
}
@available(iOS 17, macOS 14, *)
#Preview("Internal transfer — amount accessory") {
TransactionView(
icon: .system("arrow.forward.circle.fill"),
title: "Internal Transfer",
subtitle: "8:34 AM",
dashAmount: 200_000_000,
amountSign: .none,
amountAccessorySystemImage: "arrow.triangle.2.circlepath",
fiat: "$ 55.00"
)
.padding(.horizontal)
.background(Color.dash.primaryBackground)
}
@available(iOS 17, macOS 14, *)
#Preview("Locked reward") {
TransactionView(
icon: DashIcon.Transaction.mining.source,
title: "Reward",
subtitle: "8:34 AM",
dashAmount: 250_000_000,
amountSign: .always,
fiat: "$ 68.75",
trailingStatusText: "Locked"
)
.padding(.horizontal)
.background(Color.dash.primaryBackground)
}
#endif