From 9f4979ce9db08fa239031b2f2e24fadb5ba60422 Mon Sep 17 00:00:00 2001 From: FunkyMuse Date: Wed, 20 Sep 2023 11:54:33 +0200 Subject: [PATCH] feat: confirmation dialog --- .../thankyou/activities/SettingsActivity.kt | 64 ++++++++++++++++--- .../screens/alert_dialog/AlertDialogState.kt | 39 +++++++++++ 2 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/thankyou/screens/alert_dialog/AlertDialogState.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/thankyou/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/thankyou/activities/SettingsActivity.kt index 0630341..235c1c6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/thankyou/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/thankyou/activities/SettingsActivity.kt @@ -4,13 +4,19 @@ import android.annotation.SuppressLint import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent -import androidx.compose.runtime.derivedStateOf -import androidx.compose.runtime.getValue -import androidx.compose.runtime.remember +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.* +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.window.DialogProperties import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.simplemobiletools.commons.compose.extensions.enableEdgeToEdgeSimple import com.simplemobiletools.commons.compose.theme.AppThemeSurface -import com.simplemobiletools.commons.dialogs.ConfirmationDialog +import com.simplemobiletools.commons.compose.theme.Shapes import com.simplemobiletools.commons.extensions.getAppIconColors import com.simplemobiletools.commons.extensions.toggleAppIconColor import com.simplemobiletools.commons.helpers.isTiramisuPlus @@ -20,6 +26,8 @@ import com.simplemobiletools.thankyou.extensions.config import com.simplemobiletools.thankyou.extensions.launchChangeAppLanguageIntent import com.simplemobiletools.thankyou.extensions.startCustomizationActivity import com.simplemobiletools.thankyou.screens.SettingsScreen +import com.simplemobiletools.thankyou.screens.alert_dialog.AlertDialogState +import com.simplemobiletools.thankyou.screens.alert_dialog.rememberAlertDialogState import java.util.Locale import kotlin.system.exitProcess @@ -35,13 +43,16 @@ class SettingsActivity : ComponentActivity() { AppThemeSurface { val wasUseEnglishToggledFlow by preferences.wasUseEnglishToggledFlow.collectAsStateWithLifecycle(preferences.wasUseEnglishToggled) val useEnglishFlow by preferences.useEnglishFlow.collectAsStateWithLifecycle(preferences.useEnglish) - val hideLauncherIconFlow by preferences.hideLauncherIconFlow.collectAsStateWithLifecycle(preferences.useEnglish) + val hideLauncherIconFlow by preferences.hideLauncherIconFlow.collectAsStateWithLifecycle(preferences.hideLauncherIcon) val displayLanguage = remember { Locale.getDefault().displayLanguage } val isUseEnglishEnabled by remember(wasUseEnglishToggledFlow) { derivedStateOf { (wasUseEnglishToggledFlow || Locale.getDefault().language != "en") && !isTiramisuPlus() } } + val alertDialogState = rememberAlertDialogState() + ConfirmationDialog(alertDialogState) + SettingsScreen( displayLanguage = displayLanguage, isUseEnglishEnabled = isUseEnglishEnabled, @@ -53,13 +64,11 @@ class SettingsActivity : ComponentActivity() { onSetupLanguagePress = ::launchChangeAppLanguageIntent, isHidingLauncherIcon = hideLauncherIconFlow, hideLauncherIconClick = { isChecked -> - preferences.hideLauncherIcon = isChecked if (isChecked) { - toggleHideLauncherIcon() + alertDialogState.show() } else { - ConfirmationDialog(this, "", R.string.hide_launcher_icon_explanation, R.string.ok, R.string.cancel) { - toggleHideLauncherIcon() - } + toggleHideLauncherIcon() + preferences.hideLauncherIcon = false } }, customizeColors = ::startCustomizationActivity, @@ -69,6 +78,41 @@ class SettingsActivity : ComponentActivity() { } } + @Composable + private fun ConfirmationDialog(alertDialogState: AlertDialogState) { + alertDialogState.DialogMember { + AlertDialog( + modifier = Modifier.fillMaxWidth(0.9f), + properties = DialogProperties(usePlatformDefaultWidth = false), + onDismissRequest = alertDialogState::hide, + confirmButton = { + TextButton(onClick = { + alertDialogState.hide() + preferences.hideLauncherIcon = true + toggleHideLauncherIcon() + }) { + Text(text = stringResource(id = R.string.ok)) + } + }, + dismissButton = { + TextButton(onClick = { + alertDialogState.hide() + preferences.hideLauncherIcon = false + }) { + Text(text = stringResource(id = R.string.cancel)) + } + }, + shape = Shapes.large, + text = { + Text( + text = stringResource(id = R.string.hide_launcher_icon_explanation), + color = MaterialTheme.colorScheme.onSurface + ) + }, + ) + } + } + private fun toggleHideLauncherIcon() { val appId = BuildConfig.APPLICATION_ID getAppIconColors().forEachIndexed { index, color -> diff --git a/app/src/main/kotlin/com/simplemobiletools/thankyou/screens/alert_dialog/AlertDialogState.kt b/app/src/main/kotlin/com/simplemobiletools/thankyou/screens/alert_dialog/AlertDialogState.kt new file mode 100644 index 0000000..b80f1f7 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/thankyou/screens/alert_dialog/AlertDialogState.kt @@ -0,0 +1,39 @@ +package com.simplemobiletools.thankyou.screens.alert_dialog + +import androidx.compose.runtime.* + +@Composable +fun rememberAlertDialogState( + isShownInitially: Boolean = false +) = remember { AlertDialogState(isShownInitially) } + +@Stable +class AlertDialogState(isShownInitially: Boolean = false) { + var isShown by mutableStateOf(isShownInitially) + private set + + fun show() { + isShown = true + } + + fun hide() { + isShown = false + } + + fun toggle() { + isShown = !isShown + } + + fun changeValue(predicate: Boolean) { + isShown = predicate + } + + @Composable + fun DialogMember( + content: @Composable () -> Unit + ) { + if (isShown) { + content() + } + } +}