Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
Expand All @@ -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()
Expand Down
Loading