Persist torch state while the app is open (#95)

This commit is contained in:
Pierre-Yves Nicolas
2026-01-25 17:43:12 +01:00
parent 185cbc0ebc
commit 2308f21171
2 changed files with 13 additions and 3 deletions

View File

@@ -122,7 +122,7 @@ fun CameraScreen(
val document by viewModel.documentUiModel.collectAsStateWithLifecycle()
val thumbnailCoords = remember { mutableStateOf(Offset.Zero) }
var isDebugMode by remember { mutableStateOf(false) }
var isTorchEnabled by remember { mutableStateOf(false) }
val isTorchEnabled by cameraViewModel.isTorchEnabled.collectAsStateWithLifecycle()
BackHandler { navigation.back() }
@@ -130,6 +130,9 @@ fun CameraScreen(
DisposableEffect(Unit) {
onDispose { captureController.shutdown() }
}
LaunchedEffect(captureController.cameraControl, isTorchEnabled) {
captureController.cameraControl?.enableTorch(isTorchEnabled)
}
val captureState by cameraViewModel.captureState.collectAsStateWithLifecycle()
if (captureState is CaptureState.CapturePreview) {
@@ -197,8 +200,8 @@ fun CameraScreen(
onFinalizePressed = onFinalizePressed,
onDebugModeSwitched = { isDebugMode = !isDebugMode },
onTorchSwitched = {
isTorchEnabled = !isTorchEnabled
captureController.cameraControl?.enableTorch(isTorchEnabled) },
cameraViewModel.setTorchEnabled(!isTorchEnabled)
},
thumbnailCoords = thumbnailCoords,
navigation = navigation,
captureController

View File

@@ -63,6 +63,9 @@ class CameraViewModel(appContainer: AppContainer): ViewModel() {
private val _captureState = MutableStateFlow<CaptureState>(CaptureState.Idle)
val captureState: StateFlow<CaptureState> = _captureState
private val _isTorchEnabled = MutableStateFlow(false)
val isTorchEnabled: StateFlow<Boolean> = _isTorchEnabled
init {
viewModelScope.launch {
imageSegmentationService.initialize()
@@ -170,6 +173,10 @@ class CameraViewModel(appContainer: AppContainer): ViewModel() {
logger.e("Camera", message, throwable)
}
}
fun setTorchEnabled(enabled: Boolean) {
_isTorchEnabled.value = enabled
}
}
sealed class CaptureState {