-
Notifications
You must be signed in to change notification settings - Fork 9
/
ResourceBalanceView.swift
633 lines (556 loc) · 16.6 KB
/
ResourceBalanceView.swift
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import Sargon
import SwiftUI
// MARK: - ResourceBalance.ViewState
extension ResourceBalance {
// MARK: - ViewState
public enum ViewState: Sendable, Hashable {
case fungible(Fungible)
case nonFungible(NonFungible)
case liquidStakeUnit(LiquidStakeUnit)
case poolUnit(PoolUnit)
case stakeClaimNFT(StakeClaimNFT)
public struct Fungible: Sendable, Hashable {
public let address: ResourceAddress
public let icon: Thumbnail.FungibleContent
public let title: String?
public let amount: ResourceBalance.Amount?
}
public struct NonFungible: Sendable, Hashable {
public let id: NonFungibleGlobalId
public let resourceImage: URL?
public let resourceName: String?
public let nonFungibleName: String?
}
public struct LiquidStakeUnit: Sendable, Hashable {
public let address: ResourceAddress
public let icon: URL?
public let title: String?
public let amount: ResourceBalance.Amount?
public let worth: ResourceAmount
public var validatorName: String? = nil
}
public struct PoolUnit: Sendable, Hashable {
public let resourcePoolAddress: PoolAddress
public let poolUnitAddress: ResourceAddress
public let poolIcon: URL?
public let poolName: String?
public let amount: ResourceBalance.Amount?
public var dAppName: Loadable<String?>
public var resources: Loadable<[Fungible]>
}
public typealias StakeClaimNFT = ResourceBalance.StakeClaimNFT
}
var viewState: ViewState {
switch details {
case let .fungible(details):
.fungible(.init(resource: resource, details: details))
case let .nonFungible(details):
.nonFungible(.init(resource: resource, details: details))
case let .liquidStakeUnit(details):
.liquidStakeUnit(.init(resource: resource, details: details))
case let .poolUnit(details):
.poolUnit(.init(resource: resource, details: details))
case let .stakeClaimNFT(details):
.stakeClaimNFT(details)
}
}
}
private extension ResourceBalance.ViewState.Fungible {
init(resource: OnLedgerEntity.Resource, details: ResourceBalance.Fungible) {
self.init(
address: resource.resourceAddress,
icon: .token(details.isXRD ? .xrd : .other(resource.metadata.iconURL)),
title: resource.metadata.title,
amount: .init(details.amount, guaranteed: details.guarantee?.amount)
)
}
}
private extension ResourceBalance.ViewState.NonFungible {
init(resource: OnLedgerEntity.Resource, details: ResourceBalance.NonFungible) {
self.init(
id: details.id,
resourceImage: resource.metadata.iconURL,
resourceName: resource.metadata.name,
nonFungibleName: details.data?.name
)
}
}
private extension ResourceBalance.ViewState.LiquidStakeUnit {
init(resource: OnLedgerEntity.Resource, details: ResourceBalance.LiquidStakeUnit) {
self.init(
address: resource.resourceAddress,
icon: resource.metadata.iconURL,
title: resource.metadata.title,
amount: .init(details.amount, guaranteed: details.guarantee?.amount),
worth: details.worth,
validatorName: details.validator.metadata.name
)
}
}
private extension ResourceBalance.ViewState.PoolUnit {
init(resource: OnLedgerEntity.Resource, details: ResourceBalance.PoolUnit) {
self.init(
resourcePoolAddress: details.details.address,
poolUnitAddress: resource.resourceAddress,
poolIcon: resource.metadata.iconURL,
poolName: resource.fungibleResourceName,
amount: .init(details.details.poolUnitResource.amount, guaranteed: details.guarantee?.amount),
dAppName: .success(details.details.dAppName),
resources: .success(.init(resources: details.details))
)
}
}
// MARK: - ResourceBalanceView
public struct ResourceBalanceView: View {
public let viewState: ResourceBalance.ViewState
public let appearance: Appearance
public let isSelected: Bool?
public let action: (() -> Void)?
public enum Appearance: Sendable, Equatable {
case standard
case compact(border: Bool)
static let compact: Appearance = .compact(border: false)
}
init(
_ viewState: ResourceBalance.ViewState,
appearance: Appearance = .standard,
isSelected: Bool? = nil,
action: (() -> Void)? = nil
) {
self.viewState = viewState
self.appearance = appearance
self.isSelected = isSelected
self.action = action
}
public var body: some View {
content
.embedInButton(when: action)
}
@ViewBuilder
private var content: some View {
if border {
core
.padding(.small1)
.roundedCorners(strokeColor: .app.gray3)
} else {
core
}
}
private var core: some View {
HStack(alignment: .center, spacing: .small2) {
switch viewState {
case let .fungible(viewState):
Fungible(viewState: viewState, compact: compact)
case let .nonFungible(viewState):
NonFungible(viewState: viewState, compact: compact)
case let .liquidStakeUnit(viewState):
LiquidStakeUnit(viewState: viewState, compact: compact, isSelected: isSelected)
case let .poolUnit(viewState):
PoolUnit(viewState: viewState, compact: compact, isSelected: isSelected)
case let .stakeClaimNFT(viewState):
StakeClaimNFT(viewState: viewState, appearance: .standalone, compact: compact, onTap: { _ in })
}
if !delegateSelection, let isSelected {
CheckmarkView(appearance: .dark, isChecked: isSelected)
}
}
}
var compact: Bool {
appearance != .standard
}
var border: Bool {
appearance == .compact(border: true)
}
/// Delegate showing the selection state to the particular resource view
var delegateSelection: Bool {
switch viewState {
case .fungible, .nonFungible:
false
case .liquidStakeUnit, .poolUnit, .stakeClaimNFT:
true
}
}
}
extension ResourceBalanceView {
public struct Fungible: View {
@Environment(\.missingFungibleAmountFallback) var fallback
public let viewState: ResourceBalance.ViewState.Fungible
public let compact: Bool
public var body: some View {
FungibleView(
thumbnail: viewState.icon,
caption1: viewState.title,
caption2: nil,
fallback: fallback,
amount: viewState.amount,
compact: compact,
isSelected: nil
)
}
}
public struct NonFungible: View {
public let viewState: ResourceBalance.ViewState.NonFungible
public let compact: Bool
public var body: some View {
NonFungibleView(
thumbnail: .nft(viewState.resourceImage),
caption1: viewState.resourceName ?? viewState.id.resourceAddress.formatted(),
caption2: viewState.nonFungibleName ?? viewState.id.localID.formatted(),
compact: compact
)
}
}
public struct LiquidStakeUnit: View {
@Environment(\.resourceBalanceHideDetails) var hideDetails
public let viewState: ResourceBalance.ViewState.LiquidStakeUnit
public let compact: Bool
public let isSelected: Bool?
private var fungible: ResourceBalance.ViewState.Fungible {
.xrd(balance: viewState.worth, network: viewState.address.networkID)
}
public var body: some View {
VStack(alignment: .leading, spacing: .medium3) {
FungibleView(
thumbnail: .lsu(viewState.icon),
caption1: viewState.title ?? "-",
caption2: viewState.validatorName ?? "-",
fallback: nil,
amount: viewState.amount,
compact: compact,
isSelected: isSelected
)
if !hideDetails {
VStack(alignment: .leading, spacing: .small3) {
Text(L10n.Account.Staking.worth.uppercased())
.textStyle(.body2HighImportance)
.foregroundColor(.app.gray2)
ResourceBalanceView(.fungible(fungible), appearance: .compact(border: true))
}
.padding(.top, .small2)
}
}
}
}
public struct PoolUnit: View {
@Environment(\.resourceBalanceHideDetails) var hideDetails
public let viewState: ResourceBalance.ViewState.PoolUnit
public let compact: Bool
public let isSelected: Bool?
public var body: some View {
VStack(alignment: .leading, spacing: .zero) {
FungibleView(
thumbnail: .poolUnit(viewState.poolIcon),
caption1: viewState.poolName ?? "-",
caption2: viewState.dAppName.wrappedValue?.flatMap { $0 } ?? "-",
fallback: nil,
amount: viewState.amount,
compact: compact,
isSelected: isSelected
)
.padding(.horizontal, hideDetails ? .zero : .small3)
if !hideDetails {
Text(L10n.TransactionReview.worth.uppercased())
.textStyle(.body2HighImportance)
.foregroundColor(.app.gray2)
.padding(.top, .small2)
.padding(.bottom, .small3)
loadable(viewState.resources) { fungibles in
ResourceBalancesView(fungibles: fungibles)
.environment(\.missingFungibleAmountFallback, L10n.Account.PoolUnits.noTotalSupply)
}
}
}
}
}
public struct StakeClaimNFT: View {
@Environment(\.resourceBalanceHideDetails) var hideDetails
public let viewState: ResourceBalance.ViewState.StakeClaimNFT
public let appearance: Appearance
public let compact: Bool
public let onTap: (OnLedgerEntitiesClient.StakeClaim) -> Void
public var onClaimAllTapped: (() -> Void)? = nil
public enum Appearance {
case standalone
case transactionReview
}
public var body: some View {
VStack(alignment: .leading, spacing: .zero) {
NonFungibleView(
thumbnail: .stakeClaimNFT(viewState.resourceMetadata.iconURL),
caption1: viewState.resourceMetadata.title,
caption2: viewState.validatorName,
compact: compact
)
if !hideDetails {
Tokens(
viewState: viewState.stakeClaimTokens,
background: background,
onTap: onTap,
onClaimAllTapped: onClaimAllTapped
)
.padding(.top, .small2)
}
}
.padding(padding)
.background(background)
}
private var padding: CGFloat {
switch appearance {
case .standalone: .zero
case .transactionReview: .medium3
}
}
private var background: Color {
switch appearance {
case .standalone: .white
case .transactionReview: .app.gray5
}
}
public struct Tokens: View {
enum SectionKind {
case unstaking
case readyToBeClaimed
case toBeClaimed
}
public var viewState: ResourceBalance.StakeClaimNFT.Tokens
public let background: Color
public let onTap: ((OnLedgerEntitiesClient.StakeClaim) -> Void)?
public let onClaimAllTapped: (() -> Void)?
init(
viewState: ResourceBalance.StakeClaimNFT.Tokens,
background: Color,
onTap: ((OnLedgerEntitiesClient.StakeClaim) -> Void)? = nil,
onClaimAllTapped: (() -> Void)? = nil
) {
self.viewState = viewState
self.background = background
self.onTap = onTap
self.onClaimAllTapped = onClaimAllTapped
}
public var body: some View {
if !viewState.unstaking.isEmpty {
sectionView(viewState.unstaking, kind: .unstaking)
}
if !viewState.readyToBeClaimed.isEmpty {
sectionView(viewState.readyToBeClaimed, kind: .readyToBeClaimed)
}
if !viewState.toBeClaimed.isEmpty {
sectionView(viewState.toBeClaimed, kind: .toBeClaimed)
}
}
@ViewBuilder
func sectionView(
_ claims: IdentifiedArrayOf<OnLedgerEntitiesClient.StakeClaim>,
kind: SectionKind
) -> some View {
VStack(alignment: .leading, spacing: .zero) {
HStack {
Text(kind.title)
.textStyle(.body2HighImportance)
.foregroundColor(.app.gray2)
.textCase(.uppercase)
Spacer()
if case .readyToBeClaimed = kind, viewState.canClaimTokens {
let label = Text(L10n.Account.Staking.claim)
.textStyle(.body2Link)
.foregroundColor(.app.blue1)
if let onClaimAllTapped {
Button(action: onClaimAllTapped) { label }
} else {
label
}
}
}
.padding(.bottom, .small3)
VStack(alignment: .leading, spacing: .small2) {
ForEach(claims) { claim in
Button {
onTap?(claim)
} label: {
let isSelected = viewState.selectedStakeClaims?.contains(claim.id)
ResourceBalanceView(
.fungible(.xrd(balance: claim.claimAmount, network: claim.validatorAddress.networkID)),
appearance: .compact,
isSelected: isSelected
)
.padding(.small1)
.background(background)
}
.disabled(onTap == nil)
.buttonStyle(.borderless)
.roundedCorners(strokeColor: .app.gray3)
}
}
}
}
}
}
// Helper Views
private struct FungibleView: View {
public let thumbnail: Thumbnail.FungibleContent
public let caption1: String?
public let caption2: String?
public let fallback: String?
public let amount: ResourceBalance.Amount?
public let compact: Bool
public let isSelected: Bool?
public var body: some View {
HStack(spacing: .zero) {
CaptionedThumbnailView(
type: thumbnail.type,
url: thumbnail.url,
caption1: caption1,
caption2: caption2,
compact: compact
)
if useSpacer, isSelected == nil {
Spacer(minLength: .small2)
}
AmountView(amount: amount, fallback: fallback, compact: compact)
.padding(.leading, isSelected != nil ? .small2 : 0)
if let isSelected {
Spacer(minLength: .small2)
CheckmarkView(appearance: .dark, isChecked: isSelected)
}
}
}
private var size: HitTargetSize {
compact ? .smallest : .small
}
private var titleTextStyle: TextStyle {
compact ? .body2HighImportance : .body1HighImportance
}
private var useSpacer: Bool {
amount != nil || fallback != nil
}
}
private struct NonFungibleView: View {
let thumbnail: Thumbnail.NonFungibleContent
let caption1: String?
let caption2: String?
let compact: Bool
var body: some View {
HStack(spacing: .zero) {
CaptionedThumbnailView(
type: thumbnail.type,
url: thumbnail.url,
caption1: caption1 ?? "-",
caption2: caption2 ?? "-",
compact: compact
)
Spacer(minLength: 0)
}
}
}
private struct CaptionedThumbnailView: View {
let type: Thumbnail.ContentType
let url: URL?
let caption1: String?
let caption2: String?
let compact: Bool
var body: some View {
Thumbnail(type, url: url, size: size)
.padding(.trailing, .small1)
VStack(alignment: .leading, spacing: 0) {
if let caption1 {
Text(caption1)
.textStyle(titleTextStyle)
.foregroundColor(.app.gray1)
}
if let caption2 {
Text(caption2)
.textStyle(.body2Regular)
.foregroundColor(.app.gray2)
}
}
.lineLimit(1)
.truncationMode(.tail)
}
private var size: HitTargetSize {
compact ? .smallest : .smallish
}
private var titleTextStyle: TextStyle {
compact ? .body2HighImportance : .body1HighImportance
}
}
struct AmountView: View {
@Environment(\.resourceBalanceHideFiatValue) var resourceBalanceHideFiatValue
let amount: ResourceBalance.Amount?
let fallback: String?
let compact: Bool
init(amount: ResourceBalance.Amount?, fallback: String? = nil, compact: Bool) {
self.amount = amount
self.fallback = fallback
self.compact = compact
}
var body: some View {
if let amount {
core(amount: amount, compact: compact)
} else if let fallback {
Text(fallback)
.textStyle(amountTextStyle)
.foregroundColor(.app.gray2)
}
}
@ViewBuilder
private func core(amount: ResourceBalance.Amount, compact: Bool) -> some View {
if compact {
VStack(alignment: .trailing, spacing: 0) {
Text(amount.amount.nominalAmount.formatted())
.textStyle(amountTextStyle)
.foregroundColor(.app.gray1)
if !resourceBalanceHideFiatValue, let fiatWorth = amount.amount.fiatWorth?.currencyFormatted(applyCustomFont: false) {
Text(fiatWorth)
.textStyle(.body2HighImportance)
.foregroundStyle(.app.gray2)
.padding(.top, .small3)
}
}
} else {
VStack(alignment: .trailing, spacing: 0) {
if amount.guaranteed != nil {
Text(L10n.TransactionReview.estimated)
.textStyle(.body2HighImportance)
.foregroundColor(.app.gray1)
}
Text(amount.amount.nominalAmount.formatted())
.lineLimit(1)
.minimumScaleFactor(0.8)
.truncationMode(.tail)
.textStyle(.secondaryHeader)
.foregroundColor(.app.gray1)
if !resourceBalanceHideFiatValue, let fiatWorth = amount.amount.fiatWorth?.currencyFormatted(applyCustomFont: false) {
Text(fiatWorth)
.textStyle(.body2HighImportance)
.foregroundStyle(.app.gray2)
.padding(.top, .small3)
}
if let guaranteedAmount = amount.guaranteed {
Text(L10n.TransactionReview.guaranteed)
.textStyle(.body2HighImportance)
.foregroundColor(.app.gray2)
.padding(.top, .small3)
Text(guaranteedAmount.formatted())
.textStyle(.body1Header)
.foregroundColor(.app.gray2)
}
}
}
}
private var amountTextStyle: TextStyle {
compact ? .body1HighImportance : .secondaryHeader
}
}
}
extension ResourceBalanceView.StakeClaimNFT.Tokens.SectionKind {
var title: String {
switch self {
case .unstaking:
L10n.Account.Staking.unstaking
case .readyToBeClaimed:
L10n.Account.Staking.readyToBeClaimed
case .toBeClaimed:
L10n.TransactionReview.toBeClaimed
}
}
}