fix: Set state after action to avoid coroutine going out of scope

This commit is contained in:
Myzel394 2023-12-13 15:20:53 +01:00
parent d79aabab50
commit df78952cfb
No known key found for this signature in database
GPG Key ID: 50098FCA22080F0F
3 changed files with 11 additions and 5 deletions

View File

@ -32,6 +32,9 @@ abstract class RecorderService : LifecycleService() {
var state = RecorderState.IDLE
private set
protected var _newState = RecorderState.IDLE
private set
var onStateChange: ((RecorderState) -> Unit)? = null
var onError: () -> Unit = {}
@ -168,8 +171,9 @@ abstract class RecorderService : LifecycleService() {
}
suspend fun stopRecording() {
changeState(RecorderState.IDLE)
_newState = RecorderState.IDLE
stop()
changeState(RecorderState.IDLE)
}
override fun onDestroy() {

View File

@ -105,11 +105,11 @@ class VideoRecorderService :
super.startNewCycle()
fun action() {
activeRecording?.stop()
stopActiveRecording()
val newRecording = prepareVideoRecording()
activeRecording = newRecording.start(ContextCompat.getMainExecutor(this)) { event ->
if (event is VideoRecordEvent.Finalize && this@VideoRecorderService.state == RecorderState.IDLE) {
if (event is VideoRecordEvent.Finalize && this@VideoRecorderService._newState == RecorderState.IDLE) {
_videoFinalizerListener.complete(Unit)
}
}
@ -197,7 +197,9 @@ class VideoRecorderService :
// `resume` override not needed as `startNewCycle` is called by `IntervalRecorderService`
private fun stopActiveRecording() {
activeRecording?.stop()
runCatching {
activeRecording?.stop()
}
}
@SuppressLint("MissingPermission")

View File

@ -136,7 +136,7 @@ abstract class BaseRecorderModel<S : IntervalRecorderService.Settings, I, T : In
val intent = Intent(context, intentClass)
context.unbindService(connection)
unbindFromService(context)
context.stopService(intent)
}