mirror of
https://github.com/aljazceru/breez-sdk-liquid.git
synced 2026-01-06 23:54:26 +01:00
SDK events framework (#193)
* Add events framework * Adapt RN codegen and add synced event * Only use get_connection internally
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.breezliquidsdk
|
||||
|
||||
import breez_liquid_sdk.LiquidSdkEvent
|
||||
import breez_liquid_sdk.EventListener
|
||||
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
|
||||
|
||||
class BreezLiquidSDKEventListener(private val emitter: RCTDeviceEventEmitter) : EventListener {
|
||||
private var id: String? = null
|
||||
|
||||
fun setId(id: String) {
|
||||
this.id = id
|
||||
}
|
||||
|
||||
override fun onEvent(e: LiquidSdkEvent) {
|
||||
this.id?.let {
|
||||
emitter.emit("event-$it", readableMapOf(e))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -420,6 +420,78 @@ fun asSendPaymentResponseList(arr: ReadableArray): List<SendPaymentResponse> {
|
||||
return list
|
||||
}
|
||||
|
||||
fun asLiquidSdkEvent(liquidSdkEvent: ReadableMap): LiquidSdkEvent? {
|
||||
val type = liquidSdkEvent.getString("type")
|
||||
|
||||
if (type == "paymentFailed") {
|
||||
return LiquidSdkEvent.PaymentFailed(liquidSdkEvent.getMap("details")?.let { asPayment(it) }!!)
|
||||
}
|
||||
if (type == "paymentPending") {
|
||||
return LiquidSdkEvent.PaymentPending(liquidSdkEvent.getMap("details")?.let { asPayment(it) }!!)
|
||||
}
|
||||
if (type == "paymentRefunded") {
|
||||
return LiquidSdkEvent.PaymentRefunded(liquidSdkEvent.getMap("details")?.let { asPayment(it) }!!)
|
||||
}
|
||||
if (type == "paymentRefundPending") {
|
||||
return LiquidSdkEvent.PaymentRefundPending(liquidSdkEvent.getMap("details")?.let { asPayment(it) }!!)
|
||||
}
|
||||
if (type == "paymentSucceed") {
|
||||
return LiquidSdkEvent.PaymentSucceed(liquidSdkEvent.getMap("details")?.let { asPayment(it) }!!)
|
||||
}
|
||||
if (type == "paymentWaitingConfirmation") {
|
||||
return LiquidSdkEvent.PaymentWaitingConfirmation(liquidSdkEvent.getMap("details")?.let { asPayment(it) }!!)
|
||||
}
|
||||
if (type == "synced") {
|
||||
return LiquidSdkEvent.Synced
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun readableMapOf(liquidSdkEvent: LiquidSdkEvent): ReadableMap? {
|
||||
val map = Arguments.createMap()
|
||||
when (liquidSdkEvent) {
|
||||
is LiquidSdkEvent.PaymentFailed -> {
|
||||
pushToMap(map, "type", "paymentFailed")
|
||||
pushToMap(map, "details", readableMapOf(liquidSdkEvent.details))
|
||||
}
|
||||
is LiquidSdkEvent.PaymentPending -> {
|
||||
pushToMap(map, "type", "paymentPending")
|
||||
pushToMap(map, "details", readableMapOf(liquidSdkEvent.details))
|
||||
}
|
||||
is LiquidSdkEvent.PaymentRefunded -> {
|
||||
pushToMap(map, "type", "paymentRefunded")
|
||||
pushToMap(map, "details", readableMapOf(liquidSdkEvent.details))
|
||||
}
|
||||
is LiquidSdkEvent.PaymentRefundPending -> {
|
||||
pushToMap(map, "type", "paymentRefundPending")
|
||||
pushToMap(map, "details", readableMapOf(liquidSdkEvent.details))
|
||||
}
|
||||
is LiquidSdkEvent.PaymentSucceed -> {
|
||||
pushToMap(map, "type", "paymentSucceed")
|
||||
pushToMap(map, "details", readableMapOf(liquidSdkEvent.details))
|
||||
}
|
||||
is LiquidSdkEvent.PaymentWaitingConfirmation -> {
|
||||
pushToMap(map, "type", "paymentWaitingConfirmation")
|
||||
pushToMap(map, "details", readableMapOf(liquidSdkEvent.details))
|
||||
}
|
||||
is LiquidSdkEvent.Synced -> {
|
||||
pushToMap(map, "type", "synced")
|
||||
}
|
||||
}
|
||||
return map
|
||||
}
|
||||
|
||||
fun asLiquidSdkEventList(arr: ReadableArray): List<LiquidSdkEvent> {
|
||||
val list = ArrayList<LiquidSdkEvent>()
|
||||
for (value in arr.toArrayList()) {
|
||||
when (value) {
|
||||
is ReadableMap -> list.add(asLiquidSdkEvent(value)!!)
|
||||
else -> throw LiquidSdkException.Generic(errUnexpectedType("${value::class.java.name}"))
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
fun asNetwork(type: String): Network {
|
||||
return Network.valueOf(camelToUpperSnakeCase(type))
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.breezliquidsdk
|
||||
|
||||
import breez_liquid_sdk.*
|
||||
import com.facebook.react.bridge.*
|
||||
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
|
||||
import java.util.*
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
@@ -66,6 +67,37 @@ class BreezLiquidSDKModule(reactContext: ReactApplicationContext) : ReactContext
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
fun addEventListener(promise: Promise) {
|
||||
executor.execute {
|
||||
try {
|
||||
val emitter = reactApplicationContext.getJSModule(RCTDeviceEventEmitter::class.java)
|
||||
var eventListener = BreezLiquidSDKEventListener(emitter)
|
||||
val res = getBindingLiquidSdk().addEventListener(eventListener)
|
||||
|
||||
eventListener.setId(res)
|
||||
promise.resolve(res)
|
||||
} catch (e: Exception) {
|
||||
promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
fun removeEventListener(
|
||||
id: String,
|
||||
promise: Promise,
|
||||
) {
|
||||
executor.execute {
|
||||
try {
|
||||
getBindingLiquidSdk().removeEventListener(id)
|
||||
promise.resolve(readableMapOf("status" to "ok"))
|
||||
} catch (e: Exception) {
|
||||
promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
fun getInfo(
|
||||
req: ReadableMap,
|
||||
|
||||
Reference in New Issue
Block a user