mirror of
https://github.com/aljazceru/breez-sdk-liquid.git
synced 2026-02-11 09:14:28 +01:00
Notify success when waiting for claim confirmation (#562)
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user