feat: confirmation dialog

This commit is contained in:
FunkyMuse 2023-09-20 11:54:33 +02:00
parent 631dd63448
commit 9f4979ce9d
2 changed files with 93 additions and 10 deletions

View File

@ -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 ->

View File

@ -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()
}
}
}