Skip to content

Commit 364a996

Browse files
committed
added automatic update
1 parent be0e172 commit 364a996

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

BDKSwiftExampleWallet/Service/BDKSyncService/KyotoService.swift

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ extension KyotoService {
1212
static var live: BDKSyncService = KyotoService()
1313
}
1414

15+
extension Notification.Name {
16+
static let shouldUpdateWallet = Notification.Name("ShouldUpdateWallet")
17+
}
18+
1519
final class KyotoService: BDKSyncService {
1620

1721
static let shared: KyotoService = .init()
@@ -23,7 +27,7 @@ final class KyotoService: BDKSyncService {
2327

2428
private var client: CbfClient?
2529
private var node: CbfNode?
26-
private var connected = false
30+
private var isConnected = false
2731
private var isScanRunning = false
2832

2933
private var fullScanProgress: FullScanProgress?
@@ -63,7 +67,7 @@ final class KyotoService: BDKSyncService {
6367
self.client = nodeComponents.client
6468
self.node = nodeComponents.node
6569
isScanRunning = true
66-
try await startListen()
70+
try await startNode()
6771
}
6872

6973
func startFullScan(progress: @escaping FullScanProgress) async throws {
@@ -80,7 +84,7 @@ final class KyotoService: BDKSyncService {
8084
self.client = nodeComponents.client
8185
self.node = nodeComponents.node
8286
isScanRunning = true
83-
try await startListen()
87+
try await startNode()
8488
}
8589

8690
func send(address: String, amount: UInt64, feeRate: UInt64) async throws {
@@ -118,17 +122,19 @@ final class KyotoService: BDKSyncService {
118122
.build(wallet: wallet)
119123
}
120124

121-
private func startListen() async throws {
125+
private func startNode() async throws {
122126
node?.run()
123127
printLogs()
124128
updateWarn()
125-
try await startUpdating()
129+
try await updateWallet()
130+
startRealTimeWalletUpdate()
126131
}
127132

128133
@discardableResult
129-
func startUpdating() async throws -> Bool {
134+
private func updateWallet() async throws -> Bool {
130135
guard let update = await self.client?.update() else {
131136
isScanRunning = false
137+
print("Nothing to update")
132138
return false
133139
}
134140
try self.wallet?.applyUpdate(update: update)
@@ -137,7 +143,27 @@ final class KyotoService: BDKSyncService {
137143
isScanRunning = false
138144
return true
139145
}
140-
146+
147+
private func startRealTimeWalletUpdate() {
148+
print(#function)
149+
Task {
150+
while true {
151+
print("Updating: \(Date())")
152+
if let update = await client?.update() {
153+
do {
154+
try wallet?.applyUpdate(update: update)
155+
NotificationCenter.default.post(name: .shouldUpdateWallet, object: nil)
156+
print("Updated wallet")
157+
} catch {
158+
print(error)
159+
}
160+
} else {
161+
print("Nothing to update")
162+
}
163+
}
164+
}
165+
}
166+
141167
private func printLogs() {
142168
Task {
143169
while true {
@@ -146,7 +172,7 @@ final class KyotoService: BDKSyncService {
146172
switch log {
147173
case .connectionsMet:
148174
print("######### connected")
149-
self.connected = true
175+
self.isConnected = true
150176
case .progress(let progress):
151177
if let fullScanProgress = self.fullScanProgress {
152178
let _progress = UInt64(progress * 100.0)
@@ -167,7 +193,7 @@ final class KyotoService: BDKSyncService {
167193
switch warn {
168194
case .needConnections:
169195
print("######### disconnected")
170-
self.connected = false
196+
self.isConnected = false
171197
default:
172198
#if DEBUG
173199
print(warn)

BDKSwiftExampleWallet/View Model/WalletViewModel.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ class WalletViewModel {
5858
self.priceClient = priceClient
5959
self.transactions = transactions
6060
self.walletSyncState = walletSyncState
61+
NotificationCenter.default.addObserver(self, selector: #selector(receiveNotification(_:)), name: .shouldUpdateWallet, object: nil)
62+
}
63+
64+
@objc private func receiveNotification(_ notification: Notification) {
65+
if notification.name == .shouldUpdateWallet {
66+
getBalance()
67+
getTransactions()
68+
}
6169
}
6270

6371
func getBalance() {

BDKSwiftExampleWallet/View/WalletView.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,24 @@ struct WalletView: View {
5252
) {
5353
showAllTransactions = true
5454
}
55-
56-
TransactionListView(
57-
viewModel: .init(),
58-
transactions: viewModel.recentTransactions,
59-
walletSyncState: viewModel.walletSyncState
60-
)
61-
.refreshable {
62-
await viewModel.syncOrFullScan()
63-
viewModel.getBalance()
64-
viewModel.getTransactions()
65-
await viewModel.getPrices()
55+
if viewModel.syncMode == .esplora {
56+
TransactionListView(
57+
viewModel: .init(),
58+
transactions: viewModel.recentTransactions,
59+
walletSyncState: viewModel.walletSyncState
60+
)
61+
.refreshable {
62+
await viewModel.syncOrFullScan()
63+
viewModel.getBalance()
64+
viewModel.getTransactions()
65+
await viewModel.getPrices()
66+
}
67+
} else {
68+
TransactionListView(
69+
viewModel: .init(),
70+
transactions: viewModel.recentTransactions,
71+
walletSyncState: viewModel.walletSyncState
72+
)
6673
}
6774

6875
HStack {

0 commit comments

Comments
 (0)