package com.dynamsoft.reactnativelib.core import com.dynamsoft.core.basic_structures.EnumBufferOverflowProtectionMode import com.dynamsoft.core.basic_structures.EnumColourChannelUsageType import com.dynamsoft.core.basic_structures.ImageSourceAdapter import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactMethod import java.lang.annotation.Native class ImageSourceAdapterModuleImpl(private val reactContext: ReactApplicationContext) { companion object { @JvmStatic @Native val mapISA = mutableMapOf() @JvmStatic fun getISA(name: String): ImageSourceAdapter? { return mapISA[name] } @JvmStatic external fun nativeInstall(jsiPtr: Long) const val NAME = "DynamsoftImageSourceAdapterModule" } fun getName() = NAME @ReactMethod(isBlockingSynchronousMethod = true) fun createDefaultNativeISA(): String { val defaultISA = DefaultISA() mapISA[defaultISA.toString()] = defaultISA return defaultISA.toString() } @ReactMethod fun destroy(isaId: String) { mapISA.remove(isaId)?.clearBuffer() } @ReactMethod fun clearBuffer(isaId: String, promise: Promise) { mapISA[isaId]?.clearBuffer() promise.resolve(true) } @ReactMethod fun getImageCount(isaId: String, promise: Promise) { val imageCount = mapISA[isaId]?.imageCount ?: -1 promise.resolve(imageCount) } @ReactMethod fun hasImage(isaId: String, imageId: Int, promise: Promise) { promise.resolve(mapISA[isaId]?.hasImage(imageId) ?: false) } @ReactMethod fun isBufferEmpty(isaId: String, promise: Promise) { promise.resolve(mapISA[isaId]?.isBufferEmpty ?: false) } @ReactMethod fun getBufferOverflowProtectionMode(isaId: String, promise: Promise) { promise.resolve(mapISA[isaId]?.bufferOverflowProtectionMode ?: 0) } @ReactMethod fun setBufferOverflowProtectionMode(isaId: String, @EnumBufferOverflowProtectionMode mode: Int) { mapISA[isaId]?.bufferOverflowProtectionMode = mode } @ReactMethod fun getColourChannelUsageType(isaId: String, promise: Promise) { promise.resolve(mapISA[isaId]?.colourChannelUsageType ?: 0) } @ReactMethod fun setColourChannelUsageType(isaId: String, @EnumColourChannelUsageType type: Int, promise: Promise) { mapISA[isaId]?.colourChannelUsageType = type promise.resolve(true) } @ReactMethod fun getMaximumImageCount(isaId: String, promise: Promise) { promise.resolve(mapISA[isaId]?.maximumImageCount ?: 0) } @ReactMethod fun setMaximumImageCount(isaId: String, maxCount: Int, promise: Promise) { mapISA[isaId]?.maximumImageCount = maxCount promise.resolve(true) } @ReactMethod fun setNextImageToReturn(isaId: String, nextId: Int, promise: Promise) { mapISA[isaId]?.setNextImageToReturn(nextId) promise.resolve(true) } @ReactMethod fun startFetching(isaId: String) { mapISA[isaId]?.startFetching() } @ReactMethod fun stopFetching(isaId: String) { mapISA[isaId]?.stopFetching() } class DefaultISA : ImageSourceAdapter() { override fun hasNextImageToFetch(): Boolean { return imageCount != 0 } } @ReactMethod(isBlockingSynchronousMethod = true) fun install(): Boolean { val jsiPtr = reactContext.javaScriptContextHolder?.get() ?: 0 nativeInstall(jsiPtr) return true } }