nwc: turn pending zap orange when we have a NWC success

Orange means payment successful now, not just presence of zap

This introduces a paid pending state, which shows up as an orange timer
thing in the zaps view. This can be useful if the zap is never sent. We
don't want the user to think the payment didn't go through.
This commit is contained in:
William Casarin
2023-05-13 23:28:07 -07:00
parent 03691d0369
commit 69fc6694f1
6 changed files with 68 additions and 19 deletions

View File

@@ -76,7 +76,7 @@ enum NWCStateType: Equatable {
}
class NWCPendingZapState: Equatable {
var state: NWCStateType
private(set) var state: NWCStateType
let url: WalletConnectURL
init(state: NWCStateType, url: WalletConnectURL) {
@@ -84,6 +84,15 @@ class NWCPendingZapState: Equatable {
self.url = url
}
//@discardableResult -- not discardable, the ZapsDataModel may need to send objectWillChange but we don't force it
func update_state(state: NWCStateType) -> Bool {
guard state != self.state else {
return false
}
self.state = state
return true
}
static func == (lhs: NWCPendingZapState, rhs: NWCPendingZapState) -> Bool {
return lhs.state == rhs.state && lhs.url == rhs.url
}
@@ -94,7 +103,7 @@ class PendingZap {
let target: ZapTarget
let request: ZapRequest
let type: ZapType
var state: PendingZapState
private(set) var state: PendingZapState
init(amount_msat: Int64, target: ZapTarget, request: ZapRequest, type: ZapType, state: PendingZapState) {
self.amount_msat = amount_msat
@@ -103,6 +112,17 @@ class PendingZap {
self.type = type
self.state = state
}
@discardableResult
func update_state(model: ZapsDataModel, state: PendingZapState) -> Bool {
guard self.state != state else {
return false
}
self.state = state
model.objectWillChange.send()
return true
}
}
@@ -119,6 +139,23 @@ enum Zapping {
}
}
var is_paid: Bool {
switch self {
case .zap:
// we have a zap so this is proof of payment
return true
case .pending(let pzap):
switch pzap.state {
case .external:
// It could be but we don't know. We have to wait for a zap to know.
return false
case .nwc(let nwc_state):
// nwc confirmed that we have a payment, but we might not have zap yet
return nwc_state.state == .confirmed
}
}
}
var is_private: Bool {
switch self {
case .zap(let zap):