Skip to content

Commit 4c1a73e

Browse files
committed
fix: sendable protocol in WalletViewModel, ActivityListViewMode and SettingsViewModel
1 parent 5a05d59 commit 4c1a73e

3 files changed

Lines changed: 42 additions & 37 deletions

File tree

BDKSwiftExampleWallet/View Model/Activity/ActivityListViewModel.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ class ActivityListViewModel {
2222
var totalScripts: UInt64 = 0
2323
var walletSyncState: WalletSyncState
2424
var walletViewError: AppError?
25+
26+
private var updateProgress: @Sendable (UInt64, UInt64) -> Void {
27+
{ [weak self] inspected, total in
28+
DispatchQueue.main.async {
29+
self?.totalScripts = total
30+
self?.inspectedScripts = inspected
31+
self?.progress = total > 0 ? Float(inspected) / Float(total) : 0
32+
}
33+
}
34+
}
2535

2636
enum DisplayMode {
2737
case transactions
@@ -90,13 +100,4 @@ class ActivityListViewModel {
90100
func syncOrFullScan() async {
91101
await startSyncWithProgress()
92102
}
93-
94-
private func updateProgress(inspected: UInt64, total: UInt64) {
95-
DispatchQueue.main.async {
96-
self.totalScripts = total
97-
self.inspectedScripts = inspected
98-
self.progress = total > 0 ? Float(inspected) / Float(total) : 0
99-
}
100-
}
101-
102103
}

BDKSwiftExampleWallet/View Model/Settings/SettingsViewModel.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class SettingsViewModel: ObservableObject {
2020
@Published var settingsError: AppError?
2121
@Published var showingSettingsViewErrorAlert = false
2222
@Published var walletSyncState: WalletSyncState = .notStarted
23+
24+
private var updateProgressFullScan: @Sendable (UInt64) -> Void {
25+
{ [weak self] inspected in
26+
DispatchQueue.main.async {
27+
self?.inspectedScripts = inspected
28+
}
29+
}
30+
}
2331

2432
init(
2533
bdkClient: BDKClient = .live
@@ -83,11 +91,4 @@ class SettingsViewModel: ObservableObject {
8391
func getEsploraUrl() {
8492
self.esploraURL = bdkClient.getEsploraURL()
8593
}
86-
87-
private func updateProgressFullScan(inspected: UInt64) {
88-
DispatchQueue.main.async {
89-
self.inspectedScripts = inspected
90-
}
91-
}
92-
9394
}

BDKSwiftExampleWallet/View Model/WalletViewModel.swift

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ class WalletViewModel {
3939
var transactions: [CanonicalTx]
4040
var walletSyncState: WalletSyncState
4141
var walletViewError: AppError?
42+
43+
private var updateProgress: @Sendable (UInt64, UInt64) -> Void {
44+
{ [weak self] inspected, total in
45+
DispatchQueue.main.async {
46+
self?.totalScripts = total
47+
self?.inspectedScripts = inspected
48+
self?.progress = total > 0 ? Float(inspected) / Float(total) : 0
49+
}
50+
}
51+
}
52+
53+
private var updateProgressFullScan: @Sendable (UInt64) -> Void {
54+
{ [weak self] inspected in
55+
DispatchQueue.main.async {
56+
self?.inspectedScripts = inspected
57+
}
58+
}
59+
}
4260

4361
init(
4462
bdkClient: BDKClient = .live,
@@ -144,29 +162,14 @@ class WalletViewModel {
144162
await startSyncWithProgress()
145163
}
146164
}
147-
148-
private func updateProgress(inspected: UInt64, total: UInt64) {
149-
DispatchQueue.main.async {
150-
self.totalScripts = total
151-
self.inspectedScripts = inspected
152-
self.progress = total > 0 ? Float(inspected) / Float(total) : 0
153-
}
154-
}
155-
156-
private func updateProgressFullScan(inspected: UInt64) {
157-
DispatchQueue.main.async {
158-
self.inspectedScripts = inspected
159-
}
160-
}
161-
162165
}
163166

164-
class WalletSyncScriptInspector: SyncScriptInspector {
165-
private let updateProgress: (UInt64, UInt64) -> Void
167+
actor WalletSyncScriptInspector: @preconcurrency SyncScriptInspector {
168+
private let updateProgress: @Sendable (UInt64, UInt64) -> Void
166169
private var inspectedCount: UInt64 = 0
167170
private var totalCount: UInt64 = 0
168171

169-
init(updateProgress: @escaping (UInt64, UInt64) -> Void) {
172+
init(updateProgress: @escaping @Sendable (UInt64, UInt64) -> Void) {
170173
self.updateProgress = updateProgress
171174
}
172175

@@ -189,11 +192,11 @@ class WalletSyncScriptInspector: SyncScriptInspector {
189192
}
190193
}
191194

192-
class WalletFullScanScriptInspector: FullScanScriptInspector {
193-
private let updateProgress: (UInt64) -> Void
195+
actor WalletFullScanScriptInspector: @preconcurrency FullScanScriptInspector {
196+
private let updateProgress: @Sendable (UInt64) -> Void
194197
private var inspectedCount: UInt64 = 0
195198

196-
init(updateProgress: @escaping (UInt64) -> Void) {
199+
init(updateProgress: @escaping @Sendable (UInt64) -> Void) {
197200
self.updateProgress = updateProgress
198201
}
199202

0 commit comments

Comments
 (0)