-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide three options for donation reminder time (#1256)
* Provide three options for donation reminder time * Move radio buttons to top card; Change strings * Use padding instead of height for radio elements
- Loading branch information
Showing
3 changed files
with
142 additions
and
40 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
app/src/main/kotlin/at/bitfire/davdroid/ui/composable/RadioButtons.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details. | ||
*/ | ||
|
||
package at.bitfire.davdroid.ui.composable | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.selection.selectable | ||
import androidx.compose.foundation.selection.selectableGroup | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.RadioButton | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.semantics.Role | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import at.bitfire.davdroid.ui.AppTheme | ||
|
||
@Composable | ||
fun RadioButtons( | ||
radioOptions: List<String> = listOf<String>(), | ||
onOptionSelected: (String) -> Unit = {}, | ||
textPaddingPerOption: PaddingValues = PaddingValues(10.dp), | ||
modifier: Modifier = Modifier | ||
) { | ||
val (selectedOption, setSelectedOption) = remember { mutableStateOf(radioOptions[0]) } | ||
Column( | ||
// Modifier.selectableGroup() is essential to ensure correct accessibility behavior | ||
modifier = modifier.selectableGroup() | ||
) { | ||
radioOptions.forEach { text -> | ||
Row( | ||
Modifier | ||
.fillMaxWidth() | ||
.selectable( | ||
selected = (text == selectedOption), | ||
onClick = { | ||
setSelectedOption(text) | ||
onOptionSelected(text) | ||
}, | ||
role = Role.Companion.RadioButton | ||
) | ||
.padding(horizontal = 16.dp), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
RadioButton( | ||
selected = (text == selectedOption), | ||
onClick = null, // null recommended for accessibility with screen readers | ||
) | ||
Text( | ||
text = text, | ||
style = MaterialTheme.typography.bodyLarge, | ||
modifier = Modifier.padding(textPaddingPerOption) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
private fun RadioButtonsPreview() { | ||
AppTheme { | ||
RadioButtons( | ||
radioOptions = listOf( | ||
"Option 1", | ||
"Option 2 is the longest of all the options, so we can see whether line breaks are not a problem.", | ||
"Option 3") | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters