fix: Add workaround for DisposableEffect

Signed-off-by: Myzel394 <50424412+Myzel394@users.noreply.github.com>
This commit is contained in:
Myzel394 2024-03-30 22:47:09 +01:00
parent 14d6b36162
commit 798b6f2119
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185

View File

@ -291,47 +291,57 @@ fun RecorderEventsHandler(
}
// Register video recorder events
// Absolutely no idea, but somehow on some devices the `DisposableEffect`
// is registered twice, and THEN disposed once (AFTER being called twice),
// which then causes the `onRecordingSave` to be in a weird state.
// This variable is a workaround to prevent this from happening.
var alreadyRegistered = false
DisposableEffect(Unit) {
Log.d("Alibi", "===== Registering videoRecorder events $videoRecorder")
videoRecorder.onRecordingSave = { cleanupOldFiles ->
saveRecording(videoRecorder as RecorderModel, cleanupOldFiles)
}
videoRecorder.onRecordingStart = {
snackbarHostState.currentSnackbarData?.dismiss()
}
videoRecorder.onError = {
scope.launch {
saveAsLastRecording(videoRecorder as RecorderModel)
runCatching {
videoRecorder.stopRecording(context)
}
runCatching {
videoRecorder.destroyService(context)
}
showRecorderError = true
if (alreadyRegistered) {
onDispose { }
} else {
alreadyRegistered = true
Log.i("Alibi", "===== Registering videoRecorder events $videoRecorder")
videoRecorder.onRecordingSave = { cleanupOldFiles ->
saveRecording(videoRecorder as RecorderModel, cleanupOldFiles)
}
}
videoRecorder.onBatchesFolderNotAccessible = {
scope.launch {
showBatchesInaccessibleError = true
videoRecorder.onRecordingStart = {
snackbarHostState.currentSnackbarData?.dismiss()
}
videoRecorder.onError = {
scope.launch {
saveAsLastRecording(videoRecorder as RecorderModel)
runCatching {
videoRecorder.stopRecording(context)
}
runCatching {
videoRecorder.destroyService(context)
runCatching {
videoRecorder.stopRecording(context)
}
runCatching {
videoRecorder.destroyService(context)
}
showRecorderError = true
}
}
}
videoRecorder.onBatchesFolderNotAccessible = {
scope.launch {
showBatchesInaccessibleError = true
onDispose {
Log.d("Alibi", "===== Disposing videoRecorder events")
videoRecorder.onRecordingSave = {
throw NotImplementedError("onRecordingSave should not be called now")
runCatching {
videoRecorder.stopRecording(context)
}
runCatching {
videoRecorder.destroyService(context)
}
}
}
onDispose {
Log.i("Alibi", "===== Disposing videoRecorder events")
videoRecorder.onRecordingSave = {
throw NotImplementedError("onRecordingSave should not be called now")
}
videoRecorder.onError = {}
}
videoRecorder.onError = {}
}
}