Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:

verify-ios:
name: Verify iOS
runs-on: macos-latest
runs-on: macos-14
timeout-minutes: 10
steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
targets: ["CameraViewPlugin"])
],
dependencies: [
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "7.2.0")
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "7.4.2")
],
targets: [
.target(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageCapture
import androidx.camera.core.ImageCaptureException
import androidx.camera.core.ImageProxy
import androidx.camera.core.TorchState
import androidx.camera.core.resolutionselector.AspectRatioStrategy
import androidx.camera.core.resolutionselector.ResolutionSelector
import androidx.camera.mlkit.vision.MlKitAnalyzer
Expand Down Expand Up @@ -55,7 +56,6 @@ class CameraView(plugin: Plugin) {
// Camera state
private var currentCameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
private var currentFlashMode: Int = ImageCapture.FLASH_MODE_OFF
private var isTorchEnabled: Boolean = false

// Plugin context
private var lifecycleOwner: LifecycleOwner? = null
Expand Down Expand Up @@ -416,8 +416,16 @@ class CameraView(plugin: Plugin) {
}

/** Get the current torch mode */
fun getTorchMode(): Boolean {
return isTorchEnabled
fun getTorchMode(callback: (Boolean) -> Unit) {
mainHandler.post {
val cameraInfo = cameraController?.cameraInfo
?: run {
callback(false)
return@post
}

callback(cameraInfo.torchState.value == TorchState.ON)
}
}

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

controller.cameraControl?.enableTorch(enabled)
isTorchEnabled = enabled
callback?.invoke(null)
} catch (e: Exception) {
callback?.invoke(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ class CameraViewPlugin : Plugin() {

@PluginMethod
fun getTorchMode(call: PluginCall) {
try {
val enabled = implementation.getTorchMode()
implementation.getTorchMode { enabled ->
call.resolve(JSObject().apply {
put("enabled", enabled)
put(
Expand All @@ -230,8 +229,6 @@ class CameraViewPlugin : Plugin() {
if (enabled) 1.0f else 0.0f
)
})
} catch (e: Exception) {
call.reject("Failed to get torch mode: ${e.localizedMessage}", e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export class CameraModalComponent implements OnInit, OnDestroy {
]);

this.currentZoomFactor.set(this.initialZoomFactor());
await this.#debugCurrentTorchState();
}

protected async stopCamera(): Promise<void> {
Expand All @@ -185,6 +186,8 @@ export class CameraModalComponent implements OnInit, OnDestroy {
} catch (error) {
console.error('Failed to stop camera', error);
}

await this.#debugCurrentTorchState();
}

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

protected async flipCamera(): Promise<void> {
debugger;
try {
await this.#cameraViewService.flipCamera();

Expand Down Expand Up @@ -408,6 +410,8 @@ export class CameraModalComponent implements OnInit, OnDestroy {
} catch (error) {
console.error('Failed to toggle torch', error);
}

await this.#debugCurrentTorchState();
}

async #toggleTorchIos(): Promise<void> {
Expand Down Expand Up @@ -447,4 +451,12 @@ export class CameraModalComponent implements OnInit, OnDestroy {
// Android: 100% when on, 0% when off
this.torchLevel.set(enabled ? 1.0 : 0.0);
}

/**
* Helper method for checking current torch state
*/
async #debugCurrentTorchState(): Promise<void> {
const currentState = await this.#cameraViewService.getTorchMode();
console.debug('Current torch state:', currentState);
}
}
14 changes: 6 additions & 8 deletions ios/Sources/CameraViewPlugin/CameraViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
/// Currently selected flash mode.
private var flashMode: AVCaptureDevice.FlashMode = .auto

/// Currently selected torch mode and level.
private var torchEnabled: Bool = false
private var torchLevel: Float = 1.0

/// Reference to the blur overlay view that is shown when switching to the triple camera in order to have a smooth transition
private var blurOverlayView: UIVisualEffectView?

Expand Down Expand Up @@ -248,7 +244,12 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
///
/// - Returns: A tuple containing the torch enabled state and level
public func getTorchMode() -> (enabled: Bool, level: Float) {
return (torchEnabled, torchLevel)
guard let camera = currentCameraDevice else { return (false, 0.0) }

let isEnabled = camera.torchMode == .on
let level = camera.torchLevel

return (isEnabled, level)
}

/// Sets the torch mode and level for the currently active camera device.
Expand All @@ -270,9 +271,6 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
} else {
camera.torchMode = .off
}

torchEnabled = enabled && level > 0.0
torchLevel = level
} catch {
throw CameraError.configurationFailed(error)
}
Expand Down