From 7cbe176936623dfd241a97848bb6269fbd12cc12 Mon Sep 17 00:00:00 2001 From: Ross Savage <551697+dangeross@users.noreply.github.com> Date: Wed, 13 Nov 2024 14:40:37 +0100 Subject: [PATCH] Notify success when waiting for claim confirmation (#562) --- .../job/SwapUpdated.kt | 80 ++++++++++--------- .../BreezSDKLiquid/Task/SwapUpdated.swift | 22 ++--- 2 files changed, 56 insertions(+), 46 deletions(-) diff --git a/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt b/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt index 0a88731..f96300b 100644 --- a/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt +++ b/lib/bindings/langs/android/lib/src/main/kotlin/breez_sdk_liquid_notification/job/SwapUpdated.kt @@ -38,8 +38,10 @@ class SwapUpdatedJob( private val fgService: SdkForegroundService, private val payload: String, private val logger: ServiceLogger, - private var swapIdHash: String? = null, ) : Job { + private var swapIdHash: String? = null + private var notified: Boolean = false + companion object { private const val TAG = "SwapUpdatedJob" } @@ -55,26 +57,8 @@ class SwapUpdatedJob( override fun onEvent(e: SdkEvent) { when (e) { - is SdkEvent.PaymentSucceeded -> { - val payment = e.details - - val swapId = when (val details = payment.details) { - is PaymentDetails.Bitcoin -> details.swapId - is PaymentDetails.Lightning -> details.swapId - else -> null - } - - swapId?.let { - if (this.swapIdHash == hashId(it)) { - logger.log( - TAG, - "Received payment succeeded event: ${this.swapIdHash}", - "TRACE" - ) - notifySuccess(payment) - } - } - } + is SdkEvent.PaymentWaitingConfirmation -> handlePaymentEvent(e.details) + is SdkEvent.PaymentSucceeded -> handlePaymentEvent(e.details) else -> { logger.log(TAG, "Received event: ${e}", "TRACE") @@ -92,27 +76,49 @@ class SwapUpdatedJob( .fold(StringBuilder()) { sb, it -> sb.append("%02x".format(it)) } .toString() + private fun handlePaymentEvent(payment: Payment) { + val swapId = when (val details = payment.details) { + is PaymentDetails.Bitcoin -> details.swapId + is PaymentDetails.Lightning -> details.swapId + else -> null + } + + swapId?.let { + if (this.swapIdHash == hashId(it)) { + logger.log( + TAG, + "Received payment event: ${this.swapIdHash} ${payment.status}", + "TRACE" + ) + notifySuccess(payment) + } + } + } + private fun notifySuccess(payment: Payment) { - logger.log(TAG, "Payment ${payment.txId} completed successfully", "INFO") - val received = payment.paymentType == PaymentType.RECEIVE - notifyChannel( - context, - NOTIFICATION_CHANNEL_SWAP_UPDATED, - getString( + if (!this.notified) { + logger.log(TAG, "Payment ${payment.txId} processing successful", "INFO") + val received = payment.paymentType == PaymentType.RECEIVE + notifyChannel( context, - if (received) PAYMENT_RECEIVED_NOTIFICATION_TITLE else PAYMENT_SENT_NOTIFICATION_TITLE, - if (received) DEFAULT_PAYMENT_RECEIVED_NOTIFICATION_TITLE else DEFAULT_PAYMENT_SENT_NOTIFICATION_TITLE - ), - String.format( + NOTIFICATION_CHANNEL_SWAP_UPDATED, getString( context, - if (received) PAYMENT_RECEIVED_NOTIFICATION_TEXT else PAYMENT_SENT_NOTIFICATION_TEXT, - "%d", - if (received) DEFAULT_PAYMENT_RECEIVED_NOTIFICATION_TEXT else DEFAULT_PAYMENT_SENT_NOTIFICATION_TEXT - ), payment.amountSat.toLong() + if (received) PAYMENT_RECEIVED_NOTIFICATION_TITLE else PAYMENT_SENT_NOTIFICATION_TITLE, + if (received) DEFAULT_PAYMENT_RECEIVED_NOTIFICATION_TITLE else DEFAULT_PAYMENT_SENT_NOTIFICATION_TITLE + ), + String.format( + getString( + context, + if (received) PAYMENT_RECEIVED_NOTIFICATION_TEXT else PAYMENT_SENT_NOTIFICATION_TEXT, + "%d", + if (received) DEFAULT_PAYMENT_RECEIVED_NOTIFICATION_TEXT else DEFAULT_PAYMENT_SENT_NOTIFICATION_TEXT + ), payment.amountSat.toLong() + ) ) - ) - fgService.onFinished(this) + this.notified = true + fgService.onFinished(this) + } } private fun notifyFailure() { diff --git a/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift b/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift index 9eb146e..5ec6c7e 100644 --- a/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift +++ b/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/SwapUpdated.swift @@ -14,6 +14,7 @@ class SwapUpdatedTask : TaskProtocol { internal var bestAttemptContent: UNMutableNotificationContent? internal var logger: ServiceLogger internal var request: SwapUpdatedRequest? = nil + internal var notified: Bool = false init(payload: String, logger: ServiceLogger, contentHandler: ((UNNotificationContent) -> Void)? = nil, bestAttemptContent: UNMutableNotificationContent? = nil) { self.payload = payload @@ -35,10 +36,10 @@ class SwapUpdatedTask : TaskProtocol { public func onEvent(e: SdkEvent) { if let swapIdHash = self.request?.id { switch e { - case .paymentSucceeded(details: let payment): + case .paymentWaitingConfirmation(details: let payment), .paymentSucceeded(details: let payment): let swapId = self.getSwapId(details: payment.details) if swapIdHash == swapId?.sha256() { - self.logger.log(tag: TAG, line: "Received payment succeeded event: \(swapIdHash)", level: "INFO") + self.logger.log(tag: TAG, line: "Received payment event: \(swapIdHash) \(payment.status)", level: "INFO") self.notifySuccess(payment: payment) } break @@ -69,12 +70,15 @@ class SwapUpdatedTask : TaskProtocol { } func notifySuccess(payment: Payment) { - self.logger.log(tag: TAG, line: "Payment \(payment.txId ?? "") completed successfully", level: "INFO") - let received = payment.paymentType == PaymentType.receive - let notificationTitle = ResourceHelper.shared.getString( - key: received ? Constants.PAYMENT_RECEIVED_NOTIFICATION_TITLE : Constants.PAYMENT_SENT_NOTIFICATION_TITLE, - validateContains: "%d", - fallback: received ? Constants.DEFAULT_PAYMENT_RECEIVED_NOTIFICATION_TITLE: Constants.DEFAULT_PAYMENT_SENT_NOTIFICATION_TITLE) - self.displayPushNotification(title: String(format: notificationTitle, payment.amountSat), logger: self.logger, threadIdentifier: Constants.NOTIFICATION_THREAD_SWAP_UPDATED) + if !self.notified { + self.logger.log(tag: TAG, line: "Payment \(payment.txId ?? "") processing successful", level: "INFO") + let received = payment.paymentType == PaymentType.receive + let notificationTitle = ResourceHelper.shared.getString( + key: received ? Constants.PAYMENT_RECEIVED_NOTIFICATION_TITLE : Constants.PAYMENT_SENT_NOTIFICATION_TITLE, + validateContains: "%d", + fallback: received ? Constants.DEFAULT_PAYMENT_RECEIVED_NOTIFICATION_TITLE: Constants.DEFAULT_PAYMENT_SENT_NOTIFICATION_TITLE) + self.notified = true + self.displayPushNotification(title: String(format: notificationTitle, payment.amountSat), logger: self.logger, threadIdentifier: Constants.NOTIFICATION_THREAD_SWAP_UPDATED) + } } }