// AppConsentModule.java package com.reactlibrary import com.facebook.react.bridge.* import com.sfbx.appconsent.core.AppConsentError import com.sfbx.appconsent.core.listener.AppConsentNoticeListener import com.sfbx.appconsent.core.model.ConsentStatus import com.sfbx.appconsent.core.model.ConsentableType import com.sfbx.appconsent.ui.AppConsentUI class RNAppConsentModule(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule( reactContext ) { private var appconsentUi: AppConsentUI? = null private var appKey: String? = null private var forceApplyGDPR: Boolean = false override fun getName(): String { return "RNAppConsent" } override fun getConstants(): MutableMap { return mutableMapOf(Pair("version", "1.1.22")) } @ReactMethod fun requiresMainQueueSetup(): Boolean { return true } @ReactMethod fun configureWith(appKey: String, forceApplyGDPR: Boolean, forceATT: Boolean) { this.appKey = appKey this.forceApplyGDPR = forceApplyGDPR appconsentUi = AppConsentUI( context = reactContext, appKey = appKey, forceApplyGDPR = forceApplyGDPR, onReady = { } ) } @ReactMethod fun present(force: Boolean) { appconsentUi?.presentNotice(force) } @ReactMethod fun checkForUpdate(promise: Promise) { appconsentUi?.checkForUpdate(onResult = { promise.resolve(it) }, error = { promise.reject(it) }) } @ReactMethod fun isGDPRCountry(promise: Promise) { promise.resolve(appconsentUi?.isSubjectToGDPR()) } @ReactMethod fun consentAlreadyGiven(promise: Promise) { promise.resolve(appconsentUi?.consentGiven()) } @ReactMethod fun consentGiven(promise: Promise) { appconsentUi?.addNoticeListener(object : AppConsentNoticeListener { override fun onConsentGiven() { promise.resolve(appconsentUi?.consentGiven()) } override fun onError(error: AppConsentError) { promise.reject(error.cause) } }) } @ReactMethod fun clearConsent() { appconsentUi?.clearConsent() } @ReactMethod fun extraConsentableAllowed(extraId: String?, promise: Promise) { promise.resolve(appconsentUi?.extraConsentableAllowed(extraId!!)) } @ReactMethod fun extraVendorAllowed(extraId: String?, promise: Promise) { promise.resolve(appconsentUi?.extraVendorAllowed(extraId!!)) } @ReactMethod fun extraFloatingPurposeAllowed(extraId: String?, promise: Promise) { promise.resolve(appconsentUi?.extraFloatingAllowed(extraId!!)) } @ReactMethod fun setConsentableConsent(values: ReadableMap, promise: Promise) { val map = values.toHashMap().entries.associate { entry -> entry.key.toInt() to ConsentStatus.values() .first { it.ordinal == (entry.value as Double).toInt() } } appconsentUi?.setConsentableConsents(map, success = { promise.resolve(true) }, error = { promise.reject(it) }) } @ReactMethod fun setExtraConsentableConsent(values: ReadableMap, promise: Promise) { val map = values.toHashMap().mapValues { entry -> ConsentStatus.values().first { it.ordinal == (entry.value as Double).toInt() } } appconsentUi?.setExtraConsentableConsents(map, success = { promise.resolve(true) }, error = { promise.reject(it) } ) } // Deprecated @ReactMethod fun consentableAllowedByObjectId(id: String, promise: Promise) { val intId = id.toInt() promise.resolve(appconsentUi?.consentableAllowed(intId)) } @ReactMethod fun consentableAllowedByIABId(iabId: String, type: Int, promise: Promise) { val intId = iabId.toInt() val consentableType: ConsentableType = when (type) { 0 -> ConsentableType.PURPOSE 1 -> ConsentableType.FEATURE 2 -> ConsentableType.SPECIAL_FEATURE 3 -> ConsentableType.SPECIAL_PURPOSE else -> ConsentableType.UNKNOWN } promise.resolve(appconsentUi?.consentableAllowed(intId, consentableType)) } @ReactMethod fun vendorAllowedByIABId(iabId: String, promise: Promise) { val intId = iabId.toInt() promise.resolve(appconsentUi?.vendorAllowed(intId)) } @ReactMethod fun stackAllowedByIABId(iabId: String, promise: Promise) { val intId = iabId.toInt() promise.resolve(appconsentUi?.stackAllowed(intId)) } @ReactMethod fun getAppKey(promise: Promise) { promise.resolve(appKey) } @ReactMethod fun getForceApplyGDPR(promise: Promise) { promise.resolve(forceApplyGDPR) } }