Skip to content

Commit 73a7a0c

Browse files
committed
refactor: removed ActivityHomeHeardeView.DataSource and create a ActivityHomeHeardeView.State with less params
1 parent 512c037 commit 73a7a0c

File tree

2 files changed

+101
-57
lines changed

2 files changed

+101
-57
lines changed

BDKSwiftExampleWallet/View/Home/ActivityHomeHeaderView.swift

Lines changed: 74 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,88 +9,82 @@ import SwiftUI
99

1010
struct ActivityHomeHeaderView: View {
1111

12-
struct DataSource {
13-
let walletSyncState: WalletSyncState
14-
let progress: Float
15-
let inspectedScripts: UInt64
16-
let totalScripts: UInt64
17-
let needsFullScan: Bool
12+
enum State {
13+
case synced
14+
case fullSyncing(inspectedScripts: UInt64)
15+
case syncing(progress: Float, inspectedScripts: UInt64, totalScripts: UInt64)
16+
case notStarted
17+
case error(Error)
1818
}
1919

20-
let dataSource: ActivityHomeHeaderView.DataSource
20+
let state: State
2121

2222
let showAllTransactions: () -> Void
2323

2424
var body: some View {
2525
HStack {
2626
Text("Activity")
2727
Spacer()
28-
if dataSource.walletSyncState == .syncing {
29-
HStack {
30-
if dataSource.progress < 1.0 {
31-
Text("\(dataSource.inspectedScripts)")
32-
.padding(.trailing, -5.0)
33-
.fontWeight(.semibold)
34-
.contentTransition(.numericText())
35-
.transition(.opacity)
28+
29+
HStack {
30+
if case let .fullSyncing(inspectedScripts) = state {
31+
Text("\(inspectedScripts)")
32+
.padding(.trailing, -5.0)
33+
.fontWeight(.semibold)
34+
.contentTransition(.numericText())
35+
.transition(.opacity)
36+
.fontDesign(.monospaced)
37+
.foregroundStyle(.secondary)
38+
.font(.caption2)
39+
.fontWeight(.thin)
40+
.animation(.easeInOut, value: inspectedScripts)
41+
}
42+
if case let .syncing(progress, inspectedScripts, totalScripts) = state {
43+
HStack {
44+
if progress < 1.0 {
45+
Text("\(inspectedScripts)")
46+
.padding(.trailing, -5.0)
47+
.fontWeight(.semibold)
48+
.contentTransition(.numericText())
49+
.transition(.opacity)
3650

37-
if !dataSource.needsFullScan {
3851
Text("/")
3952
.padding(.trailing, -5.0)
4053
.transition(.opacity)
41-
Text("\(dataSource.totalScripts)")
54+
Text("\(totalScripts)")
4255
.contentTransition(.numericText())
4356
.transition(.opacity)
4457
}
45-
}
4658

47-
if !dataSource.needsFullScan {
4859
Text(
4960
String(
5061
format: "%.0f%%",
51-
dataSource.progress * 100
62+
progress * 100
5263
)
5364
)
5465
.contentTransition(.numericText())
5566
.transition(.opacity)
5667
}
68+
.fontDesign(.monospaced)
69+
.foregroundStyle(.secondary)
70+
.font(.caption2)
71+
.fontWeight(.thin)
72+
.animation(.easeInOut, value: inspectedScripts)
73+
.animation(.easeInOut, value: totalScripts)
74+
.animation(.easeInOut, value: progress)
5775
}
58-
.fontDesign(.monospaced)
59-
.foregroundStyle(.secondary)
60-
.font(.caption2)
61-
.fontWeight(.thin)
62-
.animation(.easeInOut, value: dataSource.inspectedScripts)
63-
.animation(.easeInOut, value: dataSource.totalScripts)
64-
.animation(.easeInOut, value: dataSource.progress)
6576
}
6677
HStack {
6778
HStack(spacing: 5) {
68-
if dataSource.walletSyncState == .syncing {
69-
Image(systemName: "slowmo")
70-
.symbolEffect(
71-
.variableColor.cumulative
72-
)
73-
} else if dataSource.walletSyncState == .synced {
74-
Image(systemName: "checkmark.circle.fill")
75-
.foregroundStyle(
76-
dataSource.walletSyncState == .synced
77-
? .green : .secondary
78-
)
79-
} else if dataSource.walletSyncState == .notStarted {
80-
Image(systemName: "arrow.clockwise")
81-
} else {
82-
Image(
83-
systemName: "person.crop.circle.badge.exclamationmark"
84-
)
85-
}
79+
state.syncImageIndicator
8680
}
8781
.contentTransition(.symbolEffect(.replace.offUp))
8882

8983
}
9084
.foregroundStyle(.secondary)
9185
.font(.caption)
92-
93-
if dataSource.walletSyncState == .synced {
86+
87+
if case .synced = state {
9488
Button {
9589
self.showAllTransactions()
9690
} label: {
@@ -103,8 +97,40 @@ struct ActivityHomeHeaderView: View {
10397
.fontWeight(.regular)
10498
}
10599
}
106-
107100
}
108101
.fontWeight(.bold)
109102
}
110103
}
104+
105+
106+
fileprivate extension ActivityHomeHeaderView.State {
107+
108+
var syncImageIndicator: some View {
109+
switch self {
110+
case .synced:
111+
return AnyView(
112+
Image(systemName: "checkmark.circle.fill")
113+
.foregroundStyle(.green)
114+
)
115+
116+
case .syncing(_, _, _), .fullSyncing(_):
117+
return AnyView(
118+
Image(systemName: "slowmo")
119+
.symbolEffect(
120+
.variableColor.cumulative
121+
)
122+
)
123+
124+
case .notStarted:
125+
return AnyView(
126+
Image(systemName: "arrow.clockwise")
127+
)
128+
default:
129+
return AnyView(
130+
Image(
131+
systemName: "person.crop.circle.badge.exclamationmark"
132+
)
133+
)
134+
}
135+
}
136+
}

BDKSwiftExampleWallet/View/WalletView.swift

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,11 @@ struct WalletView: View {
4343

4444
VStack {
4545
ActivityHomeHeaderView(
46-
dataSource: .init(
47-
walletSyncState: $viewModel.walletSyncState.wrappedValue,
48-
progress: $viewModel.progress.wrappedValue,
49-
inspectedScripts: $viewModel.inspectedScripts.wrappedValue,
50-
totalScripts: $viewModel.totalScripts.wrappedValue,
51-
needsFullScan: viewModel.bdkClient.needsFullScan()
52-
)) {
53-
showAllTransactions = true
54-
}
46+
state: viewModel.activityHeaderStateSync
47+
) {
48+
showAllTransactions = true
49+
}
50+
5551
TransactionListView(
5652
viewModel: .init(),
5753
transactions: viewModel.recentTransactions,
@@ -185,6 +181,28 @@ struct WalletView: View {
185181
}
186182
}
187183

184+
fileprivate extension WalletViewModel {
185+
186+
var activityHeaderStateSync: ActivityHomeHeaderView.State {
187+
let walletSyncState = walletSyncState
188+
let needsFullScan = bdkClient.needsFullScan()
189+
190+
if needsFullScan {
191+
return .fullSyncing(inspectedScripts: inspectedScripts)
192+
} else if walletSyncState == .synced {
193+
return .synced
194+
} else if walletSyncState == .syncing {
195+
return .syncing(
196+
progress: progress,
197+
inspectedScripts: inspectedScripts,
198+
totalScripts: totalScripts
199+
)
200+
} else {
201+
return .notStarted
202+
}
203+
}
204+
}
205+
188206
#if DEBUG
189207
#Preview("WalletView - en") {
190208
WalletView(

0 commit comments

Comments
 (0)