Skip to content

Commit 8cc7f9e

Browse files
committed
added isNeedFullScan as a @AppStorage
1 parent 3737185 commit 8cc7f9e

File tree

8 files changed

+21
-22
lines changed

8 files changed

+21
-22
lines changed

BDKSwiftExampleWallet/App/BDKSwiftExampleWalletApp.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import SwiftUI
1111
@main
1212
struct BDKSwiftExampleWalletApp: App {
1313
@AppStorage("isOnboarding") var isOnboarding: Bool = true
14+
@AppStorage("isNeedFullScan") var isNeedFullScan: Bool = false
1415
@State private var navigationPath = NavigationPath()
1516

1617
var body: some Scene {
@@ -21,6 +22,7 @@ struct BDKSwiftExampleWalletApp: App {
2122
keyClient: keyClient,
2223
network: .signet
2324
)
25+
2426
if let _ = try? KeyClient.live.getBackupInfo() {
2527
HomeView(
2628
viewModel: .init(
@@ -38,7 +40,8 @@ struct BDKSwiftExampleWalletApp: App {
3840
}
3941
}
4042
.onChange(of: isOnboarding) { oldValue, newValue in
41-
BDKClient.live.setNeedsFullScan(true)
43+
// BDKClient.live.setNeedsFullScan(true)
44+
isNeedFullScan = true
4245
navigationPath = NavigationPath()
4346
}
4447
}

BDKSwiftExampleWallet/Service/BDKSyncService/BDKSyncService.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ protocol BDKSyncService {
1313
var keyClient: KeyClient { get }
1414
var network: Network { get }
1515
var wallet: Wallet? { get }
16-
var needsFullScan: Bool { get }
1716

1817
func createWallet(params: String?) throws
1918
func loadWallet() throws

BDKSwiftExampleWallet/Service/BDKSyncService/EsploraServerSyncService.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ final class EsploraServerSyncService: BDKSyncService {
1414
var keyClient: KeyClient
1515
var network: Network
1616
var wallet: Wallet?
17-
var needsFullScan = false
1817

1918
private var esploraClient: EsploraClient
2019

@@ -23,7 +22,7 @@ final class EsploraServerSyncService: BDKSyncService {
2322
network: Network = .signet,
2423
connection: Connection? = nil
2524
) {
26-
self.connection = connection// ?? (try? Connection.createConnection())
25+
self.connection = connection
2726
self.keyClient = keyClient
2827
self.network = network
2928

@@ -36,7 +35,6 @@ final class EsploraServerSyncService: BDKSyncService {
3635
func createWallet(params: String?) throws {
3736
self.connection = try Connection.createConnection()
3837
self.wallet = try buildWallet(params: params)
39-
needsFullScan = true
4038
}
4139

4240
func loadWallet() throws {
@@ -47,7 +45,6 @@ final class EsploraServerSyncService: BDKSyncService {
4745

4846
func deleteWallet() throws {
4947
try deleteData()
50-
needsFullScan = true
5148
}
5249

5350
func updateNetwork(network: Network) {

BDKSwiftExampleWallet/Service/BDKSyncService/KyotoSyncService.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ final class KyotoSyncService: BDKSyncService {
1414
var keyClient: KeyClient
1515
var network: Network
1616
var wallet: Wallet?
17-
var needsFullScan = false
1817

1918
private var node: CbfNode?
2019
private var client: CbfClient?

BDKSwiftExampleWallet/View Model/OnboardingViewModel.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class OnboardingViewModel: ObservableObject {
2222
private var bdkSyncService: BDKSyncService
2323

2424
@AppStorage("isOnboarding") var isOnboarding: Bool?
25+
@AppStorage("isNeedFullScan") var isNeedFullScan: Bool?
2526

2627
@Published var walletSyncType: WalletSyncType = .esplora {
2728
didSet {
@@ -99,6 +100,7 @@ class OnboardingViewModel: ObservableObject {
99100
do {
100101
try bdkSyncService.deleteWallet()
101102
try bdkSyncService.createWallet(params: words.isEmpty ? nil : words)
103+
isNeedFullScan = true
102104

103105
DispatchQueue.main.async {
104106
self.isOnboarding = false

BDKSwiftExampleWallet/View Model/WalletViewModel.swift

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import SwiftUI
1313
@MainActor
1414
@Observable
1515
class WalletViewModel {
16+
1617
private let bdkSyncService: BDKSyncService
17-
let bdkClient: BDKClient
18+
private(set) var isNeedFullScan: Bool
1819
let keyClient: KeyClient
1920
let priceClient: PriceClient
2021

@@ -40,9 +41,6 @@ class WalletViewModel {
4041
var transactions: [CanonicalTx]
4142
var walletSyncState: WalletSyncState
4243
var walletViewError: AppError?
43-
var needsFullScan: Bool {
44-
bdkClient.needsFullScan()
45-
}
4644

4745
private var updateProgress: @Sendable (UInt64, UInt64) -> Void {
4846
{ [weak self] inspected, total in
@@ -63,19 +61,19 @@ class WalletViewModel {
6361
}
6462

6563
init(
66-
bdkClient: BDKClient = .live,
6764
keyClient: KeyClient = .live,
6865
priceClient: PriceClient = .live,
6966
transactions: [CanonicalTx] = [],
7067
walletSyncState: WalletSyncState = .notStarted,
71-
bdkSyncService: BDKSyncService
68+
bdkSyncService: BDKSyncService,
69+
isNeedFullScan: Bool
7270
) {
73-
self.bdkClient = bdkClient
7471
self.keyClient = keyClient
7572
self.priceClient = priceClient
7673
self.transactions = transactions
7774
self.walletSyncState = walletSyncState
7875
self.bdkSyncService = bdkSyncService
76+
self.isNeedFullScan = isNeedFullScan
7977
}
8078

8179
private func fullScanWithProgress() async {
@@ -84,7 +82,7 @@ class WalletViewModel {
8482
let inspector = WalletFullScanScriptInspector(updateProgress: updateProgressFullScan)
8583
try await bdkSyncService.startFullScan(progress: inspector)
8684

87-
// try await bdkClient.fullScanWithInspector(inspector)
85+
isNeedFullScan = false
8886
self.walletSyncState = .synced
8987
} catch let error as CannotConnectError {
9088
self.walletViewError = .generic(message: error.localizedDescription)
@@ -129,7 +127,6 @@ class WalletViewModel {
129127
func getTransactions() {
130128
do {
131129
let transactionDetails = try bdkSyncService.getTransactions()
132-
// let transactionDetails = try bdkClient.transactions()
133130
self.transactions = transactionDetails
134131
} catch let error as WalletError {
135132
self.walletViewError = .generic(message: error.localizedDescription)
@@ -146,7 +143,6 @@ class WalletViewModel {
146143
let inspector = WalletSyncScriptInspector(updateProgress: updateProgress)
147144
try await bdkSyncService.startSync(progress: inspector)
148145

149-
// try await bdkClient.syncWithInspector(inspector)
150146
self.walletSyncState = .synced
151147
} catch let error as CannotConnectError {
152148
self.walletViewError = .generic(message: error.localizedDescription)
@@ -167,9 +163,8 @@ class WalletViewModel {
167163
}
168164

169165
func syncOrFullScan() async {
170-
if bdkClient.needsFullScan() {
166+
if isNeedFullScan {
171167
await fullScanWithProgress()
172-
bdkClient.setNeedsFullScan(false)
173168
} else {
174169
await startSyncWithProgress()
175170
}

BDKSwiftExampleWallet/View/HomeView.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import SwiftUI
99

1010
struct HomeView: View {
11+
@AppStorage("isNeedFullScan") var isNeedFullScan: Bool?
12+
1113
@Bindable var viewModel: HomeViewModel
1214
@Binding var navigationPath: NavigationPath
1315

@@ -17,9 +19,9 @@ struct HomeView: View {
1719

1820
WalletView(
1921
viewModel: .init(
20-
bdkClient: .live,
2122
priceClient: .live,
22-
bdkSyncService: viewModel.bdkSyncService
23+
bdkSyncService: viewModel.bdkSyncService,
24+
isNeedFullScan: isNeedFullScan ?? false
2325
),
2426
sendNavigationPath: $navigationPath
2527
)

BDKSwiftExampleWallet/View/WalletView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import SwiftUI
1111
struct WalletView: View {
1212
@AppStorage("balanceDisplayFormat") private var balanceFormat: BalanceDisplayFormat =
1313
.bitcoinSats
14+
@AppStorage("isNeedFullScan") var isNeedFullScan: Bool?
1415
@Bindable var viewModel: WalletViewModel
1516
@Binding var sendNavigationPath: NavigationPath
1617
@State private var isFirstAppear = true
@@ -47,8 +48,9 @@ struct WalletView: View {
4748
progress: viewModel.progress,
4849
inspectedScripts: viewModel.inspectedScripts,
4950
totalScripts: viewModel.totalScripts,
50-
needsFullScan: viewModel.needsFullScan
51+
needsFullScan: viewModel.isNeedFullScan
5152
) {
53+
isNeedFullScan = false
5254
showAllTransactions = true
5355
}
5456

0 commit comments

Comments
 (0)