CameraScreen: cancel import when leaving the screen

This commit is contained in:
Pierre-Yves Nicolas
2026-04-10 07:02:01 +02:00
parent 6290717635
commit 31155275e8
2 changed files with 20 additions and 1 deletions

View File

@@ -176,6 +176,12 @@ fun CameraScreen(
cameraViewModel.resetLiveAnalysis() cameraViewModel.resetLiveAnalysis()
} }
DisposableEffect(Unit) {
onDispose {
cameraViewModel.cancelImport()
}
}
val listState = rememberLazyListState() val listState = rememberLazyListState()
LaunchedEffect(document.pageCount()) { LaunchedEffect(document.pageCount()) {
if (!document.isEmpty()) { if (!document.isEmpty()) {

View File

@@ -21,6 +21,8 @@ import androidx.camera.core.ImageProxy
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
@@ -58,6 +60,7 @@ class CameraViewModel(appContainer: AppContainer): ViewModel() {
private val _importState = MutableStateFlow<ImportState>(ImportState.Idle) private val _importState = MutableStateFlow<ImportState>(ImportState.Idle)
val importState: StateFlow<ImportState> = _importState val importState: StateFlow<ImportState> = _importState
private var importJob: Job? = null
private val _isTorchEnabled = MutableStateFlow(false) private val _isTorchEnabled = MutableStateFlow(false)
val isTorchEnabled: StateFlow<Boolean> = _isTorchEnabled val isTorchEnabled: StateFlow<Boolean> = _isTorchEnabled
@@ -191,15 +194,19 @@ class CameraViewModel(appContainer: AppContainer): ViewModel() {
} }
fun importPhotos(uris: List<Uri>) { fun importPhotos(uris: List<Uri>) {
importJob?.cancel()
if (uris.isEmpty()) { if (uris.isEmpty()) {
_importState.value = ImportState.Idle _importState.value = ImportState.Idle
return return
} }
viewModelScope.launch { importJob = viewModelScope.launch {
_importState.value = ImportState.Importing(0, uris.size) _importState.value = ImportState.Importing(0, uris.size)
uris.forEachIndexed { index, uri -> uris.forEachIndexed { index, uri ->
ensureActive()
val photoToImport = imageLoader.load(uri) val photoToImport = imageLoader.load(uri)
ensureActive()
val page = processCapturedImage(photoToImport, 0) val page = processCapturedImage(photoToImport, 0)
ensureActive()
page?.let { page?.let {
_events.emit(CameraEvent.ImageCaptured(it)) _events.emit(CameraEvent.ImageCaptured(it))
} }
@@ -213,6 +220,12 @@ class CameraViewModel(appContainer: AppContainer): ViewModel() {
_importState.value = ImportState.Selecting _importState.value = ImportState.Selecting
resetLiveAnalysis() resetLiveAnalysis()
} }
fun cancelImport() {
importJob?.cancel()
importJob = null
_importState.value = ImportState.Idle
}
} }
sealed class CaptureState { sealed class CaptureState {