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

View File

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

View File

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