diff --git a/app/src/main/java/app/myzel394/alibi/helpers/AudioRecorderExporter.kt b/app/src/main/java/app/myzel394/alibi/helpers/AudioRecorderExporter.kt index 6be1f67f3..5c1d54ceb 100644 --- a/app/src/main/java/app/myzel394/alibi/helpers/AudioRecorderExporter.kt +++ b/app/src/main/java/app/myzel394/alibi/helpers/AudioRecorderExporter.kt @@ -112,8 +112,7 @@ data class AudioRecorderExporter( getFolder(context).deleteRecursively() } - fun hasRecordingsAvailable(context: Context) { + fun hasRecordingsAvailable(context: Context) = getFolder(context).listFiles()?.isNotEmpty() ?: false - } } } \ No newline at end of file diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/molecules/StartRecording.kt b/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/molecules/StartRecording.kt index ef761282e..72f2f42e5 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/molecules/StartRecording.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/molecules/StartRecording.kt @@ -60,7 +60,8 @@ fun StartRecording( // Loading this from parent, because if we load it ourselves // and permissions have already been granted, initial // settings will be used, instead of the actual settings. - appSettings: AppSettings + appSettings: AppSettings, + onSaveLastRecording: () -> Unit, ) { val context = LocalContext.current @@ -168,10 +169,7 @@ fun StartRecording( contentDescription = label }, colors = ButtonDefaults.textButtonColors(), - onClick = { - audioRecorder.stopRecording(context) - audioRecorder.onRecordingSave() - }, + onClick = onSaveLastRecording, ) { Icon( Icons.Default.Save, diff --git a/app/src/main/java/app/myzel394/alibi/ui/screens/AudioRecorderScreen.kt b/app/src/main/java/app/myzel394/alibi/ui/screens/AudioRecorderScreen.kt index 9a434d9e4..ce3cff6d5 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/screens/AudioRecorderScreen.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/screens/AudioRecorderScreen.kt @@ -36,6 +36,7 @@ import app.myzel394.alibi.ui.utils.rememberFileSaverDialog import app.myzel394.alibi.R import app.myzel394.alibi.dataStore import app.myzel394.alibi.db.AppSettings +import app.myzel394.alibi.db.RecordingInformation import app.myzel394.alibi.helpers.AudioRecorderExporter import app.myzel394.alibi.ui.effects.rememberSettings import app.myzel394.alibi.ui.models.AudioRecorderModel @@ -60,6 +61,14 @@ fun AudioRecorderScreen( if (settings.audioRecorderSettings.deleteRecordingsImmediately) { AudioRecorderExporter.clearAllRecordings(context) } + + if (!AudioRecorderExporter.hasRecordingsAvailable(context)) { + scope.launch { + dataStore.updateData { + it.setLastRecording(null) + } + } + } } var isProcessingAudio by remember { mutableStateOf(false) } @@ -77,28 +86,34 @@ fun AudioRecorderScreen( } } + fun saveRecording() { + scope.launch { + isProcessingAudio = true + + // Give the user some time to see the processing dialog + delay(100) + + try { + val file = AudioRecorderExporter( + audioRecorder.recorderService?.getRecordingInformation() + ?: settings.lastRecording + ?: throw Exception("No recording information available"), + ).concatenateFiles() + + saveFile(file, file.name) + } catch (error: Exception) { + Log.getStackTraceString(error) + } finally { + isProcessingAudio = false + } + } + } + DisposableEffect(key1 = audioRecorder, key2 = settings) { audioRecorder.onRecordingSave = onRecordingSave@{ - val recordingInformation = audioRecorder.recorderService!!.getRecordingInformation() - - scope.launch { - isProcessingAudio = true - - // Give the user some time to see the processing dialog - delay(100) - - try { - val file = AudioRecorderExporter(recordingInformation).concatenateFiles() - - saveFile(file, file.name) + saveAsLastRecording() - saveAsLastRecording() - } catch (error: Exception) { - Log.getStackTraceString(error) - } finally { - isProcessingAudio = false - } - } + saveRecording() } audioRecorder.onError = { saveAsLastRecording() @@ -166,7 +181,9 @@ fun AudioRecorderScreen( confirmButton = { Button( onClick = { - audioRecorder.onRecordingSave() + showRecorderError = false + + saveRecording() }, colors = ButtonDefaults.textButtonColors(), ) { @@ -206,7 +223,10 @@ fun AudioRecorderScreen( if (audioRecorder.isInRecording) RecordingStatus(audioRecorder = audioRecorder) else - StartRecording(audioRecorder = audioRecorder, appSettings = appSettings) + StartRecording( + audioRecorder = audioRecorder, appSettings = appSettings, + onSaveLastRecording = ::saveRecording, + ) } } } \ No newline at end of file diff --git a/app/src/main/java/app/myzel394/alibi/ui/utils/file.kt b/app/src/main/java/app/myzel394/alibi/ui/utils/file.kt index db61daf2b..f90d01bd6 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/utils/file.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/utils/file.kt @@ -14,7 +14,7 @@ import java.io.File @Composable fun rememberFileSaverDialog( mimeType: String, - callback: (Uri) -> Unit = {}, + callback: (Uri?) -> Unit = {}, ): ((File, String) -> Unit) { val context = LocalContext.current @@ -32,9 +32,7 @@ fun rememberFileSaverDialog( file.value = null - if (it != null) { - callback(it) - } + callback(it) } return { it, name ->