package com.blux import android.content.Context import android.util.Log import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule import com.facebook.react.bridge.ReactMethod import com.facebook.react.bridge.Promise import com.blux_android_sdk.BluxClient import com.facebook.react.bridge.ReadableArray import com.facebook.react.bridge.ReadableMap import com.facebook.react.bridge.ReadableType import org.json.JSONObject import java.lang.Exception class BluxModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { private var context: Context? = null override fun getName(): String { return NAME } // Example method // See https://reactnative.dev/docs/native-modules-android @ReactMethod fun multiply(a: Double, b: Double, promise: Promise) { promise.resolve(a * b) } @ReactMethod fun initialize(bluxClientId:String, bluxSecretKey: String, requestPermissionOnLaunch: Boolean, promise: Promise){ try { this.context = reactApplicationContext.applicationContext BluxClient.init(this.context, bluxClientId, bluxSecretKey, requestPermissionOnLaunch) promise.resolve(null) }catch ( e: Exception){ Log.e("Blux", Log.getStackTraceString(e)) promise.reject(e) } } private fun convertLogLevelToInt(level: String):Int? { return when (level) { "error" -> Log.ERROR "verbose" -> Log.VERBOSE else -> null } } @ReactMethod fun setLogLevel(logLevel: String, promise: Promise) { val logLevelAsInt = convertLogLevelToInt(logLevel) ?: return BluxClient.setLogLevel(logLevelAsInt) promise.resolve(null) } @ReactMethod fun setUserId(userId: String, promise: Promise) { BluxClient.setUserId(this.context,userId) promise.resolve(null) } private fun readableArrayToList(readableArray: ReadableArray): List { val list = mutableListOf() for (i in 0 until readableArray.size()) { val readableMap = readableArray.getMap(i) val jsonObject = readableMap?.toJSONObject() if (jsonObject != null) { list.add(jsonObject) } } return list } private fun ReadableMap.toJSONObject(): JSONObject { val jsonObject = JSONObject() val iterator = this.keySetIterator() while (iterator.hasNextKey()) { val key = iterator.nextKey() when (this.getType(key)) { ReadableType.Null -> jsonObject.put(key, JSONObject.NULL) ReadableType.Boolean -> jsonObject.put(key, this.getBoolean(key)) ReadableType.Number -> jsonObject.put(key, this.getDouble(key)) ReadableType.String -> jsonObject.put(key, this.getString(key)) ReadableType.Map -> jsonObject.put(key, this.getMap(key)?.toJSONObject()) ReadableType.Array -> jsonObject.put(key, this.getArray(key)?.toJSONArray()) } } return jsonObject } private fun ReadableArray.toJSONArray(): org.json.JSONArray { val jsonArray = org.json.JSONArray() for (i in 0 until this.size()) { when (this.getType(i)) { ReadableType.Null -> jsonArray.put(JSONObject.NULL) ReadableType.Boolean -> jsonArray.put(this.getBoolean(i)) ReadableType.Number -> jsonArray.put(this.getDouble(i)) ReadableType.String -> jsonArray.put(this.getString(i)) ReadableType.Map -> jsonArray.put(this.getMap(i)?.toJSONObject()) ReadableType.Array -> jsonArray.put(this.getArray(i)?.toJSONArray()) } } return jsonArray } @ReactMethod fun sendRequest(events: ReadableArray, promise: Promise) { Log.d("neo",events.toString()) Log.d("neo",readableArrayToList(events).toString()) BluxClient.sendRequestData(this.context,readableArrayToList(events)) promise.resolve(null) } companion object { const val NAME = "Blux" } }