Skip to content

Commit 6e10de5

Browse files
committed
fix: network error
1 parent c2817ea commit 6e10de5

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

DashWallet/Sources/UI/Explore Dash/Views/DashSpend/DashSpendPayViewModel.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ import Combine
2121
private let defaultCurrency = kDefaultCurrencyCode
2222

2323
@MainActor
24-
class DashSpendPayViewModel: NSObject, ObservableObject {
24+
class DashSpendPayViewModel: NSObject, ObservableObject, NetworkReachabilityHandling {
2525
private var cancellableBag = Set<AnyCancellable>()
2626
private let fiatFormatter = NumberFormatter.fiatFormatter(currencyCode: defaultCurrency)
2727
private let ctxSpendService = CTXSpendService.shared
2828
private let customIconProvider = CustomIconMetadataProvider.shared
2929
private let txMetadataDao = TransactionMetadataDAOImpl.shared
3030
private let sendCoinsService = SendCoinsService()
31+
32+
// Network monitoring properties
33+
var networkStatusDidChange: ((NetworkStatus) -> ())?
34+
var reachabilityObserver: Any!
3135

3236
private var merchantId: String = ""
3337
private var merchantUrl: String? = nil
@@ -120,9 +124,17 @@ class DashSpendPayViewModel: NSObject, ObservableObject {
120124

121125
// Initialize with current sign-in state
122126
isUserSignedIn = ctxSpendService.isUserSignedIn
127+
128+
// Set up network status change handler
129+
networkStatusDidChange = { [weak self] status in
130+
self?.handleNetworkStatusChange(status)
131+
}
123132
}
124133

125134
func subscribeToUpdates() {
135+
// Start network monitoring
136+
startNetworkMonitoring()
137+
126138
NotificationCenter.default.publisher(for: NSNotification.Name.DSWalletBalanceDidChange)
127139
.sink { [weak self] _ in self?.refreshBalance() }
128140
.store(in: &cancellableBag)
@@ -200,13 +212,20 @@ class DashSpendPayViewModel: NSObject, ObservableObject {
200212

201213
func unsubscribeFromAll() {
202214
cancellableBag.removeAll()
215+
stopNetworkMonitoring()
203216
}
204217

205218
private func refreshBalance() {
206219
walletBalance = DWEnvironment.sharedInstance().currentWallet.balance
207220
}
208221

209222
private func checkAmountForErrors() {
223+
// Check network availability first
224+
guard networkStatus == .online else {
225+
error = SendAmountError.networkUnavailable
226+
return
227+
}
228+
210229
guard DWGlobalOptions.sharedInstance().isResyncingWallet == false ||
211230
DWEnvironment.sharedInstance().currentChainManager.syncPhase == .synced
212231
else {
@@ -296,4 +315,9 @@ class DashSpendPayViewModel: NSObject, ObservableObject {
296315
txMetadata.service = ServiceName.ctxSpend.rawValue
297316
txMetadataDao.update(dto: txMetadata)
298317
}
318+
319+
private func handleNetworkStatusChange(_ status: NetworkStatus) {
320+
// Re-check errors when network status changes
321+
checkAmountForErrors()
322+
}
299323
}

DashWallet/Sources/UI/Payments/Amount/Model/Send/SendAmountModel.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ enum SendAmountError: Error, ColorizedText, LocalizedError {
2323
case insufficientFunds
2424
case insufficientMixedFunds
2525
case syncingChain
26+
case networkUnavailable
2627

2728
var errorDescription: String? {
2829
switch self {
2930
case .insufficientMixedFunds: return NSLocalizedString("Insufficient mixed funds. Wait for CoinJoin mixing to finish or disable this feature in the settings to complete this transaction.", comment: "Send screen")
3031
case .insufficientFunds: return NSLocalizedString("Insufficient funds. Please add more Dash to your wallet or reduce the amount.", comment: "Send screen")
3132
case .syncingChain: return NSLocalizedString("Wait until wallet is synced to complete the transaction",
3233
comment: "Send screen")
34+
case .networkUnavailable: return NSLocalizedString("Network Unavailable", comment: "Network Unavailable")
3335
}
3436
}
3537

@@ -38,6 +40,7 @@ enum SendAmountError: Error, ColorizedText, LocalizedError {
3840
case .insufficientFunds: return .systemRed
3941
case .insufficientMixedFunds: return .systemRed
4042
case .syncingChain: return .secondaryLabel
43+
case .networkUnavailable: return .secondaryLabel
4144
}
4245
}
4346
}

0 commit comments

Comments
 (0)