package io.scanbot.sdk.reactnative.extensions import android.content.res.Resources import android.view.View import android.view.View.MeasureSpec import com.facebook.react.bridge.ReactContext import com.facebook.react.uimanager.UIManagerHelper import io.scanbot.sdk.reactnative.components.barcode_scanner_view.BarcodeErrorEvent import io.scanbot.sdk.reactnative.components.barcode_scanner_view.ScanbotBarcodeScannerView import io.scanbot.sdk.reactnative.components.document_scanner_view.DocumentScannerErrorEvent import io.scanbot.sdk.reactnative.components.document_scanner_view.ScanbotDocumentScannerView import io.scanbot.sdk.reactnative.models.BaseEvent import io.scanbot.sdk.ui_v2.common.ScanbotColor internal fun Int.toPx(resources: Resources): Int { return (this * resources.displayMetrics.density).toInt() } internal fun Double.toPx(resources: Resources): Int { return (this * resources.displayMetrics.density).toInt() } object Utils { fun tryToParseRGBAHexColor(colorString: String): Int? { return try { ScanbotColor(colorString, false).toAndroid() } catch (ex: Exception) { null } } fun cameraModuleFromJson(value: String): io.scanbot.sdk.camera.CameraModule { return when (value) { "FRONT" -> io.scanbot.sdk.camera.CameraModule.FRONT else -> io.scanbot.sdk.camera.CameraModule.BACK } } } internal fun View.layoutView() { measure( MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(measuredHeight, MeasureSpec.EXACTLY) ) layout(left, top, right, bottom) } internal fun View.sendEvent(event: BaseEvent) { val surfaceId = UIManagerHelper.getSurfaceId(this) event.send(surfaceId, (context as ReactContext), id) } fun io.scanbot.common.Result.getOrSendBarcodeErrorEvent(view: ScanbotBarcodeScannerView): T? { return try { this.getOrThrow() } catch (ex: Throwable) { view.sendEvent(BarcodeErrorEvent(ex)) null } } fun io.scanbot.common.Result.getOrSendDocumentErrorEvent(view: ScanbotDocumentScannerView): T? { return try { this.getOrThrow() } catch (ex: Throwable) { view.sendEvent(DocumentScannerErrorEvent(ex)) null } }