forked from geode-sdk/android-launcher
stop using alertdialog for settings
This commit is contained in:
1 file changed
+128
-104
@@ -8,6 +8,7 @@ import androidx.compose.foundation.clickable
|
|||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.ColumnScope
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.Spacer
|
import androidx.compose.foundation.layout.Spacer
|
||||||
import androidx.compose.foundation.layout.defaultMinSize
|
import androidx.compose.foundation.layout.defaultMinSize
|
||||||
@@ -23,8 +24,8 @@ import androidx.compose.material.icons.Icons
|
|||||||
import androidx.compose.material.icons.filled.Add
|
import androidx.compose.material.icons.filled.Add
|
||||||
import androidx.compose.material.icons.filled.Clear
|
import androidx.compose.material.icons.filled.Clear
|
||||||
import androidx.compose.material.icons.filled.Delete
|
import androidx.compose.material.icons.filled.Delete
|
||||||
import androidx.compose.material3.AlertDialog
|
|
||||||
import androidx.compose.material3.Card
|
import androidx.compose.material3.Card
|
||||||
|
import androidx.compose.material3.CardDefaults
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.material3.IconButton
|
import androidx.compose.material3.IconButton
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
@@ -248,33 +249,39 @@ fun StringDialog(
|
|||||||
) {
|
) {
|
||||||
var enteredValue by remember { mutableStateOf(initialValue) }
|
var enteredValue by remember { mutableStateOf(initialValue) }
|
||||||
|
|
||||||
AlertDialog(
|
BaseDialog(
|
||||||
onDismissRequest = { onDismissRequest() },
|
onDismissRequest = { onDismissRequest() },
|
||||||
title = {
|
title = title,
|
||||||
Text(title)
|
content = {
|
||||||
},
|
Column(
|
||||||
text = {
|
modifier = Modifier
|
||||||
OutlinedTextField(
|
.fillMaxWidth()
|
||||||
value = enteredValue,
|
.padding(horizontal = 12.dp),
|
||||||
onValueChange = {
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
enteredValue = filterInput?.invoke(it) ?: it
|
) {
|
||||||
},
|
OutlinedTextField(
|
||||||
singleLine = true,
|
value = enteredValue,
|
||||||
keyboardOptions = KeyboardOptions(
|
onValueChange = {
|
||||||
keyboardType = KeyboardType.Ascii,
|
enteredValue = filterInput?.invoke(it) ?: it
|
||||||
autoCorrectEnabled = false
|
},
|
||||||
),
|
singleLine = true,
|
||||||
trailingIcon = {
|
keyboardOptions = KeyboardOptions(
|
||||||
if (enteredValue.isNotEmpty()) {
|
keyboardType = KeyboardType.Ascii,
|
||||||
IconButton(onClick = { enteredValue = "" }) {
|
autoCorrectEnabled = false
|
||||||
Icon(
|
),
|
||||||
Icons.Filled.Clear,
|
trailingIcon = {
|
||||||
contentDescription = stringResource(R.string.preference_text_clear)
|
if (enteredValue.isNotEmpty()) {
|
||||||
)
|
IconButton(onClick = { enteredValue = "" }) {
|
||||||
|
Icon(
|
||||||
|
Icons.Filled.Clear,
|
||||||
|
contentDescription = stringResource(R.string.preference_text_clear)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
)
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
confirmButton = {
|
confirmButton = {
|
||||||
TextButton(onClick = { onSelect(enteredValue) }) {
|
TextButton(onClick = { onSelect(enteredValue) }) {
|
||||||
@@ -305,34 +312,29 @@ fun RangeDialog(
|
|||||||
mutableFloatStateOf(initialValue / scale.toFloat())
|
mutableFloatStateOf(initialValue / scale.toFloat())
|
||||||
}
|
}
|
||||||
|
|
||||||
AlertDialog(
|
BaseDialog(
|
||||||
onDismissRequest = { onDismissRequest() },
|
onDismissRequest = { onDismissRequest() },
|
||||||
title = {
|
title = title,
|
||||||
Text(title)
|
content = {
|
||||||
},
|
Row(
|
||||||
text = {
|
horizontalArrangement = Arrangement.spacedBy(16.dp),
|
||||||
Column {
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
Row(
|
modifier = Modifier.padding(horizontal = 16.dp, vertical = 4.dp)
|
||||||
horizontalArrangement = Arrangement.spacedBy(16.dp),
|
) {
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
val precision = log10(scale.toFloat()).roundToInt()
|
||||||
modifier = Modifier.weight(1.0f, false)
|
|
||||||
) {
|
|
||||||
val precision = log10(scale.toFloat()).roundToInt()
|
|
||||||
|
|
||||||
Slider(
|
Slider(
|
||||||
value = enteredValue,
|
value = enteredValue,
|
||||||
onValueChange = { enteredValue = it },
|
onValueChange = { enteredValue = it },
|
||||||
valueRange = (range.first.toFloat()/scale)..(range.last.toFloat()/scale),
|
valueRange = (range.first.toFloat()/scale)..(range.last.toFloat()/scale),
|
||||||
steps = ((range.last - range.first) - 1) / step,
|
steps = ((range.last - range.first) - 1) / step,
|
||||||
modifier = Modifier.weight(1.0f)
|
modifier = Modifier.weight(1.0f)
|
||||||
)
|
)
|
||||||
|
|
||||||
Text("%.${precision}f$labelSuffix".format(enteredValue))
|
|
||||||
}
|
|
||||||
|
|
||||||
children()
|
|
||||||
|
|
||||||
|
Text("%.${precision}f$labelSuffix".format(enteredValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
children()
|
||||||
},
|
},
|
||||||
confirmButton = {
|
confirmButton = {
|
||||||
TextButton(onClick = { onSelect((enteredValue * scale).roundToInt()) }) {
|
TextButton(onClick = { onSelect((enteredValue * scale).roundToInt()) }) {
|
||||||
@@ -367,16 +369,16 @@ fun FrameRateDialog(
|
|||||||
val maximumReached = currentValue != null && currentValue > maxFrameRate
|
val maximumReached = currentValue != null && currentValue > maxFrameRate
|
||||||
val minimumReached = currentValue != null && currentValue < minFrameRate
|
val minimumReached = currentValue != null && currentValue < minFrameRate
|
||||||
|
|
||||||
AlertDialog(
|
BaseDialog(
|
||||||
onDismissRequest = { onDismissRequest() },
|
onDismissRequest = { onDismissRequest() },
|
||||||
title = {
|
title = title,
|
||||||
Text(title)
|
content = {
|
||||||
},
|
Column(
|
||||||
text = {
|
modifier = Modifier
|
||||||
Column {
|
.padding(horizontal = 12.dp)
|
||||||
|
) {
|
||||||
Row(
|
Row(
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
modifier = Modifier.weight(1.0f, false)
|
|
||||||
) {
|
) {
|
||||||
IconButton(onClick = {
|
IconButton(onClick = {
|
||||||
val prevValue = max((currentValue ?: 0) - 5, minFrameRate)
|
val prevValue = max((currentValue ?: 0) - 5, minFrameRate)
|
||||||
@@ -629,32 +631,30 @@ fun ProfileCreateDialog(onDismissRequest: () -> Unit) {
|
|||||||
|
|
||||||
val isDuplicate = currentProfiles.contains(filename)
|
val isDuplicate = currentProfiles.contains(filename)
|
||||||
|
|
||||||
AlertDialog(
|
BaseDialog(
|
||||||
onDismissRequest = { onDismissRequest() },
|
onDismissRequest = { onDismissRequest() },
|
||||||
title = {
|
title = stringResource(R.string.preference_profiles_create),
|
||||||
Text(stringResource(R.string.preference_profiles_create))
|
content = {
|
||||||
},
|
Column(
|
||||||
text = {
|
modifier = Modifier
|
||||||
Column {
|
.fillMaxWidth()
|
||||||
Row(
|
.padding(horizontal = 12.dp)
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
) {
|
||||||
modifier = Modifier.weight(1.0f, false)
|
OutlinedTextField(
|
||||||
) {
|
value = enteredValue,
|
||||||
OutlinedTextField(
|
onValueChange = {
|
||||||
value = enteredValue,
|
enteredValue = it
|
||||||
onValueChange = {
|
},
|
||||||
enteredValue = it
|
singleLine = true,
|
||||||
},
|
keyboardOptions = KeyboardOptions(
|
||||||
singleLine = true,
|
keyboardType = KeyboardType.Text,
|
||||||
keyboardOptions = KeyboardOptions(
|
),
|
||||||
keyboardType = KeyboardType.Text,
|
label = {
|
||||||
),
|
Text(stringResource(R.string.preference_profiles_create_name))
|
||||||
label = {
|
},
|
||||||
Text(stringResource(R.string.preference_profiles_create_name))
|
isError = isDuplicate,
|
||||||
},
|
modifier = Modifier.align(Alignment.CenterHorizontally)
|
||||||
isError = isDuplicate
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isDuplicate) {
|
if (isDuplicate) {
|
||||||
Spacer(Modifier.size(8.dp))
|
Spacer(Modifier.size(8.dp))
|
||||||
@@ -806,18 +806,19 @@ fun ProfileSelectCard() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun <T> SelectDialog(
|
fun BaseDialog(
|
||||||
title: String,
|
title: String,
|
||||||
onDismissRequest: () -> Unit,
|
onDismissRequest: () -> Unit,
|
||||||
onSelect: (T) -> Unit,
|
dismissButton: @Composable () -> Unit,
|
||||||
initialValue: T,
|
confirmButton: @Composable () -> Unit,
|
||||||
options: @Composable () -> Unit,
|
content: @Composable (ColumnScope.() -> Unit),
|
||||||
) {
|
) {
|
||||||
val (selectedValue, setSelectedValue) = remember { mutableStateOf(initialValue) }
|
|
||||||
|
|
||||||
Dialog(onDismissRequest = onDismissRequest) {
|
Dialog(onDismissRequest = onDismissRequest) {
|
||||||
Card(
|
Card(
|
||||||
shape = RoundedCornerShape(16.dp)
|
shape = RoundedCornerShape(16.dp),
|
||||||
|
colors = CardDefaults.cardColors(
|
||||||
|
containerColor = MaterialTheme.colorScheme.surfaceContainerHigh
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
// styling a dialog is actually a little hard if you're doing what i'm doing
|
// styling a dialog is actually a little hard if you're doing what i'm doing
|
||||||
// maybe there's a better way to make these padding values...
|
// maybe there's a better way to make these padding values...
|
||||||
@@ -832,15 +833,7 @@ fun <T> SelectDialog(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
CompositionLocalProvider(
|
content()
|
||||||
LocalSelectValue provides (selectedValue as Any),
|
|
||||||
LocalSelectSetValue provides ({
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
setSelectedValue(it as T)
|
|
||||||
})
|
|
||||||
) {
|
|
||||||
options()
|
|
||||||
}
|
|
||||||
|
|
||||||
Row(
|
Row(
|
||||||
horizontalArrangement = Arrangement.End,
|
horizontalArrangement = Arrangement.End,
|
||||||
@@ -852,19 +845,50 @@ fun <T> SelectDialog(
|
|||||||
top = 4.dp
|
top = 4.dp
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
TextButton(onClick = onDismissRequest) {
|
dismissButton()
|
||||||
Text(stringResource(R.string.message_box_cancel))
|
confirmButton()
|
||||||
}
|
|
||||||
|
|
||||||
TextButton(onClick = { onSelect(selectedValue) }) {
|
|
||||||
Text(stringResource(R.string.message_box_accept))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun <T> SelectDialog(
|
||||||
|
title: String,
|
||||||
|
onDismissRequest: () -> Unit,
|
||||||
|
onSelect: (T) -> Unit,
|
||||||
|
initialValue: T,
|
||||||
|
options: @Composable () -> Unit,
|
||||||
|
) {
|
||||||
|
val (selectedValue, setSelectedValue) = remember { mutableStateOf(initialValue) }
|
||||||
|
|
||||||
|
BaseDialog(
|
||||||
|
title = title,
|
||||||
|
onDismissRequest = onDismissRequest,
|
||||||
|
dismissButton = {
|
||||||
|
TextButton(onClick = onDismissRequest) {
|
||||||
|
Text(stringResource(R.string.message_box_cancel))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
confirmButton = {
|
||||||
|
TextButton(onClick = { onSelect(selectedValue) }) {
|
||||||
|
Text(stringResource(R.string.message_box_accept))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
CompositionLocalProvider(
|
||||||
|
LocalSelectValue provides (selectedValue as Any),
|
||||||
|
LocalSelectSetValue provides ({
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
setSelectedValue(it as T)
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
options()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun OptionsButton(title: String, description: String? = null, icon: (@Composable () -> Unit)? = null, displayInline: Boolean = false, onClick: () -> Unit) {
|
fun OptionsButton(title: String, description: String? = null, icon: (@Composable () -> Unit)? = null, displayInline: Boolean = false, onClick: () -> Unit) {
|
||||||
OptionsCard(
|
OptionsCard(
|
||||||
|
|||||||
Reference in new issue
Block a user