fix: Replace deprecated RCTEventEmitter
with UIManager
(#2569)
use new events
This commit is contained in:
parent
aad7d2b8b3
commit
57c6431353
@ -4,57 +4,64 @@ import android.util.Log
|
|||||||
import com.facebook.react.bridge.Arguments
|
import com.facebook.react.bridge.Arguments
|
||||||
import com.facebook.react.bridge.ReactContext
|
import com.facebook.react.bridge.ReactContext
|
||||||
import com.facebook.react.bridge.WritableMap
|
import com.facebook.react.bridge.WritableMap
|
||||||
import com.facebook.react.uimanager.events.RCTEventEmitter
|
import com.facebook.react.uimanager.UIManagerHelper
|
||||||
|
import com.facebook.react.uimanager.events.Event
|
||||||
import com.google.mlkit.vision.barcode.common.Barcode
|
import com.google.mlkit.vision.barcode.common.Barcode
|
||||||
import com.mrousavy.camera.core.CameraError
|
import com.mrousavy.camera.core.CameraError
|
||||||
import com.mrousavy.camera.core.CodeScannerFrame
|
import com.mrousavy.camera.core.CodeScannerFrame
|
||||||
import com.mrousavy.camera.core.UnknownCameraError
|
import com.mrousavy.camera.core.UnknownCameraError
|
||||||
import com.mrousavy.camera.core.code
|
import com.mrousavy.camera.core.code
|
||||||
import com.mrousavy.camera.types.CodeType
|
import com.mrousavy.camera.types.*
|
||||||
|
|
||||||
fun CameraView.invokeOnInitialized() {
|
fun CameraView.invokeOnInitialized() {
|
||||||
Log.i(CameraView.TAG, "invokeOnInitialized()")
|
Log.i(CameraView.TAG, "invokeOnInitialized()")
|
||||||
|
|
||||||
val reactContext = context as ReactContext
|
val surfaceId = UIManagerHelper.getSurfaceId(this)
|
||||||
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "cameraInitialized", null)
|
val event = CameraInitializedEvent(surfaceId, id)
|
||||||
|
this.sendEvent(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun CameraView.invokeOnStarted() {
|
fun CameraView.invokeOnStarted() {
|
||||||
Log.i(CameraView.TAG, "invokeOnStarted()")
|
Log.i(CameraView.TAG, "invokeOnStarted()")
|
||||||
|
|
||||||
val reactContext = context as ReactContext
|
val surfaceId = UIManagerHelper.getSurfaceId(this)
|
||||||
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "cameraStarted", null)
|
val event = CameraStartedEvent(surfaceId, id)
|
||||||
|
this.sendEvent(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun CameraView.invokeOnStopped() {
|
fun CameraView.invokeOnStopped() {
|
||||||
Log.i(CameraView.TAG, "invokeOnStopped()")
|
Log.i(CameraView.TAG, "invokeOnStopped()")
|
||||||
|
|
||||||
val reactContext = context as ReactContext
|
val surfaceId = UIManagerHelper.getSurfaceId(this)
|
||||||
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "cameraStopped", null)
|
val event = CameraStoppedEvent(surfaceId, id)
|
||||||
|
this.sendEvent(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun CameraView.invokeOnError(error: Throwable) {
|
fun CameraView.invokeOnError(error: Throwable) {
|
||||||
Log.e(CameraView.TAG, "invokeOnError(...):")
|
Log.e(CameraView.TAG, "invokeOnError(...):")
|
||||||
error.printStackTrace()
|
error.printStackTrace()
|
||||||
|
|
||||||
val cameraError = when (error) {
|
val cameraError =
|
||||||
is CameraError -> error
|
when (error) {
|
||||||
else -> UnknownCameraError(error)
|
is CameraError -> error
|
||||||
}
|
else -> UnknownCameraError(error)
|
||||||
val event = Arguments.createMap()
|
}
|
||||||
event.putString("code", cameraError.code)
|
val data = Arguments.createMap()
|
||||||
event.putString("message", cameraError.message)
|
data.putString("code", cameraError.code)
|
||||||
|
data.putString("message", cameraError.message)
|
||||||
cameraError.cause?.let { cause ->
|
cameraError.cause?.let { cause ->
|
||||||
event.putMap("cause", errorToMap(cause))
|
data.putMap("cause", errorToMap(cause))
|
||||||
}
|
}
|
||||||
val reactContext = context as ReactContext
|
|
||||||
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "cameraError", event)
|
val surfaceId = UIManagerHelper.getSurfaceId(this)
|
||||||
|
val event = CameraErrorEvent(surfaceId, id, data)
|
||||||
|
this.sendEvent(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun CameraView.invokeOnViewReady() {
|
fun CameraView.invokeOnViewReady() {
|
||||||
val event = Arguments.createMap()
|
val surfaceId = UIManagerHelper.getSurfaceId(this)
|
||||||
val reactContext = context as ReactContext
|
val event = CameraViewReadyEvent(surfaceId, id)
|
||||||
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "cameraViewReady", event)
|
this.sendEvent(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun CameraView.invokeOnCodeScanned(barcodes: List<Barcode>, scannerFrame: CodeScannerFrame) {
|
fun CameraView.invokeOnCodeScanned(barcodes: List<Barcode>, scannerFrame: CodeScannerFrame) {
|
||||||
@ -87,14 +94,23 @@ fun CameraView.invokeOnCodeScanned(barcodes: List<Barcode>, scannerFrame: CodeSc
|
|||||||
codes.pushMap(code)
|
codes.pushMap(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
val event = Arguments.createMap()
|
val data = Arguments.createMap()
|
||||||
event.putArray("codes", codes)
|
data.putArray("codes", codes)
|
||||||
val codeScannerFrame = Arguments.createMap()
|
val codeScannerFrame = Arguments.createMap()
|
||||||
codeScannerFrame.putInt("width", scannerFrame.width)
|
codeScannerFrame.putInt("width", scannerFrame.width)
|
||||||
codeScannerFrame.putInt("height", scannerFrame.height)
|
codeScannerFrame.putInt("height", scannerFrame.height)
|
||||||
event.putMap("frame", codeScannerFrame)
|
data.putMap("frame", codeScannerFrame)
|
||||||
|
|
||||||
|
val surfaceId = UIManagerHelper.getSurfaceId(this)
|
||||||
|
val event = CameraCodeScannedEvent(surfaceId, id, data)
|
||||||
|
this.sendEvent(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun CameraView.sendEvent(event: Event<*>) {
|
||||||
val reactContext = context as ReactContext
|
val reactContext = context as ReactContext
|
||||||
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "cameraCodeScanned", event)
|
val dispatcher =
|
||||||
|
UIManagerHelper.getEventDispatcherForReactTag(reactContext, id)
|
||||||
|
dispatcher?.dispatchEvent(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun errorToMap(error: Throwable): WritableMap {
|
private fun errorToMap(error: Throwable): WritableMap {
|
||||||
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.mrousavy.camera.types
|
||||||
|
|
||||||
|
import com.facebook.react.bridge.Arguments
|
||||||
|
import com.facebook.react.bridge.WritableMap
|
||||||
|
import com.facebook.react.uimanager.events.Event
|
||||||
|
|
||||||
|
class CameraInitializedEvent(surfaceId: Int, viewId: Int) : Event<CameraInitializedEvent>(surfaceId, viewId) {
|
||||||
|
override fun getEventName() = "cameraInitialized"
|
||||||
|
override fun getEventData(): WritableMap = Arguments.createMap()
|
||||||
|
}
|
||||||
|
|
||||||
|
class CameraStartedEvent(surfaceId: Int, viewId: Int) : Event<CameraStartedEvent>(surfaceId, viewId) {
|
||||||
|
override fun getEventName() = "cameraStarted"
|
||||||
|
override fun getEventData(): WritableMap = Arguments.createMap()
|
||||||
|
}
|
||||||
|
|
||||||
|
class CameraStoppedEvent(surfaceId: Int, viewId: Int) : Event<CameraStoppedEvent>(surfaceId, viewId) {
|
||||||
|
override fun getEventName() = "cameraStopped"
|
||||||
|
override fun getEventData(): WritableMap = Arguments.createMap()
|
||||||
|
}
|
||||||
|
|
||||||
|
class CameraErrorEvent(surfaceId: Int, viewId: Int, private val data: WritableMap) : Event<CameraErrorEvent>(surfaceId, viewId) {
|
||||||
|
override fun getEventName() = "cameraError"
|
||||||
|
override fun getEventData() = data
|
||||||
|
}
|
||||||
|
|
||||||
|
class CameraViewReadyEvent(surfaceId: Int, viewId: Int) : Event<CameraViewReadyEvent>(surfaceId, viewId) {
|
||||||
|
override fun getEventName() = "cameraViewReady"
|
||||||
|
override fun getEventData(): WritableMap = Arguments.createMap()
|
||||||
|
}
|
||||||
|
|
||||||
|
class CameraCodeScannedEvent(surfaceId: Int, viewId: Int, private val data: WritableMap) :
|
||||||
|
Event<CameraCodeScannedEvent>(surfaceId, viewId) {
|
||||||
|
override fun getEventName() = "cameraCodeScanned"
|
||||||
|
override fun getEventData() = data
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user