fix: Move lastRecording to AppSettings

This commit is contained in:
Myzel394 2023-10-26 19:41:13 +02:00
parent 369050e94e
commit eacf3cc2f1
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
6 changed files with 34 additions and 19 deletions

View File

@ -18,6 +18,7 @@ data class AppSettings(
val hasSeenOnboarding: Boolean = false,
val showAdvancedSettings: Boolean = false,
val theme: Theme = Theme.SYSTEM,
val lastRecording: RecordingInformation? = null,
) {
fun setShowAdvancedSettings(showAdvancedSettings: Boolean): AppSettings {
return copy(showAdvancedSettings = showAdvancedSettings)
@ -39,6 +40,10 @@ data class AppSettings(
return copy(theme = theme)
}
fun setLastRecording(lastRecording: RecordingInformation?): AppSettings {
return copy(lastRecording = lastRecording)
}
enum class Theme {
SYSTEM,
LIGHT,

View File

@ -30,7 +30,7 @@ abstract class IntervalRecorderService : ExtraRecorderInformationService() {
protected val outputFolder: File
get() = AudioRecorderExporter.getFolder(this)
fun createLastRecording(): RecordingInformation = RecordingInformation(
fun getRecordingInformation(): RecordingInformation = RecordingInformation(
folderPath = outputFolder.absolutePath,
recordingStart = recordingStart,
maxDuration = settings!!.maxDuration,

View File

@ -148,7 +148,7 @@ fun StartRecording(
.fillMaxWidth(),
textAlign = TextAlign.Center,
)
if (audioRecorder.lastRecording != null && audioRecorder.lastRecording!!.hasRecordingsAvailable) {
if (appSettings.lastRecording?.hasRecordingsAvailable == true) {
Column(
modifier = Modifier.weight(1f),
horizontalAlignment = Alignment.CenterHorizontally,
@ -157,7 +157,7 @@ fun StartRecording(
val label = stringResource(
R.string.ui_audioRecorder_action_saveOldRecording_label,
DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.format(audioRecorder.lastRecording!!.recordingStart),
.format(appSettings.lastRecording.recordingStart),
)
Button(
modifier = Modifier

View File

@ -106,7 +106,8 @@ fun RecordingStatus(
) {
DeleteButton(
onDelete = {
audioRecorder.stopRecording(context, saveAsLastRecording = false)
audioRecorder.stopRecording(context)
AudioRecorderExporter.clearAllRecordings(context)
}
)

View File

@ -42,9 +42,6 @@ class AudioRecorderModel : ViewModel() {
var recorderService: AudioRecorderService? = null
private set
var lastRecording: RecordingInformation? by mutableStateOf<RecordingInformation?>(null)
private set
var onRecordingSave: () -> Unit = {}
var onError: () -> Unit = {}
var notificationDetails: RecorderNotificationHelper.NotificationDetails? = null
@ -73,7 +70,6 @@ class AudioRecorderModel : ViewModel() {
onAmplitudeChange()
}
recorder.onError = {
recorderService!!.createLastRecording()
onError()
}
recorder.onSelectedMicrophoneChange = { microphone ->
@ -132,11 +128,7 @@ class AudioRecorderModel : ViewModel() {
context.bindService(intent, connection, Context.BIND_AUTO_CREATE)
}
fun stopRecording(context: Context, saveAsLastRecording: Boolean = true) {
if (saveAsLastRecording) {
lastRecording = recorderService!!.createLastRecording()
}
fun stopRecording(context: Context) {
runCatching {
context.unbindService(connection)
}

View File

@ -50,6 +50,7 @@ fun AudioRecorder(
) {
val context = LocalContext.current
val dataStore = context.dataStore
val settings = rememberSettings()
val scope = rememberCoroutineScope()
@ -64,8 +65,22 @@ fun AudioRecorder(
var isProcessingAudio by remember { mutableStateOf(false) }
var showRecorderError by remember { mutableStateOf(false) }
DisposableEffect(key1 = audioRecorder, key2 = settings.audioRecorderSettings) {
audioRecorder.onRecordingSave = {
fun saveAsLastRecording() {
if (!settings.audioRecorderSettings.deleteRecordingsImmediately) {
scope.launch {
dataStore.updateData {
it.setLastRecording(
audioRecorder.recorderService!!.getRecordingInformation()
)
}
}
}
}
DisposableEffect(key1 = audioRecorder, key2 = settings) {
audioRecorder.onRecordingSave = onRecordingSave@{
val recordingInformation = audioRecorder.recorderService!!.getRecordingInformation()
scope.launch {
isProcessingAudio = true
@ -73,10 +88,11 @@ fun AudioRecorder(
delay(100)
try {
val file =
AudioRecorderExporter(audioRecorder.lastRecording!!).concatenateFiles()
val file = AudioRecorderExporter(recordingInformation).concatenateFiles()
saveFile(file, file.name)
saveAsLastRecording()
} catch (error: Exception) {
Log.getStackTraceString(error)
} finally {
@ -85,8 +101,9 @@ fun AudioRecorder(
}
}
audioRecorder.onError = {
// No need to save last recording as it's done automatically on error
audioRecorder.stopRecording(context, saveAsLastRecording = false)
saveAsLastRecording()
audioRecorder.stopRecording(context)
showRecorderError = true
}