Skip to content

Commit 9d7f3c5

Browse files
fix: inspector warnings
1 parent 5a05d59 commit 9d7f3c5

File tree

6 files changed

+117
-73
lines changed

6 files changed

+117
-73
lines changed

BDKSwiftExampleWallet.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/* Begin PBXBuildFile section */
1010
774586B52DB7B2BC00A631E1 /* BalanceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 774586B42DB7B2BC00A631E1 /* BalanceView.swift */; };
11+
779E70872DB9C98A006E22D3 /* WalletSyncScriptInspector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 779E70862DB9C98A006E22D3 /* WalletSyncScriptInspector.swift */; };
12+
779E70892DB9C9AB006E22D3 /* WalletFullScanScriptInspector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 779E70882DB9C9AB006E22D3 /* WalletFullScanScriptInspector.swift */; };
1113
77F0FDC92DA9A93D00B30E4F /* Connection+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77F0FDC82DA9A93700B30E4F /* Connection+Extensions.swift */; };
1214
A733D6D02A81113000F333B4 /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = A733D6CF2A81113000F333B4 /* Localizable.xcstrings */; };
1315
A73F7A362A3B778E00B87FC6 /* Int+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A73F7A352A3B778E00B87FC6 /* Int+Extensions.swift */; };
@@ -110,6 +112,8 @@
110112

111113
/* Begin PBXFileReference section */
112114
774586B42DB7B2BC00A631E1 /* BalanceView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BalanceView.swift; sourceTree = "<group>"; };
115+
779E70862DB9C98A006E22D3 /* WalletSyncScriptInspector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletSyncScriptInspector.swift; sourceTree = "<group>"; };
116+
779E70882DB9C9AB006E22D3 /* WalletFullScanScriptInspector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletFullScanScriptInspector.swift; sourceTree = "<group>"; };
113117
77F0FDC82DA9A93700B30E4F /* Connection+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Connection+Extensions.swift"; sourceTree = "<group>"; };
114118
A733D6CF2A81113000F333B4 /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; };
115119
A73F7A352A3B778E00B87FC6 /* Int+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Int+Extensions.swift"; sourceTree = "<group>"; };
@@ -222,6 +226,15 @@
222226
/* End PBXFrameworksBuildPhase section */
223227

