Skip to content

fix: issue 289 #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions BDKSwiftExampleWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

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

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

/* Begin PBXGroup section */
779E70832DB9C94C006E22D3 /* Actor */ = {
isa = PBXGroup;
children = (
779E70862DB9C98A006E22D3 /* WalletSyncScriptInspector.swift */,
779E70882DB9C9AB006E22D3 /* WalletFullScanScriptInspector.swift */,
);
path = Actor;
sourceTree = "<group>";
};
A7FBCE392A72944C007C960E /* Resources */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -414,6 +427,7 @@
AE1C34222A424440008F807A /* App */,
AE7F670A2A7451B600CED561 /* Model */,
AE1C341F2A424415008F807A /* View */,
779E70832DB9C94C006E22D3 /* Actor */,
AE0C31002A804C05008F1EAE /* View Model */,
AEB905C52A7EECD900CD0337 /* Service */,
AE1C341E2A42440A008F807A /* Extensions */,
Expand Down Expand Up @@ -708,7 +722,9 @@
AED4CC102A1D522100CE1831 /* WalletView.swift in Sources */,
AE7F67092A7451AA00CED561 /* Price.swift in Sources */,
AE184EFC2BFE52C800374362 /* Amount+Extensions.swift in Sources */,
779E70892DB9C9AB006E22D3 /* WalletFullScanScriptInspector.swift in Sources */,
AE2381B12C60690900F6B00C /* LocalOutput+Extensions.swift in Sources */,
779E70872DB9C98A006E22D3 /* WalletSyncScriptInspector.swift in Sources */,
AE7F67072A744CE200CED561 /* Double+Extensions.swift in Sources */,
A73F7A362A3B778E00B87FC6 /* Int+Extensions.swift in Sources */,
AE7839FD2AB4E18D005F0CBA /* AmountView.swift in Sources */,
Expand Down
22 changes: 22 additions & 0 deletions BDKSwiftExampleWallet/Actor/WalletFullScanScriptInspector.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// WalletFullScanScriptInspector.swift
// BDKSwiftExampleWallet
//
// Created by Rubens Machion on 23/04/25.
//

import BitcoinDevKit

actor WalletFullScanScriptInspector: @preconcurrency FullScanScriptInspector {
private let updateProgress: @Sendable (UInt64) -> Void
private var inspectedCount: UInt64 = 0

init(updateProgress: @escaping @Sendable (UInt64) -> Void) {
self.updateProgress = updateProgress
}

func inspect(keychain: KeychainKind, index: UInt32, script: Script) {
inspectedCount += 1
updateProgress(inspectedCount)
}
}
43 changes: 43 additions & 0 deletions BDKSwiftExampleWallet/Actor/WalletSyncScriptInspector.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// WalletSyncScriptInspector.swift
// BDKSwiftExampleWallet
//
// Created by Rubens Machion on 23/04/25.
//

import BitcoinDevKit
import Foundation

actor WalletSyncScriptInspector: @preconcurrency SyncScriptInspector {
private let updateProgress: @Sendable (UInt64, UInt64) -> Void
private var inspectedCount: UInt64 = 0
private var totalCount: UInt64 = 0

init(updateProgress: @escaping @Sendable (UInt64, UInt64) -> Void) {
self.updateProgress = updateProgress
}

func inspect(script: Script, total: UInt64) {
totalCount = total
inspectedCount += 1

let delay: TimeInterval =
if total <= 5 {
0.2
} else if total < 10 {
0.15
} else if total < 20 {
0.1
} else {
0
}
if delay > 0 {
Task {
try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
updateProgress(inspectedCount, totalCount)
}
} else {
updateProgress(inspectedCount, totalCount)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ class ActivityListViewModel {
var walletSyncState: WalletSyncState
var walletViewError: AppError?

private var updateProgress: @Sendable (UInt64, UInt64) -> Void {
{ [weak self] inspected, total in
DispatchQueue.main.async {
self?.totalScripts = total
self?.inspectedScripts = inspected
self?.progress = total > 0 ? Float(inspected) / Float(total) : 0
}
}
}

enum DisplayMode {
case transactions
case outputs
Expand Down Expand Up @@ -90,13 +100,4 @@ class ActivityListViewModel {
func syncOrFullScan() async {
await startSyncWithProgress()
}

private func updateProgress(inspected: UInt64, total: UInt64) {
DispatchQueue.main.async {
self.totalScripts = total
self.inspectedScripts = inspected
self.progress = total > 0 ? Float(inspected) / Float(total) : 0
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class SettingsViewModel: ObservableObject {
@Published var showingSettingsViewErrorAlert = false
@Published var walletSyncState: WalletSyncState = .notStarted

private var updateProgressFullScan: @Sendable (UInt64) -> Void {
{ [weak self] inspected in
DispatchQueue.main.async {
self?.inspectedScripts = inspected
}
}
}

init(
bdkClient: BDKClient = .live
) {
Expand Down Expand Up @@ -83,11 +91,4 @@ class SettingsViewModel: ObservableObject {
func getEsploraUrl() {
self.esploraURL = bdkClient.getEsploraURL()
}

private func updateProgressFullScan(inspected: UInt64) {
DispatchQueue.main.async {
self.inspectedScripts = inspected
}
}

}
75 changes: 18 additions & 57 deletions BDKSwiftExampleWallet/View Model/WalletViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ class WalletViewModel {
var walletSyncState: WalletSyncState
var walletViewError: AppError?

private var updateProgress: @Sendable (UInt64, UInt64) -> Void {
{ [weak self] inspected, total in
DispatchQueue.main.async {
self?.totalScripts = total
self?.inspectedScripts = inspected
self?.progress = total > 0 ? Float(inspected) / Float(total) : 0
}
}
}

private var updateProgressFullScan: @Sendable (UInt64) -> Void {
{ [weak self] inspected in
DispatchQueue.main.async {
self?.inspectedScripts = inspected
}
}
}

init(
bdkClient: BDKClient = .live,
keyClient: KeyClient = .live,
Expand Down Expand Up @@ -144,61 +162,4 @@ class WalletViewModel {
await startSyncWithProgress()
}
}

private func updateProgress(inspected: UInt64, total: UInt64) {
DispatchQueue.main.async {
self.totalScripts = total
self.inspectedScripts = inspected
self.progress = total > 0 ? Float(inspected) / Float(total) : 0
}
}

private func updateProgressFullScan(inspected: UInt64) {
DispatchQueue.main.async {
self.inspectedScripts = inspected
}
}

}

class WalletSyncScriptInspector: SyncScriptInspector {
private let updateProgress: (UInt64, UInt64) -> Void
private var inspectedCount: UInt64 = 0
private var totalCount: UInt64 = 0

init(updateProgress: @escaping (UInt64, UInt64) -> Void) {
self.updateProgress = updateProgress
}

func inspect(script: Script, total: UInt64) {
totalCount = total
inspectedCount += 1

let delay: TimeInterval =
if total <= 5 {
0.2
} else if total < 10 {
0.15
} else if total < 20 {
0.1
} else {
0
}
Thread.sleep(forTimeInterval: delay)
updateProgress(inspectedCount, totalCount)
}
}

class WalletFullScanScriptInspector: FullScanScriptInspector {
private let updateProgress: (UInt64) -> Void
private var inspectedCount: UInt64 = 0

init(updateProgress: @escaping (UInt64) -> Void) {
self.updateProgress = updateProgress
}

func inspect(keychain: KeychainKind, index: UInt32, script: Script) {
inspectedCount += 1
updateProgress(inspectedCount)
}
}