diff --git a/android/src/main/java/com/sourcepoint/reactnativecmp/ReactNativeCmpModule.kt b/android/src/main/java/com/sourcepoint/reactnativecmp/ReactNativeCmpModule.kt index ed2a2fd..c64ba41 100644 --- a/android/src/main/java/com/sourcepoint/reactnativecmp/ReactNativeCmpModule.kt +++ b/android/src/main/java/com/sourcepoint/reactnativecmp/ReactNativeCmpModule.kt @@ -1,7 +1,6 @@ package com.sourcepoint.reactnativecmp import android.view.View -import com.facebook.react.bridge.Arguments.createMap import com.facebook.react.bridge.Callback import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext @@ -21,6 +20,8 @@ import com.sourcepoint.reactnativecmp.arguments.BuildOptions import com.sourcepoint.reactnativecmp.arguments.toList import com.sourcepoint.reactnativecmp.consents.RNSPGDPRConsent import com.sourcepoint.reactnativecmp.consents.RNSPUserData +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json data class SPLoadMessageParams(val authId: String?) { constructor(fromReadableMap: ReadableMap?) : this(authId = fromReadableMap?.getString("authId")) @@ -139,8 +140,8 @@ class ReactNativeCmpModule(reactContext: ReactApplicationContext) : NativeReactN vendors: ReadableArray, categories: ReadableArray, legIntCategories: ReadableArray, - callback: Callback) - { + callback: Callback + ) { runOnMainThread { spConsentLib?.deleteCustomConsentTo( vendors.toList(), @@ -162,17 +163,17 @@ class ReactNativeCmpModule(reactContext: ReactApplicationContext) : NativeReactN } override fun onAction(view: View, consentAction: ConsentAction): ConsentAction { - emitOnAction(createMap().apply { - putString("actionType", RNSourcepointActionType.from(consentAction.actionType).name) - putString("customActionId", consentAction.customActionId) - }) + emitInternalOnAction(Json.encodeToString(mapOf( + "actionType" to RNSourcepointActionType.from(consentAction.actionType).name, + "customActionId" to consentAction.customActionId + ))) return consentAction } override fun onConsentReady(consent: SPConsents) {} override fun onError(error: Throwable) { - emitOnError(createMap().apply { putString("description", error.message) }) + emitInternalOnError(Json.encodeToString(mapOf("description" to error.message))) } override fun onNoIntentActivitiesFound(url: String) {} diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 1236731..1dde7ac 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1659,7 +1659,7 @@ PODS: - React-logger (= 0.79.2) - React-perflogger (= 0.79.2) - React-utils (= 0.79.2) - - ReactNativeCmp (1.0.8): + - ReactNativeCmp (1.0.9-beta.2): - ConsentViewController (= 7.12.1) - DoubleConversion - glog @@ -1989,11 +1989,11 @@ SPEC CHECKSUMS: ReactAppDependencyProvider: 04d5eb15eb46be6720e17a4a7fa92940a776e584 ReactCodegen: c63eda03ba1d94353fb97b031fc84f75a0d125ba ReactCommon: 76d2dc87136d0a667678668b86f0fca0c16fdeb0 - ReactNativeCmp: 332f0bc3333fc115006179d18a0dc7809d8e1c5f + ReactNativeCmp: d79d97ff49c3e5ca3aa5f8a6608dbe06a30e7c7a SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748 SPMobileCore: 220d109e404cb17169f7e26f2a34c6022b80079a Yoga: c758bfb934100bb4bf9cbaccb52557cee35e8bdf PODFILE CHECKSUM: decdc7519d77aa5eae65b167fa59bcfce25e15d2 -COCOAPODS: 1.15.2 +COCOAPODS: 1.16.2 diff --git a/ios/RNSourcepointCmp.swift b/ios/RNSourcepointCmp.swift index 6266f58..246d1f4 100644 --- a/ios/RNSourcepointCmp.swift +++ b/ios/RNSourcepointCmp.swift @@ -28,9 +28,18 @@ import React self.customActionId = customActionId } - @objc public func toDictionary() -> [String: Any] { + func toDictionary() -> [String: Any] { ["actionType": type.description, "customActionId": customActionId] } + + @objc public func stringifiedJson() -> String { + if let jsonData = try? JSONSerialization.data(withJSONObject: toDictionary()), + let jsonString = String(data: jsonData, encoding: .utf8) { + return jsonString + } else { + return "{\"actionType\":\"unknown\"}" + } + } } @objc public protocol ReactNativeCmpImplDelegate { diff --git a/ios/ReactNativeCmp.mm b/ios/ReactNativeCmp.mm index 76e7878..f63d3f5 100644 --- a/ios/ReactNativeCmp.mm +++ b/ios/ReactNativeCmp.mm @@ -130,11 +130,14 @@ - (void)postDeleteCustomConsentGDPR:(NSArray *)vendors // MARK: SPDelegate - (void)onAction:(RNAction*)action { - [self emitOnAction: [action toDictionary]]; + [self emitInternalOnAction: [action stringifiedJson]]; } - (void)onErrorWithDescription:(NSString * _Nonnull)description { - [self emitOnError: @{ @"description": description }]; + NSDictionary *dict = @{@"description": description}; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil]; + NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; + [self emitInternalOnError: json]; } - (void)onFinished { diff --git a/package.json b/package.json index 48c893c..f2dd1e0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sourcepoint/react-native-cmp", - "version": "1.0.8", + "version": "1.0.9-beta.2", "description": "The official react native bridge to the native Sourcepoint SDKs", "main": "./lib/module/index.js", "types": "./lib/typescript/src/index.d.ts", diff --git a/src/NativeReactNativeCmp.ts b/src/NativeReactNativeCmp.ts index 4d34d02..a9a5b61 100644 --- a/src/NativeReactNativeCmp.ts +++ b/src/NativeReactNativeCmp.ts @@ -201,6 +201,10 @@ export type SPBuildOptions = { androidDismissMessageOnBackPress?: boolean; } +export type SPError = { + description: string; +}; + export interface Spec extends TurboModule { build( accountId: number, @@ -230,12 +234,12 @@ export interface Spec extends TurboModule { callback: (consent: GDPRConsent) => void ): void; - readonly onAction: EventEmitter; + readonly internalOnAction: EventEmitter; readonly onSPUIReady: EventEmitter; readonly onSPUIFinished: EventEmitter; readonly onFinished: EventEmitter; readonly onMessageInactivityTimeout: EventEmitter; - readonly onError: EventEmitter<{ description: string }>; + readonly internalOnError: EventEmitter; } export default TurboModuleRegistry.getEnforcing('ReactNativeCmp'); diff --git a/src/index.tsx b/src/index.tsx index 56fdf35..1ea577b 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -6,6 +6,7 @@ import type { SPAction, SPBuildOptions, GDPRConsent, + SPError } from './NativeReactNativeCmp'; import ReactNativeCmp, { SPMessageLanguage } from './NativeReactNativeCmp'; import type { EventEmitter } from 'react-native/Libraries/Types/CodegenTypes'; @@ -18,6 +19,32 @@ const defaultBuildOptions: SPBuildOptions = { } export default class SPConsentManager implements Spec { + /** intended to be used by the SDK only */ + internalOnAction: EventEmitter = ReactNativeCmp.internalOnAction; + /** intended to be used by the SDK only */ + internalOnError: EventEmitter = ReactNativeCmp.internalOnError; + + onSPUIReady: EventEmitter = ReactNativeCmp.onSPUIReady; + onSPUIFinished: EventEmitter = ReactNativeCmp.onSPUIFinished; + onFinished: EventEmitter = ReactNativeCmp.onFinished; + onMessageInactivityTimeout: EventEmitter = ReactNativeCmp.onMessageInactivityTimeout; + + onAction(handler: (action: SPAction) => void) { + ReactNativeCmp.internalOnAction((stringifiedAction) => { + handler(JSON.parse(stringifiedAction) as SPAction); + }); + } + + onError(handler: (error: SPError) => void) { + ReactNativeCmp.internalOnError((stringifiedError) => { + handler(JSON.parse(stringifiedError) as SPError); + }); + } + + getConstants?(): {} { + throw new Error('Method not implemented.'); + } + build( accountId: number, propertyId: number, @@ -74,14 +101,7 @@ export default class SPConsentManager implements Spec { categories: string[], legIntCategories: string[], callback: (consent: GDPRConsent) => void - ) { - ReactNativeCmp.postDeleteCustomConsentGDPR(vendors, categories, legIntCategories, callback); + ) { + ReactNativeCmp.postDeleteCustomConsentGDPR(vendors, categories, legIntCategories, callback); } - - onAction: EventEmitter = ReactNativeCmp.onAction; - onSPUIReady: EventEmitter = ReactNativeCmp.onSPUIReady; - onSPUIFinished: EventEmitter = ReactNativeCmp.onSPUIFinished; - onFinished: EventEmitter = ReactNativeCmp.onFinished; - onMessageInactivityTimeout: EventEmitter = ReactNativeCmp.onMessageInactivityTimeout; - onError: EventEmitter<{ description: string }> = ReactNativeCmp.onError; }