package com.appsonairreactnativeappremark import android.app.Activity import com.facebook.react.bridge.* import com.facebook.react.modules.core.DeviceEventManagerModule import com.appsonair.appremark.services.AppRemarkService import org.json.JSONObject class AppsonairReactNativeAppremarkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { private var options: Map = mutableMapOf( "pageBackgroundColor" to "#E8F1FF", "appbarBackgroundColor" to "#E8F1FF", "appbarTitleText" to "Add Remark", "appbarTitleColor" to "#000000", "remarkTypeLabelText" to "Remark Type", "descriptionLabelText" to "Description", "descriptionHintText" to "Add description here...", "descriptionMaxLength" to "255", "buttonText" to "Submit", "buttonTextColor" to "#FFFFFF", "buttonBackgroundColor" to "#007AFF", "labelColor" to "#000000", "hintColor" to "#B1B1B3", "inputTextColor" to "#000000" ) override fun getName(): String { return NAME } private fun ReadableMap.toMap(): Map { val map = mutableMapOf() val iterator = this.keySetIterator() while (iterator.hasNextKey()) { val key = iterator.nextKey() when (this.getType(key)) { ReadableType.String -> map[key] = this.getString(key) ?: "" ReadableType.Boolean -> map[key] = this.getBoolean(key) ReadableType.Number -> { val number = this.getDouble(key) map[key] = if (number % 1 == 0.0) number.toLong() else number } ReadableType.Map -> this.getMap(key)?.toMap()?.let { map[key] = it } else -> Unit } } return map } @ReactMethod fun initialize(shakeGestureEnable: Boolean, theme: ReadableMap) { options = theme.toMap() val activity: Activity? = reactApplicationContext.currentActivity activity?.let { AppRemarkService.initialize( activity, shakeGestureEnable, options ) { response: JSONObject -> val map = jsonToWritableMap(response) sendEvent("onRemarkResponse", map) Unit } } } @ReactMethod fun addRemark() { AppRemarkService.addRemark(reactApplicationContext) } @ReactMethod fun setAdditionalMetaData(metadata: ReadableMap) { val extraPayload = metadata.toMap() AppRemarkService.setAdditionalMetaData(extraPayload) } @ReactMethod fun addListener(eventName: String?) {} @ReactMethod fun removeListeners(count: Int) {} private fun sendEvent(eventName: String, params: WritableMap) { reactApplicationContext .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) .emit(eventName, params) } private fun jsonToWritableMap(json: JSONObject): WritableMap { val map = Arguments.createMap() val keys = json.keys() while (keys.hasNext()) { val key = keys.next() val value = json.opt(key) when (value) { is JSONObject -> map.putMap(key, jsonToWritableMap(value)) is org.json.JSONArray -> map.putArray(key, jsonToWritableArray(value)) is Boolean -> map.putBoolean(key, value) is Int -> map.putInt(key, value) is Double -> map.putDouble(key, value) is String -> map.putString(key, value) else -> map.putString(key, value?.toString()) } } return map } private fun jsonToWritableArray(array: org.json.JSONArray): WritableArray { val writableArray = Arguments.createArray() for (i in 0 until array.length()) { val value = array.opt(i) when (value) { is JSONObject -> writableArray.pushMap(jsonToWritableMap(value)) is org.json.JSONArray -> writableArray.pushArray(jsonToWritableArray(value)) is Boolean -> writableArray.pushBoolean(value) is Int -> writableArray.pushInt(value) is Double -> writableArray.pushDouble(value) is String -> writableArray.pushString(value) else -> writableArray.pushString(value?.toString()) } } return writableArray } companion object { const val NAME = "AppsonairReactNativeAppremark" } }