Skip to content

Commit

Permalink
fix: Fix recording deletions
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Oct 26, 2023
1 parent b2a413f commit 558aa2c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ data class AudioRecorderExporter(
getFolder(context).deleteRecursively()
}

fun hasRecordingsAvailable(context: Context) {
fun hasRecordingsAvailable(context: Context) =
getFolder(context).listFiles()?.isNotEmpty() ?: false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -168,10 +169,7 @@ fun StartRecording(
contentDescription = label
},
colors = ButtonDefaults.textButtonColors(),
onClick = {
audioRecorder.stopRecording(context)
audioRecorder.onRecordingSave()
},
onClick = onSaveLastRecording,
) {
Icon(
Icons.Default.Save,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }
Expand All @@ -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()
Expand Down Expand Up @@ -166,7 +181,9 @@ fun AudioRecorderScreen(
confirmButton = {
Button(
onClick = {
audioRecorder.onRecordingSave()
showRecorderError = false

saveRecording()
},
colors = ButtonDefaults.textButtonColors(),
) {
Expand Down Expand Up @@ -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,
)
}
}
}
6 changes: 2 additions & 4 deletions app/src/main/java/app/myzel394/alibi/ui/utils/file.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -32,9 +32,7 @@ fun rememberFileSaverDialog(

file.value = null

if (it != null) {
callback(it)
}
callback(it)
}

return { it, name ->
Expand Down

0 comments on commit 558aa2c

Please sign in to comment.