Skip to content

Commit

Permalink
[MP-2072] Reinstate device attributes (#124)
Browse files Browse the repository at this point in the history
* [MP-2072] Reinstate device attributes

* [MP-2072] Fix array handling and improve test coverage
  • Loading branch information
igstewart3 authored Dec 11, 2024
1 parent c4e55d0 commit db8a7fb
Show file tree
Hide file tree
Showing 14 changed files with 1,220 additions and 71 deletions.
5 changes: 4 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ android {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
}

repositories {
Expand All @@ -83,7 +86,7 @@ def kotlin_version = getExtOrDefault("kotlinVersion")
dependencies {
//noinspection GradleDynamicVersion
api "com.facebook.react:react-android:+"
api "com.marigold.sdk:marigold:23.0.0"
api "com.marigold.sdk:marigold:24.0.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

testImplementation 'org.mockito.kotlin:mockito-kotlin:5.3.1'
Expand Down
121 changes: 121 additions & 0 deletions android/src/main/java/com/marigold/rnsdk/RNEngageBySailthruModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.marigold.sdk.EngageBySailthru
import com.marigold.sdk.Marigold
import com.marigold.sdk.enums.MergeRules
import com.marigold.sdk.model.AttributeMap
import com.marigold.sdk.model.Purchase
import org.json.JSONException
import org.json.JSONObject
import java.net.URI
import java.net.URISyntaxException
import java.util.ArrayList
import java.util.Date

class RNEngageBySailthruModule (reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

Expand Down Expand Up @@ -58,6 +61,51 @@ class RNEngageBySailthruModule (reactContext: ReactApplicationContext) : ReactCo
})
}

@ReactMethod
fun setAttributes(readableMap: ReadableMap, promise: Promise) {
val attributeMap = try {
getAttributeMap(readableMap)
} catch (e: JSONException) {
promise.reject(ERROR_CODE_DEVICE, e.message)
return
}
createEngageBySailthru(promise)?.setAttributes(attributeMap, object : EngageBySailthru.AttributesHandler {
override fun onSuccess() {
promise.resolve(null)
}

override fun onFailure(error: Error) {
promise.reject(ERROR_CODE_DEVICE, error.message)
}
})
}

@ReactMethod
fun removeAttribute(key: String, promise: Promise) {
createEngageBySailthru(promise)?.removeAttribute(key, object : EngageBySailthru.AttributesHandler {
override fun onSuccess() {
promise.resolve(null)
}

override fun onFailure(error: Error) {
promise.reject(ERROR_CODE_DEVICE, error.message)
}
})
}

@ReactMethod
fun clearAttributes(promise: Promise) {
createEngageBySailthru(promise)?.clearAttributes(object : EngageBySailthru.AttributesHandler {
override fun onSuccess() {
promise.resolve(true)
}

override fun onFailure(error: Error) {
promise.reject(ERROR_CODE_DEVICE, error.message)
}
})
}

@ReactMethod
fun setUserId(userId: String?, promise: Promise) {
createEngageBySailthru(promise)?.setUserId(userId, object : Marigold.MarigoldHandler<Void?> {
Expand Down Expand Up @@ -240,4 +288,77 @@ class RNEngageBySailthruModule (reactContext: ReactApplicationContext) : ReactCo
promise.reject(ERROR_CODE_PURCHASE, e.message)
null
}

@VisibleForTesting
@Throws(JSONException::class)
fun getAttributeMap(readableMap: ReadableMap): AttributeMap {
val attributeMapJson = jsonConverter.convertMapToJson(readableMap)
val attributes = attributeMapJson.getJSONObject("attributes")
val attributeMap = AttributeMap()
val mergeRule = MergeRules.entries[attributeMapJson.getInt("mergeRule")]
attributeMap.setMergeRules(mergeRule)
attributes.keys().forEach { key ->
val attribute = attributes.getJSONObject(key)
convertValue(key, attribute, attributeMap)
}
return attributeMap
}

private fun convertValue(key: String, attribute: JSONObject, attributeMap: AttributeMap) {
val attributeType = attribute.getString("type")
when (attributeType) {
"string" -> {
attributeMap.putString(key, attribute.getString("value"))
}
"stringArray" -> {
val array: ArrayList<String> = ArrayList()
val values = attribute.getJSONArray("value")
for (i in 0 until values.length()) {
array.add(values.get(i) as String)
}
attributeMap.putStringArray(key, array)
}
"integer" -> {
attributeMap.putInt(key, attribute.getInt("value"))
}
"integerArray" -> {
val array: ArrayList<Int> = ArrayList()
val values = attribute.getJSONArray("value")
for (i in 0 until values.length()) {
val j = values.getInt(i)
array.add(j)
}
attributeMap.putIntArray(key, array)
}
"boolean" -> {
attributeMap.putBoolean(key, attribute.getBoolean("value"))
}
"float" -> {
attributeMap.putFloat(key, attribute.getDouble("value").toFloat())
}
"floatArray" -> {
val array: ArrayList<Float> = ArrayList()
val values = attribute.getJSONArray("value")
for (i in 0 until values.length()) {
val value = (values.get(i).toString()).toFloat()
array.add(value)
}
attributeMap.putFloatArray(key, array)
}
"date" -> {
val value = Date(attribute.getLong("value"))
attributeMap.putDate(key, value)
}
"dateArray" -> {
val array: ArrayList<Date> = ArrayList()
val values = attribute.getJSONArray("value")
for (i in 0 until values.length()) {
val dateValue = values.getLong(i)
val date = Date(dateValue)
array.add(date)
}
attributeMap.putDateArray(key, array)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RNMarigoldModule(reactContext: ReactApplicationContext) : ReactContextBase
val setWrapperMethod = companionClass.getDeclaredMethod("setWrapper", *cArg)

setWrapperMethod.isAccessible = true
setWrapperMethod.invoke(Marigold.Companion, "React Native", "13.0.0")
setWrapperMethod.invoke(Marigold.Companion, "React Native", "14.0.0")
} catch (e: NoSuchMethodException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
Expand Down
8 changes: 4 additions & 4 deletions android/src/test/java/com/marigold/rnsdk/JsonConverterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class JsonConverterTest {

val writableArray = jsonConverter.convertJsonToArray(jsonArray)

assertEquals("thing", writableArray.getMap(0).getString("some"))
assertEquals("thing", writableArray.getMap(0)?.getString("some"))
}

@Test
Expand All @@ -195,7 +195,7 @@ class JsonConverterTest {

val writableArray = jsonConverter.convertJsonToArray(jsonArray)

assertEquals("inner string", writableArray.getArray(0).getString(0))
assertEquals("inner string", writableArray.getArray(0)?.getString(0))
}

@Test
Expand All @@ -221,8 +221,8 @@ class JsonConverterTest {
assertEquals(123, writableArray.getInt(1))
assertEquals(1.23, writableArray.getDouble(2), 0.001)
assertEquals("test string", writableArray.getString(3))
assertEquals("thing", writableArray.getMap(4).getString("some"))
assertEquals("inner string", writableArray.getArray(5).getString(0))
assertEquals("thing", writableArray.getMap(4)?.getString("some"))
assertEquals("inner string", writableArray.getArray(5)?.getString(0))
}


Expand Down
Loading

0 comments on commit db8a7fb

Please sign in to comment.