fix: Differentiate between stopping and destroying -> destroy service after stoppind and saving last recording information

This commit is contained in:
Myzel394 2023-12-15 19:54:42 +01:00
parent 4be2fc52e2
commit fa5cd6fbca
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
5 changed files with 52 additions and 33 deletions

View File

@ -55,11 +55,6 @@ abstract class RecorderService : LifecycleService() {
protected open suspend fun stop() { protected open suspend fun stop() {
recordingTimeTimer.shutdown() recordingTimeTimer.shutdown()
NotificationManagerCompat.from(this)
.cancel(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID)
stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf()
} }
protected abstract fun startForegroundService() protected abstract fun startForegroundService()
@ -85,6 +80,13 @@ abstract class RecorderService : LifecycleService() {
changeState(RecorderState.RECORDING) changeState(RecorderState.RECORDING)
} }
fun destroy() {
NotificationManagerCompat.from(this)
.cancel(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID)
stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf()
}
override fun onBind(intent: Intent): IBinder? { override fun onBind(intent: Intent): IBinder? {
super.onBind(intent) super.onBind(intent)
return binder return binder

View File

@ -82,16 +82,14 @@ fun RecorderEventsHandler(
} }
} }
fun saveAsLastRecording( suspend fun saveAsLastRecording(
recorder: RecorderModel recorder: RecorderModel
) { ) {
if (!settings.deleteRecordingsImmediately) { if (!settings.deleteRecordingsImmediately) {
scope.launch { dataStore.updateData {
dataStore.updateData { it.setLastRecording(
it.setLastRecording( recorder.recorderService!!.getRecordingInformation()
recorder.recorderService!!.getRecordingInformation() )
)
}
} }
} }
} }
@ -189,14 +187,24 @@ fun RecorderEventsHandler(
// Register audio recorder events // Register audio recorder events
DisposableEffect(key1 = audioRecorder, key2 = settings) { DisposableEffect(key1 = audioRecorder, key2 = settings) {
audioRecorder.onRecordingSave = { audioRecorder.onRecordingSave = {
saveAsLastRecording(audioRecorder as RecorderModel) scope.launch {
audioRecorder.stopRecording(context)
saveRecording(audioRecorder) kotlin.runCatching {
saveAsLastRecording(audioRecorder as RecorderModel)
saveRecording(audioRecorder)
}
audioRecorder.destroyService(context)
}
} }
audioRecorder.onError = { audioRecorder.onError = {
saveAsLastRecording(audioRecorder as RecorderModel) scope.launch {
saveAsLastRecording(audioRecorder as RecorderModel)
showRecorderError = true showRecorderError = true
}
} }
onDispose { onDispose {
@ -208,14 +216,24 @@ fun RecorderEventsHandler(
// Register video recorder events // Register video recorder events
DisposableEffect(key1 = videoRecorder, key2 = settings) { DisposableEffect(key1 = videoRecorder, key2 = settings) {
videoRecorder.onRecordingSave = { videoRecorder.onRecordingSave = {
saveAsLastRecording(videoRecorder as RecorderModel) scope.launch {
videoRecorder.stopRecording(context)
saveRecording(videoRecorder) kotlin.runCatching {
saveAsLastRecording(videoRecorder as RecorderModel)
saveRecording(videoRecorder)
}
videoRecorder.destroyService(context)
}
} }
videoRecorder.onError = { videoRecorder.onError = {
saveAsLastRecording(videoRecorder as RecorderModel) scope.launch {
saveAsLastRecording(videoRecorder as RecorderModel)
showRecorderError = true showRecorderError = true
}
} }
onDispose { onDispose {

View File

@ -89,10 +89,7 @@ fun AudioRecordingStatus(
} }
}, },
onSave = { onSave = {
scope.launch { audioRecorder.onRecordingSave()
audioRecorder.stopRecording(context)
audioRecorder.onRecordingSave()
}
} }
) )

View File

@ -115,10 +115,7 @@ fun VideoRecordingStatus(
} }
}, },
onSave = { onSave = {
scope.launch { videoRecorder.onRecordingSave()
videoRecorder.stopRecording(context)
videoRecorder.onRecordingSave()
}
} }
) )
} }

View File

@ -136,14 +136,9 @@ abstract class BaseRecorderModel<S : IntervalRecorderService.Settings, I, T : In
// TODO: Also show what camera is in use while recording // TODO: Also show what camera is in use while recording
recorderService!!.stopRecording() recorderService!!.stopRecording()
val intent = Intent(context, intentClass)
runCatching { runCatching {
context.unbindService(connection) context.unbindService(connection)
} }
runCatching {
context.stopService(intent)
}
reset()
} }
fun pauseRecording() { fun pauseRecording() {
@ -154,6 +149,16 @@ abstract class BaseRecorderModel<S : IntervalRecorderService.Settings, I, T : In
recorderService!!.resumeRecording() recorderService!!.resumeRecording()
} }
fun destroyService(context: Context) {
recorderService!!.destroy()
reset()
val intent = Intent(context, intentClass)
runCatching {
context.stopService(intent)
}
}
// Bind functions used to manually bind to the service if the app // Bind functions used to manually bind to the service if the app
// is closed and reopened for example // is closed and reopened for example
fun bindToService(context: Context) { fun bindToService(context: Context) {