package com.dynamsoft.reactnativelib.dce import android.util.Log import com.dynamsoft.core.basic_structures.ImageData import com.dynamsoft.dce.DrawingItem import com.dynamsoft.dce.ImageEditorView import com.dynamsoft.dce.QuadDrawingItem import com.dynamsoft.reactnativelib.basicutils.toQuad import com.dynamsoft.reactnativelib.basicutils.toWritableMap import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactMethod import com.facebook.react.bridge.ReadableArray import com.facebook.react.uimanager.UIManagerHelper import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import kotlinx.coroutines.withTimeoutOrNull class ImageEditorViewModuleImpl(private val reactContext: ReactApplicationContext) { companion object { const val NAME = "DynamsoftImageEditorView" const val TAG = "ImageEditorModuleImpl" } fun getName() = NAME @ReactMethod fun setQuads(viewTag: Int, readableArray: ReadableArray, layerId: Int, promise: Promise) { CoroutineScope(Dispatchers.Default).launch(CoroutineName("setQuads")) { val imageEditorView = findImageEditorView(viewTag) if(imageEditorView == null) { Log.e(TAG, "setQuads: Can't not find imageEditorView!(viewId = $viewTag)") promise.resolve(null) return@launch } val drawingItems = ArrayList>() for (i in 0 until readableArray.size()) { readableArray.getMap(i)?.toQuad()?.apply { drawingItems.add(QuadDrawingItem(this)) } } imageEditorView.getDrawingLayer(layerId).drawingItems = drawingItems promise.resolve(null) } } @ReactMethod fun getSelectedQuad(viewTag: Int, promise: Promise) { CoroutineScope(Dispatchers.Default).launch(CoroutineName("getSelectedQuad")) { val imageEditorView = findImageEditorView(viewTag) if(imageEditorView == null) { Log.e(TAG, "getSelectedQuad: Can't not find imageEditorView!(viewId = $viewTag)") promise.resolve(null) return@launch } val selectedDrawingItem = imageEditorView.selectedDrawingItem if (selectedDrawingItem != null) { promise.resolve((selectedDrawingItem as QuadDrawingItem).quad.toWritableMap()) } else { promise.resolve(null) } } } @ReactMethod(isBlockingSynchronousMethod = true) fun install(): Boolean { reactContext.javaScriptContextHolder?.apply { nativeInstall(get()) } return true } private suspend fun findImageEditorView(viewId: Int): ImageEditorView? = withTimeoutOrNull(300) { while (true) { val view = withContext(Dispatchers.Main) { try { UIManagerHelper.getUIManager(reactContext, viewId)?.resolveView(viewId) as? ImageEditorView } catch (e: Exception) { null } } if (view != null) { return@withTimeoutOrNull view } if (!isActive) { break } } return@withTimeoutOrNull null } //@Native private fun setOriginalImageCalledInJSI(imageData: ImageData, editorViewTag: Int) { CoroutineScope(Dispatchers.Default).launch(CoroutineName("setOriginalImageCalledInJSI")) { val imageEditorView = findImageEditorView(editorViewTag) if(imageEditorView == null) { Log.e(TAG, "setOriginalImageCalledInJSI: Can't not find imageEditorView!(viewId = $editorViewTag)") return@launch } imageEditorView?.originalImage = imageData } } private external fun nativeInstall(jsiPtr: Long) }