feat: Add onStarted and onStopped events (#2273)

* feat: Add `onStarted` and `onStopped` events

* Implement `onStart` for Android

* Update CameraSession.kt

* Update CameraSessionDelegate.swift
This commit is contained in:
Marc Rousavy
2023-12-09 21:09:55 +03:00
committed by GitHub
parent 9ef4a9a210
commit 4ee52d6211
12 changed files with 105 additions and 11 deletions

View File

@@ -19,6 +19,20 @@ fun CameraView.invokeOnInitialized() {
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "cameraInitialized", null)
}
fun CameraView.invokeOnStarted() {
Log.i(CameraView.TAG, "invokeOnStarted()")
val reactContext = context as ReactContext
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "cameraStarted", null)
}
fun CameraView.invokeOnStopped() {
Log.i(CameraView.TAG, "invokeOnStopped()")
val reactContext = context as ReactContext
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "cameraStopped", null)
}
fun CameraView.invokeOnError(error: Throwable) {
Log.e(CameraView.TAG, "invokeOnError(...):")
error.printStackTrace()

View File

@@ -239,6 +239,14 @@ class CameraView(context: Context) :
invokeOnInitialized()
}
override fun onStarted() {
invokeOnStarted()
}
override fun onStopped() {
invokeOnStopped()
}
override fun onCodeScanned(codes: List<Barcode>, scannerFrame: CodeScannerFrame) {
invokeOnCodeScanned(codes, scannerFrame)
}

View File

@@ -25,6 +25,8 @@ class CameraViewManager : ViewGroupManager<CameraView>() {
MapBuilder.builder<String, Any>()
.put("cameraViewReady", MapBuilder.of("registrationName", "onViewReady"))
.put("cameraInitialized", MapBuilder.of("registrationName", "onInitialized"))
.put("cameraStarted", MapBuilder.of("registrationName", "onStarted"))
.put("cameraStopped", MapBuilder.of("registrationName", "onStopped"))
.put("cameraError", MapBuilder.of("registrationName", "onError"))
.put("cameraCodeScanned", MapBuilder.of("registrationName", "onCodeScanned"))
.build()

View File

@@ -76,7 +76,9 @@ data class CameraConfiguration(
// Outputs & Session (Photo, Video, CodeScanner, HDR, Format)
val outputsChanged: Boolean,
// Side-Props for CaptureRequest (fps, low-light-boost, torch, zoom, videoStabilization)
val sidePropsChanged: Boolean
val sidePropsChanged: Boolean,
// (isActive) changed
val isActiveChanged: Boolean
) {
val hasAnyDifference: Boolean
get() = sidePropsChanged || outputsChanged || deviceChanged
@@ -98,10 +100,13 @@ data class CameraConfiguration(
left.zoom != right.zoom || left.videoStabilizationMode != right.videoStabilizationMode || left.isActive != right.isActive ||
left.exposure != right.exposure
val isActiveChanged = left?.isActive != right.isActive
return Difference(
deviceChanged,
outputsChanged,
sidePropsChanged
sidePropsChanged,
isActiveChanged
)
}
}

View File

@@ -146,6 +146,16 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
if (diff.deviceChanged) {
callback.onInitialized()
}
// Notify about Camera start/stop
if (diff.isActiveChanged) {
// TODO: Move that into the CaptureRequest callback to get actual first-frame arrive time?
if (config.isActive) {
callback.onStarted()
} else {
callback.onStopped()
}
}
} catch (error: Throwable) {
Log.e(TAG, "Failed to configure CameraSession! Error: ${error.message}, Config-Diff: $diff", error)
callback.onError(error)
@@ -367,6 +377,7 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
// TODO: Do we want to do stopRepeating() or entirely destroy the session?
// If the Camera is not active, we don't do anything.
captureSession?.stopRepeating()
isRunning = false
return
}
@@ -621,6 +632,8 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
interface CameraSessionCallback {
fun onError(error: Throwable)
fun onInitialized()
fun onStarted()
fun onStopped()
fun onCodeScanned(codes: List<Barcode>, scannerFrame: CodeScannerFrame)
}
}