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() {
recordingTimeTimer.shutdown()
NotificationManagerCompat.from(this)
.cancel(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID)
stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf()
}
protected abstract fun startForegroundService()
@ -85,6 +80,13 @@ abstract class RecorderService : LifecycleService() {
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? {
super.onBind(intent)
return binder

View File

@ -82,16 +82,14 @@ fun RecorderEventsHandler(
}
}
fun saveAsLastRecording(
suspend fun saveAsLastRecording(
recorder: RecorderModel
) {
if (!settings.deleteRecordingsImmediately) {
scope.launch {
dataStore.updateData {
it.setLastRecording(
recorder.recorderService!!.getRecordingInformation()
)
}
dataStore.updateData {
it.setLastRecording(
recorder.recorderService!!.getRecordingInformation()
)
}
}
}
@ -189,14 +187,24 @@ fun RecorderEventsHandler(
// Register audio recorder events
DisposableEffect(key1 = audioRecorder, key2 = settings) {
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 = {
saveAsLastRecording(audioRecorder as RecorderModel)
scope.launch {
saveAsLastRecording(audioRecorder as RecorderModel)
showRecorderError = true
showRecorderError = true
}
}
onDispose {
@ -208,14 +216,24 @@ fun RecorderEventsHandler(
// Register video recorder events
DisposableEffect(key1 = videoRecorder, key2 = settings) {
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 = {
saveAsLastRecording(videoRecorder as RecorderModel)
scope.launch {
saveAsLastRecording(videoRecorder as RecorderModel)
showRecorderError = true
showRecorderError = true
}
}
onDispose {

View File

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

View File

@ -115,10 +115,7 @@ fun VideoRecordingStatus(
}
},
onSave = {
scope.launch {
videoRecorder.stopRecording(context)
videoRecorder.onRecordingSave()
}
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
recorderService!!.stopRecording()
val intent = Intent(context, intentClass)
runCatching {
context.unbindService(connection)
}
runCatching {
context.stopService(intent)
}
reset()
}
fun pauseRecording() {
@ -154,6 +149,16 @@ abstract class BaseRecorderModel<S : IntervalRecorderService.Settings, I, T : In
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
// is closed and reopened for example
fun bindToService(context: Context) {