From 68438a6a0bc87d2bc59c24803e11956a0d55dd94 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:08:58 +0200 Subject: [PATCH] refactor: Move settings out of SettingsScreen's atoms --- .../atoms/SaveRecordingButton.kt | 110 ------------------ .../SettingsScreen/atoms/BitrateTile.kt | 8 +- .../atoms/CustomNotificationTile.kt | 5 +- .../atoms/DeleteRecordingsImmediatelyTile.kt | 8 +- .../SettingsScreen/atoms/EncoderTile.kt | 13 +-- .../atoms/ForceExactMaxDurationTile.kt | 9 +- .../atoms/InAppLanguagePicker.kt | 1 + .../atoms/IntervalDurationTile.kt | 8 +- .../SettingsScreen/atoms/MaxDurationTile.kt | 8 +- .../SettingsScreen/atoms/OutputFormatTile.kt | 16 +-- .../SettingsScreen/atoms/SamplingRateTile.kt | 8 +- .../atoms/ShowAllMicrophonesTile.kt | 8 +- .../alibi/ui/screens/SettingsScreen.kt | 26 ++--- 13 files changed, 44 insertions(+), 184 deletions(-) delete mode 100644 app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/atoms/SaveRecordingButton.kt diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/atoms/SaveRecordingButton.kt b/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/atoms/SaveRecordingButton.kt deleted file mode 100644 index 1933c685a..000000000 --- a/app/src/main/java/app/myzel394/alibi/ui/components/AudioRecorder/atoms/SaveRecordingButton.kt +++ /dev/null @@ -1,110 +0,0 @@ -package app.myzel394.alibi.ui.components.AudioRecorder.atoms - -import android.util.Log -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.size -import androidx.compose.foundation.layout.width -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Memory -import androidx.compose.material.icons.filled.Save -import androidx.compose.material3.AlertDialog -import androidx.compose.material3.Button -import androidx.compose.material3.ButtonDefaults -import androidx.compose.material3.Icon -import androidx.compose.material3.LinearProgressIndicator -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.rememberCoroutineScope -import androidx.compose.runtime.setValue -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.graphicsLayer -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.semantics.contentDescription -import androidx.compose.ui.semantics.semantics -import androidx.compose.ui.unit.dp -import app.myzel394.alibi.R -import app.myzel394.alibi.services.RecorderService -import app.myzel394.alibi.ui.BIG_PRIMARY_BUTTON_SIZE -import kotlinx.coroutines.launch -import java.io.File - -@Composable -fun SaveRecordingButton( - modifier: Modifier = Modifier, - service: RecorderService, - onSaveFile: (File) -> Unit, - label: String = stringResource(R.string.ui_audioRecorder_action_save_label), -) { - val context = LocalContext.current - val scope = rememberCoroutineScope() - - var isProcessingAudio by remember { mutableStateOf(false) } - - if (isProcessingAudio) - AlertDialog( - onDismissRequest = { }, - icon = { - Icon( - Icons.Default.Memory, - contentDescription = null, - ) - }, - title = { - Text( - stringResource(R.string.ui_audioRecorder_action_save_processing_dialog_title), - ) - }, - text = { - Column( - horizontalAlignment = Alignment.CenterHorizontally, - ) { - Text( - stringResource(R.string.ui_audioRecorder_action_save_processing_dialog_description), - ) - Spacer(modifier = Modifier.height(32.dp)) - LinearProgressIndicator() - } - }, - confirmButton = {} - ) - Button( - modifier = Modifier - .padding(16.dp) - .fillMaxWidth() - .height(BIG_PRIMARY_BUTTON_SIZE) - .semantics { - contentDescription = label - } - .then(modifier), - onClick = { - isProcessingAudio = true - - scope.launch { - try { - } catch (error: Exception) { - Log.getStackTraceString(error) - } finally { - isProcessingAudio = false - } - } - }, - ) { - Icon( - Icons.Default.Save, - contentDescription = null, - modifier = Modifier.size(ButtonDefaults.IconSize), - ) - Spacer(modifier = Modifier.width(ButtonDefaults.IconSpacing)) - Text(label) - } -} \ No newline at end of file diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/BitrateTile.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/BitrateTile.kt index 9618b3965..5e070f9c1 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/BitrateTile.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/BitrateTile.kt @@ -36,14 +36,12 @@ import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @Composable -fun BitrateTile() { +fun BitrateTile( + settings: AppSettings, +) { val scope = rememberCoroutineScope() val showDialog = rememberUseCaseState() val dataStore = LocalContext.current.dataStore - val settings = dataStore - .data - .collectAsState(initial = AppSettings.getDefaultInstance()) - .value fun updateValue(bitRate: Int) { scope.launch { diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/CustomNotificationTile.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/CustomNotificationTile.kt index 76be693fd..d32bde411 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/CustomNotificationTile.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/CustomNotificationTile.kt @@ -22,12 +22,9 @@ import app.myzel394.alibi.ui.enums.Screen @Composable fun CustomNotificationTile( navController: NavController, + settings: AppSettings, ) { val dataStore = LocalContext.current.dataStore - val settings = dataStore - .data - .collectAsState(initial = AppSettings.getDefaultInstance()) - .value val label = if (settings.notificationSettings == null) stringResource(R.string.ui_settings_option_customNotification_description_setup) diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/DeleteRecordingsImmediatelyTile.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/DeleteRecordingsImmediatelyTile.kt index 453f9b662..5dfafebbc 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/DeleteRecordingsImmediatelyTile.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/DeleteRecordingsImmediatelyTile.kt @@ -16,14 +16,12 @@ import app.myzel394.alibi.ui.components.atoms.SettingsTile import kotlinx.coroutines.launch @Composable -fun DeleteRecordingsImmediatelyTile() { +fun DeleteRecordingsImmediatelyTile( + settings: AppSettings, +) { val scope = rememberCoroutineScope() val dataStore = LocalContext.current.dataStore - val settings = dataStore - .data - .collectAsState(initial = AppSettings.getDefaultInstance()) - .value SettingsTile( title = stringResource(R.string.ui_settings_option_deleteRecordingsImmediately_title), diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/EncoderTile.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/EncoderTile.kt index e574e54f3..c8ad06e45 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/EncoderTile.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/EncoderTile.kt @@ -2,7 +2,6 @@ package app.myzel394.alibi.ui.components.SettingsScreen.atoms import android.media.MediaRecorder import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.AudioFile import androidx.compose.material.icons.filled.Memory import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults @@ -35,17 +34,15 @@ import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @Composable fun EncoderTile( - snackbarHostState: SnackbarHostState + snackbarHostState: SnackbarHostState, + settings: AppSettings, ) { val scope = rememberCoroutineScope() val showDialog = rememberUseCaseState() val dataStore = LocalContext.current.dataStore - val settings = dataStore - .data - .collectAsState(initial = AppSettings.getDefaultInstance()) - .value - val updatedOutputFormatLabel = stringResource(R.string.ui_settings_option_encoder_extra_outputFormatChanged) + val updatedOutputFormatLabel = + stringResource(R.string.ui_settings_option_encoder_extra_outputFormatChanged) fun updateValue(encoder: Int?) { scope.launch { @@ -92,7 +89,7 @@ fun EncoderTile( selected = settings.audioRecorderSettings.encoder == index, ) }.toList() - ) {index, option -> + ) { index, _ -> updateValue(index) }, ) diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/ForceExactMaxDurationTile.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/ForceExactMaxDurationTile.kt index 5050df5d0..5baea8c2f 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/ForceExactMaxDurationTile.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/ForceExactMaxDurationTile.kt @@ -18,14 +18,11 @@ import kotlinx.coroutines.launch @Composable -fun ForceExactMaxDurationTile() { +fun ForceExactMaxDurationTile( + settings: AppSettings, +) { val scope = rememberCoroutineScope() - val showDialog = rememberUseCaseState() val dataStore = LocalContext.current.dataStore - val settings = dataStore - .data - .collectAsState(initial = AppSettings.getDefaultInstance()) - .value fun updateValue(forceExactMaxDuration: Boolean) { scope.launch { diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/InAppLanguagePicker.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/InAppLanguagePicker.kt index c7d4714ca..bb3b1c5cf 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/InAppLanguagePicker.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/InAppLanguagePicker.kt @@ -24,6 +24,7 @@ import androidx.compose.ui.res.stringResource import androidx.core.os.LocaleListCompat import app.myzel394.alibi.R import app.myzel394.alibi.SUPPORTED_LOCALES +import app.myzel394.alibi.db.AppSettings import app.myzel394.alibi.db.AudioRecorderSettings import app.myzel394.alibi.ui.components.atoms.SettingsTile import app.myzel394.alibi.ui.utils.IconResource diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/IntervalDurationTile.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/IntervalDurationTile.kt index 3437568c9..c8dc328c4 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/IntervalDurationTile.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/IntervalDurationTile.kt @@ -34,14 +34,12 @@ import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @Composable -fun IntervalDurationTile() { +fun IntervalDurationTile( + settings: AppSettings, +) { val scope = rememberCoroutineScope() val showDialog = rememberUseCaseState() val dataStore = LocalContext.current.dataStore - val settings = dataStore - .data - .collectAsState(initial = AppSettings.getDefaultInstance()) - .value fun updateValue(intervalDuration: Long) { scope.launch { diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/MaxDurationTile.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/MaxDurationTile.kt index 03d897d11..aacd9f0e1 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/MaxDurationTile.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/MaxDurationTile.kt @@ -33,14 +33,12 @@ import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @Composable -fun MaxDurationTile() { +fun MaxDurationTile( + settings: AppSettings, +) { val scope = rememberCoroutineScope() val showDialog = rememberUseCaseState() val dataStore = LocalContext.current.dataStore - val settings = dataStore - .data - .collectAsState(initial = AppSettings.getDefaultInstance()) - .value fun updateValue(maxDuration: Long) { scope.launch { diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/OutputFormatTile.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/OutputFormatTile.kt index f5d4f8c92..2d52c9f39 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/OutputFormatTile.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/OutputFormatTile.kt @@ -1,6 +1,5 @@ package app.myzel394.alibi.ui.components.SettingsScreen.atoms -import android.media.MediaRecorder import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.AudioFile import androidx.compose.material3.Button @@ -8,13 +7,8 @@ import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.SnackbarDuration -import androidx.compose.material3.SnackbarHostState -import androidx.compose.material3.SnackbarVisuals import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.collectAsState -import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource @@ -35,14 +29,12 @@ import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @Composable -fun OutputFormatTile() { +fun OutputFormatTile( + settings: AppSettings, +) { val scope = rememberCoroutineScope() val showDialog = rememberUseCaseState() val dataStore = LocalContext.current.dataStore - val settings = dataStore - .data - .collectAsState(initial = AppSettings.getDefaultInstance()) - .value val availableOptions = if (settings.audioRecorderSettings.encoder == null) AudioRecorderSettings.OUTPUT_FORMAT_INDEX_TEXT_MAP.keys.toTypedArray() else AudioRecorderSettings.ENCODER_SUPPORTED_OUTPUT_FORMATS_MAP[settings.audioRecorderSettings.encoder]!! @@ -74,7 +66,7 @@ fun OutputFormatTile() { selected = settings.audioRecorderSettings.outputFormat == option, ) }.toList() - ) {index, option -> + ) { index, _ -> updateValue(availableOptions[index]) }, ) diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/SamplingRateTile.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/SamplingRateTile.kt index e3eeb3e7e..c7a735239 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/SamplingRateTile.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/SamplingRateTile.kt @@ -36,14 +36,12 @@ import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @Composable -fun SamplingRateTile() { +fun SamplingRateTile( + settings: AppSettings, +) { val scope = rememberCoroutineScope() val showDialog = rememberUseCaseState() val dataStore = LocalContext.current.dataStore - val settings = dataStore - .data - .collectAsState(initial = AppSettings.getDefaultInstance()) - .value fun updateValue(samplingRate: Int?) { scope.launch { diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/ShowAllMicrophonesTile.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/ShowAllMicrophonesTile.kt index f2a1d8e49..001b4cbaa 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/ShowAllMicrophonesTile.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/ShowAllMicrophonesTile.kt @@ -19,13 +19,11 @@ import kotlinx.coroutines.launch @Composable -fun ShowAllMicrophonesTile() { +fun ShowAllMicrophonesTile( + settings: AppSettings, +) { val scope = rememberCoroutineScope() val dataStore = LocalContext.current.dataStore - val settings = dataStore - .data - .collectAsState(initial = AppSettings.getDefaultInstance()) - .value fun updateValue(showAllMicrophones: Boolean) { scope.launch { diff --git a/app/src/main/java/app/myzel394/alibi/ui/screens/SettingsScreen.kt b/app/src/main/java/app/myzel394/alibi/ui/screens/SettingsScreen.kt index f9e8ab844..0a1b4f96d 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/screens/SettingsScreen.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/screens/SettingsScreen.kt @@ -56,6 +56,7 @@ import app.myzel394.alibi.ui.components.SettingsScreen.atoms.ThemeSelector import app.myzel394.alibi.ui.components.atoms.GlobalSwitch import app.myzel394.alibi.ui.components.atoms.MessageBox import app.myzel394.alibi.ui.components.atoms.MessageType +import app.myzel394.alibi.ui.effects.rememberSettings import app.myzel394.alibi.ui.models.AudioRecorderModel import kotlinx.coroutines.launch @@ -114,10 +115,7 @@ fun SettingsScreen( ) { val scope = rememberCoroutineScope() val dataStore = LocalContext.current.dataStore - val settings = dataStore - .data - .collectAsState(initial = AppSettings.getDefaultInstance()) - .value + val settings = rememberSettings() // Show alert if (audioRecorder.isInRecording) @@ -145,12 +143,12 @@ fun SettingsScreen( } } ) - MaxDurationTile() - IntervalDurationTile() - ForceExactMaxDurationTile() + MaxDurationTile(settings = settings) + IntervalDurationTile(settings = settings) + ForceExactMaxDurationTile(settings = settings) InAppLanguagePicker() - DeleteRecordingsImmediatelyTile() - CustomNotificationTile(navController = navController) + DeleteRecordingsImmediatelyTile(settings = settings) + CustomNotificationTile(navController = navController, settings = settings) AboutTile(navController = navController) AnimatedVisibility(visible = settings.showAdvancedSettings) { Column( @@ -163,11 +161,11 @@ fun SettingsScreen( .fillMaxWidth() .padding(horizontal = 16.dp, vertical = 32.dp) ) - ShowAllMicrophonesTile() - BitrateTile() - SamplingRateTile() - EncoderTile(snackbarHostState = snackbarHostState) - OutputFormatTile() + ShowAllMicrophonesTile(settings = settings) + BitrateTile(settings = settings) + SamplingRateTile(settings = settings) + EncoderTile(snackbarHostState = snackbarHostState, settings = settings) + OutputFormatTile(settings = settings) } Divider( modifier = Modifier