Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom object issue in android bridge #337

Merged
merged 1 commit into from
Aug 9, 2024
Merged
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
Expand Up @@ -51,7 +51,13 @@ class NamiCampaignManagerBridgeModule(reactContext: ReactApplicationContext) :
}

@ReactMethod
fun launch(label: String?, withUrl: String?, context: ReadableMap?, resultCallback: Callback, actionCallback: Callback) {
fun launch(
label: String?,
withUrl: String?,
context: ReadableMap?,
resultCallback: Callback,
actionCallback: Callback,
) {
var theActivity: Activity? = null
if (reactApplicationContext.hasCurrentActivity()) {
theActivity = reactApplicationContext.getCurrentActivity()
Expand All @@ -61,7 +67,7 @@ class NamiCampaignManagerBridgeModule(reactContext: ReactApplicationContext) :
if (context != null) {
val productGroups: MutableList<String> = mutableListOf()
val customAttributes: MutableMap<String, String> = mutableMapOf()
val customObject: MutableMap<String, String> = mutableMapOf()
var customObject: MutableMap<String, Any> = mutableMapOf()

if (context.hasKey("productGroups")) {
val groups = context.getArray("productGroups")
Expand Down Expand Up @@ -91,12 +97,11 @@ class NamiCampaignManagerBridgeModule(reactContext: ReactApplicationContext) :
if (context.hasKey("customObject")) {
val attr = context.getMap("customObject")
if (attr != null) {
val keyIterator = attr.keySetIterator()
while (keyIterator.hasNextKey()) {
val key = keyIterator.nextKey()
customObject[key] = attr.getString(key) ?: ""
try {
customObject = attr.toHashMap().toMutableMap()
} catch (e: Exception) {
Log.d(LOG_TAG, "Unable to parse PaywallLaunchContext customObject $customObject")
}
Log.d(LOG_TAG, "customObject $customObject")
}
}

Expand Down Expand Up @@ -152,44 +157,46 @@ class NamiCampaignManagerBridgeModule(reactContext: ReactApplicationContext) :

val purchasesArray = createPurchaseArray(paywallEvent.purchases)

val resultMap = Arguments.createMap().apply {
putString(CAMPAIGN_ID, paywallEvent.campaignId)
putString(CAMPAIGN_LABEL, paywallEvent.campaignLabel ?: "")
putString(PAYWALL_ID, paywallEvent.paywallId)
putString(ACTION, actionString)
putString(SKU_ID, skuString)
putString(PURCHASE_ERROR, paywallEvent.purchaseError ?: "")
putArray(PURCHASES, purchasesArray)
putString(CAMPAIGN_NAME, paywallEvent.campaignName ?: "")
putString(CAMPAIGN_TYPE, paywallEvent.campaignType ?: "")
putString(CAMPAIGN_URL, paywallEvent.campaignUrl ?: "")
putString(PAYWALL_NAME, paywallEvent.paywallName ?: "")
putString(SEGMENT_ID, paywallEvent.segmentId ?: "")
putString(EXTERNAL_SEGMENT_ID, paywallEvent.externalSegmentId ?: "")
putString(DEEP_LINK_URL, paywallEvent.deeplinkUrl ?: "")

}
val resultMap =
Arguments.createMap().apply {
putString(CAMPAIGN_ID, paywallEvent.campaignId)
putString(CAMPAIGN_LABEL, paywallEvent.campaignLabel ?: "")
putString(PAYWALL_ID, paywallEvent.paywallId)
putString(ACTION, actionString)
putString(SKU_ID, skuString)
putString(PURCHASE_ERROR, paywallEvent.purchaseError ?: "")
putArray(PURCHASES, purchasesArray)
putString(CAMPAIGN_NAME, paywallEvent.campaignName ?: "")
putString(CAMPAIGN_TYPE, paywallEvent.campaignType ?: "")
putString(CAMPAIGN_URL, paywallEvent.campaignUrl ?: "")
putString(PAYWALL_NAME, paywallEvent.paywallName ?: "")
putString(SEGMENT_ID, paywallEvent.segmentId ?: "")
putString(EXTERNAL_SEGMENT_ID, paywallEvent.externalSegmentId ?: "")
putString(DEEP_LINK_URL, paywallEvent.deeplinkUrl ?: "")
}

if (paywallEvent.componentChange != null) {
val componentChangeMap = Arguments.createMap().apply {
putString("id", paywallEvent.componentChange?.id ?: "")
putString("name", paywallEvent.componentChange?.name ?: "")
}
val componentChangeMap =
Arguments.createMap().apply {
putString("id", paywallEvent.componentChange?.id ?: "")
putString("name", paywallEvent.componentChange?.name ?: "")
}

resultMap.putMap(COMPONENT_CHANGE, componentChangeMap)
}

if (paywallEvent.videoMetadata != null) {
val videoMetadataMap = Arguments.createMap().apply {
putString("id", paywallEvent.videoMetadata?.id ?: "")
putString("name", paywallEvent.videoMetadata?.name ?: "")
putString("url", paywallEvent.videoMetadata?.url ?: "")
putBoolean("autoplayVideo", paywallEvent.videoMetadata?.autoplayVideo ?: false)
putBoolean("muteByDefault", paywallEvent.videoMetadata?.muteByDefault ?: false)
putBoolean("loopVideo", paywallEvent.videoMetadata?.loopVideo ?: false)
putDouble("contentDuration", paywallEvent.videoMetadata?.contentDuration ?: 0.0)
putDouble("contentTimecode", paywallEvent.videoMetadata?.contentTimecode ?: 0.0)
}
val videoMetadataMap =
Arguments.createMap().apply {
putString("id", paywallEvent.videoMetadata?.id ?: "")
putString("name", paywallEvent.videoMetadata?.name ?: "")
putString("url", paywallEvent.videoMetadata?.url ?: "")
putBoolean("autoplayVideo", paywallEvent.videoMetadata?.autoplayVideo ?: false)
putBoolean("muteByDefault", paywallEvent.videoMetadata?.muteByDefault ?: false)
putBoolean("loopVideo", paywallEvent.videoMetadata?.loopVideo ?: false)
putDouble("contentDuration", paywallEvent.videoMetadata?.contentDuration ?: 0.0)
putDouble("contentTimecode", paywallEvent.videoMetadata?.contentTimecode ?: 0.0)
}

resultMap.putMap(VIDEO_METADATA, videoMetadataMap)
}
Expand All @@ -213,7 +220,10 @@ class NamiCampaignManagerBridgeModule(reactContext: ReactApplicationContext) :
}
}

private fun emitEvent(event: String, map: WritableMap) {
private fun emitEvent(
event: String,
map: WritableMap,
) {
val emitter = reactApplicationContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
if (emitter is DeviceEventManagerModule.RCTDeviceEventEmitter) {
emitter.emit(event, map)
Expand All @@ -222,7 +232,10 @@ class NamiCampaignManagerBridgeModule(reactContext: ReactApplicationContext) :
}
}

private fun handleResult(result: LaunchCampaignResult, resultCallback: Callback) {
private fun handleResult(
result: LaunchCampaignResult,
resultCallback: Callback,
) {
val resultMap = Arguments.createMap()
when (result) {
is LaunchCampaignResult.Success -> {
Expand Down Expand Up @@ -258,18 +271,22 @@ class NamiCampaignManagerBridgeModule(reactContext: ReactApplicationContext) :
}

@ReactMethod
fun isCampaignAvailable(campaignSource: String?, promise: Promise) {
val isCampaignAvailable = when {
campaignSource == null -> NamiCampaignManager.isCampaignAvailable()
Uri.parse(campaignSource)?.scheme != null -> NamiCampaignManager.isCampaignAvailable(Uri.parse(campaignSource))
else -> NamiCampaignManager.isCampaignAvailable(campaignSource)
}
fun isCampaignAvailable(
campaignSource: String?,
promise: Promise,
) {
val isCampaignAvailable =
when {
campaignSource == null -> NamiCampaignManager.isCampaignAvailable()
Uri.parse(campaignSource)?.scheme != null -> NamiCampaignManager.isCampaignAvailable(Uri.parse(campaignSource))
else -> NamiCampaignManager.isCampaignAvailable(campaignSource)
}
promise.resolve(isCampaignAvailable)
}

@ReactMethod
fun refresh() {
NamiCampaignManager.refresh() { }
NamiCampaignManager.refresh { }
}

@ReactMethod
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-nami-sdk",
"version": "3.2.2",
"version": "3.2.2-2",
"description": "React Native Module for Nami - Easy subscriptions & in-app purchases, with powerful built-in paywalls and A/B testing.",
"main": "index.ts",
"types": "index.d.ts",
Expand Down
Loading