Skip to content

Commit b6f7306

Browse files
committed
Better camera job management
1 parent 7cec113 commit b6f7306

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

app/src/main/kotlin/com/darkrockstudios/app/securecamera/camera/CameraControls.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ fun CameraControls(
4444
val scope = rememberCoroutineScope()
4545
var isFlashOn by rememberSaveable(cameraController.flashMode) { mutableStateOf(cameraController.flashMode == ImageCapture.FLASH_MODE_ON) }
4646
var isTopControlsVisible by rememberSaveable { mutableStateOf(false) }
47-
var activeJobs by remember { mutableStateOf(0) }
48-
val isLoading by remember { derivedStateOf { activeJobs > 0 } }
47+
var activeJobs by remember { mutableStateOf(mutableListOf<kotlinx.coroutines.Job>()) }
48+
val isLoading by remember { derivedStateOf { activeJobs.isNotEmpty() } }
4949
var isFlashing by rememberSaveable { mutableStateOf(false) }
5050
val imageSaver = koinInject<SecureImageManager>()
5151
val authManager = koinInject<AuthorizationManager>()
@@ -61,8 +61,7 @@ fun CameraControls(
6161
if (authManager.checkSessionValidity()) {
6262
isFlashing = true
6363

64-
activeJobs++
65-
scope.launch(Dispatchers.IO) {
64+
val job = scope.launch(Dispatchers.IO) {
6665
val location = if (locationPermissionState) {
6766
locationRepository.currentLocation()
6867
} else {
@@ -78,9 +77,14 @@ fun CameraControls(
7877
context = context,
7978
)
8079
} finally {
81-
activeJobs--
80+
activeJobs =
81+
activeJobs
82+
.filterNot { it === this }
83+
.filter { it.isCompleted.not() && it.isCancelled.not() }
84+
.toMutableList()
8285
}
8386
}
87+
activeJobs = (activeJobs + job).toMutableList()
8488
} else {
8589
navController.navigate(AppDestinations.createPinVerificationRoute(AppDestinations.CAMERA_ROUTE))
8690
}
@@ -132,7 +136,7 @@ fun CameraControls(
132136
}
133137
}
134138

135-
if (activeJobs > 0) {
139+
if (activeJobs.isNotEmpty()) {
136140
CircularProgressIndicator(
137141
modifier = Modifier
138142
.padding(start = 16.dp, top = paddingValues?.calculateTopPadding()?.plus(16.dp) ?: 16.dp)
@@ -208,4 +212,3 @@ private fun FlashEffect(isFlashing: Boolean) {
208212
) {}
209213
}
210214
}
211-

0 commit comments

Comments
 (0)