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

View File

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

View File

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

View File

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

View File

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

View File

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