refactor: Small improvements for changeState in RecorderService

This commit is contained in:
Myzel394 2023-12-01 00:18:04 +01:00
parent c38be920ec
commit 79b33ced2e
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B

View File

@ -91,6 +91,14 @@ abstract class RecorderService : LifecycleService() {
}
}
protected fun _changeStateValue(newState: RecorderState) {
state = newState
onStateChange?.invoke(newState)
}
// Used to change the state of the service
// will internally call start() / pause() / resume() / stop()
@SuppressLint("MissingPermission")
fun changeState(newState: RecorderState) {
if (state == newState) {
@ -106,27 +114,23 @@ abstract class RecorderService : LifecycleService() {
} else {
start()
}
createRecordingTimeTimer()
}
RecorderState.PAUSED -> {
pause()
isPaused = true
recordingTimeTimer.shutdown()
}
else -> {}
}
when (newState) {
RecorderState.RECORDING -> {
createRecordingTimeTimer()
}
RecorderState.PAUSED, RecorderState.IDLE -> {
RecorderState.IDLE -> {
recordingTimeTimer.shutdown()
}
}
// Update notification
if (
arrayOf(
RecorderState.RECORDING,
@ -140,7 +144,8 @@ abstract class RecorderService : LifecycleService() {
notification
)
}
onStateChange?.invoke(newState)
_changeStateValue(newState)
}
// Must be immediately called after creating the service!