CameraScreen: avoid losing state in case of configuration change

This commit is contained in:
Pierre-Yves Nicolas
2025-06-03 14:46:12 +02:00
parent c6d77d14f0
commit fd8c26da7f
2 changed files with 13 additions and 10 deletions

View File

@@ -38,6 +38,9 @@ class MainViewModel(private val imageSegmentationService: ImageSegmentationServi
private val _pages = MutableStateFlow<List<Bitmap>>(listOf()) private val _pages = MutableStateFlow<List<Bitmap>>(listOf())
val pages: StateFlow<List<Bitmap>> = _pages val pages: StateFlow<List<Bitmap>> = _pages
private var _pageToValidate = MutableStateFlow<Bitmap?>(null)
val pageToValidate: StateFlow<Bitmap?> = _pageToValidate.asStateFlow()
init { init {
viewModelScope.launch { viewModelScope.launch {
imageSegmentationService.initialize() imageSegmentationService.initialize()
@@ -73,8 +76,8 @@ class MainViewModel(private val imageSegmentationService: ImageSegmentationServi
fun processCapturedImageThen(imageProxy: ImageProxy, onResult: (Bitmap?) -> Unit) { fun processCapturedImageThen(imageProxy: ImageProxy, onResult: (Bitmap?) -> Unit) {
viewModelScope.launch { viewModelScope.launch {
val processedImage = processCapturedImage(imageProxy) _pageToValidate.value = processCapturedImage(imageProxy)
onResult(processedImage) onResult(_pageToValidate.value)
} }
} }

View File

@@ -37,6 +37,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Offset
@@ -53,6 +54,7 @@ import androidx.core.content.ContextCompat
import androidx.core.graphics.scale import androidx.core.graphics.scale
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.compose.LocalLifecycleOwner import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.google.common.util.concurrent.ListenableFuture import com.google.common.util.concurrent.ListenableFuture
import org.mydomain.myscan.LiveAnalysisState import org.mydomain.myscan.LiveAnalysisState
import org.mydomain.myscan.MainViewModel import org.mydomain.myscan.MainViewModel
@@ -70,11 +72,10 @@ fun CameraScreen(
onImageAnalyzed: (ImageProxy) -> Unit, onImageAnalyzed: (ImageProxy) -> Unit,
onFinalizePressed: () -> Unit onFinalizePressed: () -> Unit
) { ) {
// TODO Should we move those variables to ViewModel?
// TODO pause the live analysis when displaying the PageValidationDialogs // TODO pause the live analysis when displaying the PageValidationDialogs
val showPageDialog = remember { mutableStateOf(false) } val showPageDialog = rememberSaveable { mutableStateOf(false) }
val isProcessing = remember { mutableStateOf(false) } val isProcessing = rememberSaveable { mutableStateOf(false) }
val processedPage = remember { mutableStateOf<Bitmap?>(null) } val pageToValidate by viewModel.pageToValidate.collectAsStateWithLifecycle()
val context = LocalContext.current val context = LocalContext.current
val requestPermissionLauncher = rememberLauncherForActivityResult( val requestPermissionLauncher = rememberLauncherForActivityResult(
@@ -107,8 +108,7 @@ fun CameraScreen(
captureController.takePicture( captureController.takePicture(
onImageCaptured = { imageProxy -> onImageCaptured = { imageProxy ->
if (imageProxy != null) { if (imageProxy != null) {
viewModel.processCapturedImageThen(imageProxy) { result -> viewModel.processCapturedImageThen(imageProxy) {
processedPage.value = result
isProcessing.value = false isProcessing.value = false
} }
} else { } else {
@@ -129,9 +129,9 @@ fun CameraScreen(
if (showPageDialog.value) { if (showPageDialog.value) {
PageValidationDialog( PageValidationDialog(
isProcessing = isProcessing.value, isProcessing = isProcessing.value,
pageBitmap = processedPage.value, pageBitmap = pageToValidate,
onConfirm = { onConfirm = {
viewModel.addPage(processedPage.value!!) viewModel.addPage(pageToValidate!!)
showPageDialog.value = false showPageDialog.value = false
}, },
onReject = { onReject = {