package com.dittolive.rnsdk import android.os.Build import android.util.Log import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder import com.facebook.soloader.SoLoader // Import the generated spec base class import com.dittolive.rnsdk.NativeDittoRNSDKSpec class DittoRNSDKModule(reactContext: ReactApplicationContext) : NativeDittoRNSDKSpec(reactContext) { override fun getName(): String { return NAME } private external fun nativeInstall( context: ReactApplicationContext, callInvoker: CallInvokerHolder, jsiPtr: Long, defaultDir: String ) override fun install(): Boolean { return try { SoLoader.init(reactApplicationContext, false) SoLoader.loadLibrary("dittorn") val jsContextHolder = reactApplicationContext.javaScriptContextHolder val jsCallInvokerHolder = reactApplicationContext.catalystInstance.jsCallInvokerHolder if (jsContextHolder != null && jsCallInvokerHolder != null) { nativeInstall( reactApplicationContext, jsCallInvokerHolder, jsContextHolder.get(), reactApplicationContext.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 } } override fun getTypedExportedConstants(): Map { return mapOf( "DEFAULT_DIRECTORY" to reactApplicationContext.filesDir.absolutePath ) } // This is actually called by the C++ JSI code @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" } }