fix: Fix recording deletions

This commit is contained in:
Myzel394 2023-10-26 19:56:52 +02:00
parent b2a413f609
commit 558aa2c86f
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
4 changed files with 47 additions and 32 deletions

View File

@ -112,8 +112,7 @@ data class AudioRecorderExporter(
getFolder(context).deleteRecursively()
}
fun hasRecordingsAvailable(context: Context) {
fun hasRecordingsAvailable(context: Context) =
getFolder(context).listFiles()?.isNotEmpty() ?: false
}
}
}

View File

@ -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,

View File

@ -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()
saveAsLastRecording()
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()
} 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,
)
}
}
}

View File

@ -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 ->