package com.dittolive.rnsdk import android.os.Build 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.turbomodule.core.interfaces.CallInvokerHolder import com.facebook.soloader.SoLoader class DittoRNSDKModule(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { override fun getName(): String { return NAME } private external fun nativeInstall( context: ReactApplicationContext, callInvoker: CallInvokerHolder, jsiPtr: Long, defaultDir: String ) @ReactMethod(isBlockingSynchronousMethod = true) fun install(): Boolean { return try { SoLoader.init(reactContext, false) SoLoader.loadLibrary("dittorn") val jsContextHolder = reactContext.javaScriptContextHolder // Although catalystInstance.jsCallInvokerHolder, it used to be the only // way to make it work with older RN project (0.73, I believe), so be sure // to check there if modifying. val jsCallInvokerHolder = reactContext.catalystInstance.jsCallInvokerHolder if (jsContextHolder != null && jsCallInvokerHolder != null) { nativeInstall( reactContext, jsCallInvokerHolder, jsContextHolder.get(), reactContext.filesDir.absolutePath ) true } else { Log.e("ReactNative", "JavaScript context or call invoker holder is null.") false } } catch (exception: Exception) { Log.e("ReactNative", "Failed to load library or initialize: ${exception.message}", exception) false } } // This is actually called by the JS SDK. @Suppress("unused") fun defaultDeviceName(): String { val manufacturer = Build.MANUFACTURER val model = Build.MODEL return if (model.startsWith(manufacturer)) model else "$manufacturer $model" } companion object { const val NAME = "DittoRNSDK" } }