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

View File

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