diff --git a/android/src/main/java/com/sourcepoint/reactnativecmp/EventEmitter.kt b/android/src/main/java/com/sourcepoint/reactnativecmp/EventEmitter.kt new file mode 100644 index 0000000..4be2357 --- /dev/null +++ b/android/src/main/java/com/sourcepoint/reactnativecmp/EventEmitter.kt @@ -0,0 +1,60 @@ +package com.sourcepoint.reactnativecmp + +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReadableMap +import com.facebook.react.modules.core.DeviceEventManagerModule + +interface SPEventEmitter { + fun emitOnAction(params: ReadableMap?) + fun emitOnSPUIReady() + fun emitOnSPUIFinished() + fun emitOnFinished() + fun emitOnMessageInactivityTimeout() + fun emitOnError(params: ReadableMap?) +} + +class EventEmitter(val reactContext: ReactApplicationContext): SPEventEmitter { + private enum class EventName(val value: String) { + OnAction("onAction"), + OnSPUIReady("onSPUIReady"), + OnSPUIFinished("onSPUIFinished"), + OnFinished("onFinished"), + OnMessageInactivityTimeout("onMessageInactivityTimeout"), + OnError("onError") + } + + private fun emitEvent(name: EventName, params: ReadableMap? = null) { + if (!reactContext.hasActiveReactInstance()) return + + try { + reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) + .emit(name.value, params) + } catch (t: Throwable) { + t.printStackTrace() + } + } + + override fun emitOnAction(params: ReadableMap?) { + emitEvent(EventName.OnAction, params) + } + + override fun emitOnSPUIReady() { + emitEvent(EventName.OnSPUIReady) + } + + override fun emitOnSPUIFinished() { + emitEvent(EventName.OnSPUIFinished) + } + + override fun emitOnFinished() { + emitEvent(EventName.OnFinished) + } + + override fun emitOnMessageInactivityTimeout() { + emitEvent(EventName.OnMessageInactivityTimeout) + } + + override fun emitOnError(params: ReadableMap?) { + emitEvent(EventName.OnError, params) + } +} diff --git a/android/src/main/java/com/sourcepoint/reactnativecmp/ReactNativeCmpModule.kt b/android/src/main/java/com/sourcepoint/reactnativecmp/ReactNativeCmpModule.kt index ed2a2fd..03f99d8 100644 --- a/android/src/main/java/com/sourcepoint/reactnativecmp/ReactNativeCmpModule.kt +++ b/android/src/main/java/com/sourcepoint/reactnativecmp/ReactNativeCmpModule.kt @@ -26,8 +26,9 @@ data class SPLoadMessageParams(val authId: String?) { constructor(fromReadableMap: ReadableMap?) : this(authId = fromReadableMap?.getString("authId")) } -class ReactNativeCmpModule(reactContext: ReactApplicationContext) : NativeReactNativeCmpSpec(reactContext), - SpClient { +class ReactNativeCmpModule(reactContext: ReactApplicationContext): NativeReactNativeCmpSpec(reactContext), SpClient { + private val eventEmitter = EventEmitter(reactContext) + private var spConsentLib: SpConsentLib? = null override fun getName() = NAME @@ -162,7 +163,7 @@ class ReactNativeCmpModule(reactContext: ReactApplicationContext) : NativeReactN } override fun onAction(view: View, consentAction: ConsentAction): ConsentAction { - emitOnAction(createMap().apply { + eventEmitter.emitOnAction(createMap().apply { putString("actionType", RNSourcepointActionType.from(consentAction.actionType).name) putString("customActionId", consentAction.customActionId) }) @@ -172,27 +173,27 @@ class ReactNativeCmpModule(reactContext: ReactApplicationContext) : NativeReactN override fun onConsentReady(consent: SPConsents) {} override fun onError(error: Throwable) { - emitOnError(createMap().apply { putString("description", error.message) }) + eventEmitter.emitOnError(createMap().apply { putString("description", error.message) }) } override fun onNoIntentActivitiesFound(url: String) {} override fun onSpFinished(sPConsents: SPConsents) { - emitOnFinished() + eventEmitter.emitOnFinished() } override fun onMessageInactivityTimeout() { - emitOnMessageInactivityTimeout() + eventEmitter.emitOnMessageInactivityTimeout() } override fun onUIFinished(view: View) { spConsentLib?.removeView(view) - emitOnSPUIFinished() + eventEmitter.emitOnSPUIFinished() } override fun onUIReady(view: View) { spConsentLib?.showView(view) - emitOnSPUIReady() + eventEmitter.emitOnSPUIReady() } private fun userConsentsToWriteableMap(consents: SPConsents) = RNSPUserData(consents).toRN()