Skip to content

Commit 840d6b5

Browse files
committed
fix(*): reflect correct torch state
Instead of internally handling the torch state now using the actual state of the camera.
1 parent ce377e6 commit 840d6b5

File tree

4 files changed

+140
-126
lines changed

4 files changed

+140
-126
lines changed

android/src/main/java/com/michaelwolz/capacitorcameraview/CameraView.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import androidx.camera.core.ImageAnalysis
1919
import androidx.camera.core.ImageCapture
2020
import androidx.camera.core.ImageCaptureException
2121
import androidx.camera.core.ImageProxy
22+
import androidx.camera.core.TorchState
2223
import androidx.camera.core.resolutionselector.AspectRatioStrategy
2324
import androidx.camera.core.resolutionselector.ResolutionSelector
2425
import androidx.camera.mlkit.vision.MlKitAnalyzer
@@ -55,7 +56,6 @@ class CameraView(plugin: Plugin) {
5556
// Camera state
5657
private var currentCameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
5758
private var currentFlashMode: Int = ImageCapture.FLASH_MODE_OFF
58-
private var isTorchEnabled: Boolean = false
5959

6060
// Plugin context
6161
private var lifecycleOwner: LifecycleOwner? = null
@@ -416,8 +416,16 @@ class CameraView(plugin: Plugin) {
416416
}
417417

418418
/** Get the current torch mode */
419-
fun getTorchMode(): Boolean {
420-
return isTorchEnabled
419+
fun getTorchMode(callback: (Boolean) -> Unit) {
420+
mainHandler.post {
421+
val cameraInfo = cameraController?.cameraInfo
422+
?: run {
423+
callback(false)
424+
return@post
425+
}
426+
427+
callback(cameraInfo.torchState.value == TorchState.ON)
428+
}
421429
}
422430

423431
/** Set the torch mode */
@@ -437,7 +445,6 @@ class CameraView(plugin: Plugin) {
437445
}
438446

439447
controller.cameraControl?.enableTorch(enabled)
440-
isTorchEnabled = enabled
441448
callback?.invoke(null)
442449
} catch (e: Exception) {
443450
callback?.invoke(e)

android/src/main/java/com/michaelwolz/capacitorcameraview/CameraViewPlugin.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ class CameraViewPlugin : Plugin() {
220220

221221
@PluginMethod
222222
fun getTorchMode(call: PluginCall) {
223-
try {
224-
val enabled = implementation.getTorchMode()
223+
implementation.getTorchMode { enabled ->
225224
call.resolve(JSObject().apply {
226225
put("enabled", enabled)
227226
put(
@@ -230,8 +229,6 @@ class CameraViewPlugin : Plugin() {
230229
if (enabled) 1.0f else 0.0f
231230
)
232231
})
233-
} catch (e: Exception) {
234-
call.reject("Failed to get torch mode: ${e.localizedMessage}", e)
235232
}
236233
}
237234

example-app/src/app/components/camera-modal/camera-modal.component.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export class CameraModalComponent implements OnInit, OnDestroy {
177177
]);
178178

179179
this.currentZoomFactor.set(this.initialZoomFactor());
180+
await this.#debugCurrentTorchState();
180181
}
181182

182183
protected async stopCamera(): Promise<void> {
@@ -185,6 +186,8 @@ export class CameraModalComponent implements OnInit, OnDestroy {
185186
} catch (error) {
186187
console.error('Failed to stop camera', error);
187188
}
189+
190+
await this.#debugCurrentTorchState();
188191
}
189192

190193
protected async close(): Promise<void> {
@@ -267,7 +270,6 @@ export class CameraModalComponent implements OnInit, OnDestroy {
267270
}
268271

269272
protected async flipCamera(): Promise<void> {
270-
debugger;
271273
try {
272274
await this.#cameraViewService.flipCamera();
273275

@@ -408,6 +410,8 @@ export class CameraModalComponent implements OnInit, OnDestroy {
408410
} catch (error) {
409411
console.error('Failed to toggle torch', error);
410412
}
413+
414+
await this.#debugCurrentTorchState();
411415
}
412416

413417
async #toggleTorchIos(): Promise<void> {
@@ -447,4 +451,12 @@ export class CameraModalComponent implements OnInit, OnDestroy {
447451
// Android: 100% when on, 0% when off
448452
this.torchLevel.set(enabled ? 1.0 : 0.0);
449453
}
454+
455+
/**
456+
* Helper method for checking current torch state
457+
*/
458+
async #debugCurrentTorchState(): Promise<void> {
459+
const currentState = await this.#cameraViewService.getTorchMode();
460+
console.debug('Current torch state:', currentState);
461+
}
450462
}

0 commit comments

Comments
 (0)