224228
/* Begin PBXGroup section */
229+
779E70832DB9C94C006E22D3 /* Actor */ = {
230+
isa = PBXGroup;
231+
children = (
232+
779E70862DB9C98A006E22D3 /* WalletSyncScriptInspector.swift */,
233+
779E70882DB9C9AB006E22D3 /* WalletFullScanScriptInspector.swift */,
234+
);
235+
path = Actor;
236+
sourceTree = "<group>";
237+
};
225238
A7FBCE392A72944C007C960E /* Resources */ = {
226239
isa = PBXGroup;
227240
children = (
@@ -414,6 +427,7 @@
414427
AE1C34222A424440008F807A /* App */,
415428
AE7F670A2A7451B600CED561 /* Model */,
416429
AE1C341F2A424415008F807A /* View */,
430+
779E70832DB9C94C006E22D3 /* Actor */,
417431
AE0C31002A804C05008F1EAE /* View Model */,
418432
AEB905C52A7EECD900CD0337 /* Service */,
419433
AE1C341E2A42440A008F807A /* Extensions */,
@@ -708,7 +722,9 @@
708722
AED4CC102A1D522100CE1831 /* WalletView.swift in Sources */,
709723
AE7F67092A7451AA00CED561 /* Price.swift in Sources */,
710724
AE184EFC2BFE52C800374362 /* Amount+Extensions.swift in Sources */,
725+
779E70892DB9C9AB006E22D3 /* WalletFullScanScriptInspector.swift in Sources */,
711726
AE2381B12C60690900F6B00C /* LocalOutput+Extensions.swift in Sources */,
727+
779E70872DB9C98A006E22D3 /* WalletSyncScriptInspector.swift in Sources */,
712728
AE7F67072A744CE200CED561 /* Double+Extensions.swift in Sources */,
713729
A73F7A362A3B778E00B87FC6 /* Int+Extensions.swift in Sources */,
714730
AE7839FD2AB4E18D005F0CBA /* AmountView.swift in Sources */,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// WalletFullScanScriptInspector.swift
3+
// BDKSwiftExampleWallet
4+
//
5+
// Created by Rubens Machion on 23/04/25.
6+
//
7+
8+
import BitcoinDevKit
9+
10+
actor WalletFullScanScriptInspector: @preconcurrency FullScanScriptInspector {
11+
private let updateProgress: @Sendable (UInt64) -> Void
12+
private var inspectedCount: UInt64 = 0
13+
14+
init(updateProgress: @escaping @Sendable (UInt64) -> Void) {
15+
self.updateProgress = updateProgress
16+
}
17+
18+
func inspect(keychain: KeychainKind, index: UInt32, script: Script) {
19+
inspectedCount += 1
20+
updateProgress(inspectedCount)
21+
}
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// WalletSyncScriptInspector.swift
3+
// BDKSwiftExampleWallet
4+
//
5+
// Created by Rubens Machion on 23/04/25.
6+
//
7+
8+
import BitcoinDevKit
9+
import Foundation
10+
11+
actor WalletSyncScriptInspector: @preconcurrency SyncScriptInspector {
12+
private let updateProgress: @Sendable (UInt64, UInt64) -> Void
13+
private var inspectedCount: UInt64 = 0
14+
private var totalCount: UInt64 = 0
15+
16+
init(updateProgress: @escaping @Sendable (UInt64, UInt64) -> Void) {
17+
self.updateProgress = updateProgress
18+
}
19+
20+
func inspect(script: Script, total: UInt64) {
21+
totalCount = total
22+
inspectedCount += 1
23+
24+
let delay: TimeInterval =
25+
if total <= 5 {
26+
0.2
27+
} else if total < 10 {
28+
0.15
29+
} else if total < 20 {
30+
0.1
31+
} else {
32+
0
33+
}
34+
if delay > 0 {
35+
Task {
36+
try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
37+
updateProgress(inspectedCount, totalCount)
38+
}
39+
} else {
40+
updateProgress(inspectedCount, totalCount)
41+
}
42+
}
43+
}

BDKSwiftExampleWallet/View Model/Activity/ActivityListViewModel.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ class ActivityListViewModel {
2323
var walletSyncState: WalletSyncState
2424
var walletViewError: AppError?
2525

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+
}
35+
2636
enum DisplayMode {
2737
case transactions
2838
case outputs
@@ -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
@@ -21,6 +21,14 @@ class SettingsViewModel: ObservableObject {
2121
@Published var showingSettingsViewErrorAlert = false
2222
@Published var walletSyncState: WalletSyncState = .notStarted
2323

24+
private var updateProgressFullScan: @Sendable (UInt64) -> Void {
25+
{ [weak self] inspected in
26+
DispatchQueue.main.async {
27+
self?.inspectedScripts = inspected
28+
}
29+
}
30+
}
31+
2432
init(
2533
bdkClient: BDKClient = .live
2634
) {
@@ -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: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ class WalletViewModel {
4040
var walletSyncState: WalletSyncState
4141
var walletViewError: AppError?
4242

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+
}
60+
4361
init(
4462
bdkClient: BDKClient = .live,
4563
keyClient: KeyClient = .live,
@@ -144,61 +162,4 @@ 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-
162-
}
163-
164-
class WalletSyncScriptInspector: SyncScriptInspector {
165-
private let updateProgress: (UInt64, UInt64) -> Void
166-
private var inspectedCount: UInt64 = 0
167-
private var totalCount: UInt64 = 0
168-
169-
init(updateProgress: @escaping (UInt64, UInt64) -> Void) {
170-
self.updateProgress = updateProgress
171-
}
172-
173-
func inspect(script: Script, total: UInt64) {
174-
totalCount = total
175-
inspectedCount += 1
176-
177-
let delay: TimeInterval =
178-
if total <= 5 {
179-
0.2
180-
} else if total < 10 {
181-
0.15
182-
} else if total < 20 {
183-
0.1
184-
} else {
185-
0
186-
}
187-
Thread.sleep(forTimeInterval: delay)
188-
updateProgress(inspectedCount, totalCount)
189-
}
190-
}
191-
192-
class WalletFullScanScriptInspector: FullScanScriptInspector {
193-
private let updateProgress: (UInt64) -> Void
194-
private var inspectedCount: UInt64 = 0
195-
196-
init(updateProgress: @escaping (UInt64) -> Void) {
197-
self.updateProgress = updateProgress
198-
}
199-
200-
func inspect(keychain: KeychainKind, index: UInt32, script: Script) {
201-
inspectedCount += 1
202-
updateProgress(inspectedCount)
203-
}
204165
}

0 commit comments

Comments
 (0)