From bd4af5f26aad5a4bd63ac3c0923b1b5034564272 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 28 Oct 2023 18:40:59 +0200 Subject: [PATCH] feat: Add BootBehaviorTile to SettingsScreen --- .../SettingsScreen/atoms/BootBehaviorTile.kt | 63 +++++++++++++------ .../alibi/ui/screens/SettingsScreen.kt | 2 + app/src/main/res/values/strings.xml | 7 +++ 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/BootBehaviorTile.kt b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/BootBehaviorTile.kt index efa48da..9982f5b 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/BootBehaviorTile.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/SettingsScreen/atoms/BootBehaviorTile.kt @@ -9,12 +9,16 @@ import androidx.compose.material.icons.filled.Smartphone import androidx.compose.material.icons.filled.Translate import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon +import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext 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.dataStore import app.myzel394.alibi.db.AppSettings import app.myzel394.alibi.ui.components.atoms.SettingsTile import app.myzel394.alibi.ui.utils.IconResource @@ -26,23 +30,43 @@ import com.maxkeppeker.sheets.core.models.base.rememberUseCaseState import com.maxkeppeler.sheets.list.ListDialog import com.maxkeppeler.sheets.list.models.ListOption import com.maxkeppeler.sheets.list.models.ListSelection +import kotlinx.coroutines.launch import java.util.Locale -/* val BOOT_BEHAVIOR_TITLE_MAP = mapOf( AppSettings.BootBehavior.SHOW_NOTIFICATION to R.string.ui_settings_bootBehavior_values_SHOW_NOTIFICATION_title, - AppSettings.BootBehavior.START_RECORDING to R.string.ui_settings_bootBehavior_values_START_RECORDING_title + AppSettings.BootBehavior.START_RECORDING to R.string.ui_settings_bootBehavior_values_START_RECORDING_title, + AppSettings.BootBehavior.CONTINUE_RECORDING to R.string.ui_settings_bootBehavior_values_CONTINUE_RECORDING_title, +) +val BOOT_BEHAVIOR_DESCRIPTION_MAP = mapOf( + AppSettings.BootBehavior.SHOW_NOTIFICATION to R.string.ui_settings_bootBehavior_values_SHOW_NOTIFICATION_description, + AppSettings.BootBehavior.START_RECORDING to R.string.ui_settings_bootBehavior_values_START_RECORDING_description, + AppSettings.BootBehavior.CONTINUE_RECORDING to R.string.ui_settings_bootBehavior_values_CONTINUE_RECORDING_description, ) @OptIn(ExperimentalMaterial3Api::class) @Composable -fun BootBehaviorTile() { +fun BootBehaviorTile( + settings: AppSettings, +) { + val scope = rememberCoroutineScope() val showDialog = rememberUseCaseState() + val dataStore = LocalContext.current.dataStore + + fun updateValue(behavior: AppSettings.BootBehavior?) { + scope.launch { + dataStore.updateData { + it.setBootBehavior( + behavior + ) + } + } + } ListDialog( state = showDialog, header = Header.Default( - title = stringResource(R.string.ui_settings_bootBehavior_title), + title = stringResource(R.string.ui_settings_bootBehavior_help), icon = IconSource( painter = IconResource.fromImageVector(Icons.Default.Smartphone) .asPainterResource(), @@ -53,18 +77,19 @@ fun BootBehaviorTile() { showRadioButtons = true, options = AppSettings.BootBehavior.entries.map { ListOption( - titleText = stringResource("ui_settings_bootBehavior_values_${it.name}_title"), - subtitleText = stringResource("ui_settings_bootBehavior_values_${it.name}_subtitle"), + titleText = stringResource( + BOOT_BEHAVIOR_TITLE_MAP[it]!! + ), + subtitleText = stringResource( + BOOT_BEHAVIOR_DESCRIPTION_MAP[it]!! + ), + selected = settings.bootBehavior == it, ) - }.toList(), - options = IntRange(0, AppSettings.BootBehavior.entries.size).map { index -> - val locale = locales[index]!! - + }.toList() + listOf( ListOption( - titleText = locale.displayName, - subtitleText = locale.getDisplayName(Locale.ENGLISH), + titleText = stringResource(R.string.ui_settings_bootBehavior_values_nothing_title), ) - }.toList(), + ), positiveButton = SelectionButton( icon = IconSource( painter = IconResource.fromImageVector(Icons.Default.CheckCircle) @@ -75,11 +100,8 @@ fun BootBehaviorTile() { type = ButtonStyle.TEXT, ) ) { index, _ -> - AppCompatDelegate.setApplicationLocales( - LocaleListCompat.forLanguageTags( - locales[index]!!.toLanguageTag(), - ), - ) + val behavior = AppSettings.BootBehavior.values().getOrNull(index) + updateValue(behavior) }, ) SettingsTile( @@ -89,6 +111,9 @@ fun BootBehaviorTile() { showDialog.show() }, title = stringResource(R.string.ui_settings_bootBehavior_title), + description = stringResource( + BOOT_BEHAVIOR_TITLE_MAP[settings.bootBehavior] ?: R.string.ui_settings_bootBehavior_help + ), leading = { Icon( Icons.Default.Smartphone, @@ -97,5 +122,3 @@ fun BootBehaviorTile() { }, ) } - - */ \ No newline at end of file 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 0a1b4f9..f28bafa 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 @@ -41,6 +41,7 @@ import app.myzel394.alibi.db.AppSettings import app.myzel394.alibi.ui.SUPPORTS_DARK_MODE_NATIVELY import app.myzel394.alibi.ui.components.SettingsScreen.atoms.AboutTile import app.myzel394.alibi.ui.components.SettingsScreen.atoms.BitrateTile +import app.myzel394.alibi.ui.components.SettingsScreen.atoms.BootBehaviorTile import app.myzel394.alibi.ui.components.SettingsScreen.atoms.CustomNotificationTile import app.myzel394.alibi.ui.components.SettingsScreen.atoms.DeleteRecordingsImmediatelyTile import app.myzel394.alibi.ui.components.SettingsScreen.atoms.EncoderTile @@ -149,6 +150,7 @@ fun SettingsScreen( InAppLanguagePicker() DeleteRecordingsImmediatelyTile(settings = settings) CustomNotificationTile(navController = navController, settings = settings) + BootBehaviorTile(settings = settings) AboutTile(navController = navController) AnimatedVisibility(visible = settings.showAdvancedSettings) { Column( diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f33ccf0..c1db6e5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -115,4 +115,11 @@ If enabled, you\'ll be informed that your recording was interrupted Alibi was interrupted Your device restarted and your recording has been interrupted + Always start recording + Continue recording + Show a notification if recording has been interrupted + Continue recording, if interrupted, otherwise start a new recording + If recording has been interrupted, continue it + Do nothing + What should Alibi do when your phone boots up? \ No newline at end of file