|
| 1 | +package com.bitwarden.ui.platform.components.dropdown |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.ColumnScope |
| 4 | +import androidx.compose.foundation.layout.PaddingValues |
| 5 | +import androidx.compose.foundation.layout.RowScope |
| 6 | +import androidx.compose.material3.OutlinedTextField |
| 7 | +import androidx.compose.runtime.Composable |
| 8 | +import androidx.compose.runtime.getValue |
| 9 | +import androidx.compose.runtime.mutableStateOf |
| 10 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 11 | +import androidx.compose.runtime.setValue |
| 12 | +import androidx.compose.ui.Modifier |
| 13 | +import androidx.compose.ui.res.pluralStringResource |
| 14 | +import androidx.compose.ui.res.stringResource |
| 15 | +import androidx.compose.ui.unit.dp |
| 16 | +import com.bitwarden.ui.platform.components.button.BitwardenTextSelectionButton |
| 17 | +import com.bitwarden.ui.platform.components.dialog.BitwardenTimePickerDialog |
| 18 | +import com.bitwarden.ui.platform.components.model.CardStyle |
| 19 | +import com.bitwarden.ui.platform.components.model.TooltipData |
| 20 | +import com.bitwarden.ui.platform.resource.BitwardenPlurals |
| 21 | +import com.bitwarden.ui.platform.resource.BitwardenString |
| 22 | + |
| 23 | +private const val MINUTES_PER_HOUR: Int = 60 |
| 24 | + |
| 25 | +/** |
| 26 | + * A button that displays a selected time duration and opens a time picker dialog when clicked. |
| 27 | + * |
| 28 | + * @param label The descriptive text label for the [OutlinedTextField]. |
| 29 | + * @param totalMinutes The currently selected time value in minutes. |
| 30 | + * @param onTimeSelect A lambda that is invoked when a time is selected from the menu. |
| 31 | + * @param is24Hour Whether or not the time should be displayed in 24-hour format. |
| 32 | + * @param cardStyle Indicates the type of card style to be applied. |
| 33 | + * @param modifier A [Modifier] that you can use to apply custom modifications to the composable. |
| 34 | + * @param isEnabled Whether or not the button is enabled. |
| 35 | + * @param supportingContent An optional supporting content that will appear below the button. |
| 36 | + * @param tooltip A nullable [TooltipData], representing the tooltip icon. |
| 37 | + * @param insets Inner padding to be applied within the card. |
| 38 | + * @param textFieldTestTag The optional test tag associated with the inner text field. |
| 39 | + * @param actionsPadding Padding to be applied to the [actions] block. |
| 40 | + * @param actions A lambda containing the set of actions (usually icons or similar) to display |
| 41 | + * in the app bar's trailing side. This lambda extends [RowScope], allowing flexibility in |
| 42 | + * defining the layout of the actions. |
| 43 | + */ |
| 44 | +@Composable |
| 45 | +fun BitwardenTimePickerButton( |
| 46 | + label: String, |
| 47 | + totalMinutes: Int, |
| 48 | + onTimeSelect: (minutes: Int) -> Unit, |
| 49 | + is24Hour: Boolean, |
| 50 | + cardStyle: CardStyle?, |
| 51 | + modifier: Modifier = Modifier, |
| 52 | + isEnabled: Boolean = true, |
| 53 | + supportingContent: @Composable (ColumnScope.() -> Unit)?, |
| 54 | + tooltip: TooltipData? = null, |
| 55 | + insets: PaddingValues = PaddingValues(), |
| 56 | + textFieldTestTag: String? = null, |
| 57 | + actionsPadding: PaddingValues = PaddingValues(end = 4.dp), |
| 58 | + actions: @Composable RowScope.() -> Unit = {}, |
| 59 | +) { |
| 60 | + BitwardenTimePickerButton( |
| 61 | + label = label, |
| 62 | + hours = totalMinutes / MINUTES_PER_HOUR, |
| 63 | + minutes = totalMinutes.mod(MINUTES_PER_HOUR), |
| 64 | + onTimeSelect = { hour, minute -> onTimeSelect((hour * MINUTES_PER_HOUR) + minute) }, |
| 65 | + cardStyle = cardStyle, |
| 66 | + is24Hour = is24Hour, |
| 67 | + modifier = modifier, |
| 68 | + isEnabled = isEnabled, |
| 69 | + supportingContent = supportingContent, |
| 70 | + tooltip = tooltip, |
| 71 | + insets = insets, |
| 72 | + textFieldTestTag = textFieldTestTag, |
| 73 | + actionsPadding = actionsPadding, |
| 74 | + actions = actions, |
| 75 | + ) |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * A button that displays a selected time duration and opens a time picker dialog when clicked. |
| 80 | + * |
| 81 | + * @param label The descriptive text label for the [OutlinedTextField]. |
| 82 | + * @param hours The currently selected time value in hours. |
| 83 | + * @param minutes The currently selected time value in minutes. |
| 84 | + * @param onTimeSelect A lambda that is invoked when a time is selected from the menu. |
| 85 | + * @param is24Hour Whether or not the time should be displayed in 24-hour format. |
| 86 | + * @param cardStyle Indicates the type of card style to be applied. |
| 87 | + * @param modifier A [Modifier] that you can use to apply custom modifications to the composable. |
| 88 | + * @param isEnabled Whether or not the button is enabled. |
| 89 | + * @param supportingContent An optional supporting content that will appear below the button. |
| 90 | + * @param tooltip A nullable [TooltipData], representing the tooltip icon. |
| 91 | + * @param insets Inner padding to be applied within the card. |
| 92 | + * @param textFieldTestTag The optional test tag associated with the inner text field. |
| 93 | + * @param actionsPadding Padding to be applied to the [actions] block. |
| 94 | + * @param actions A lambda containing the set of actions (usually icons or similar) to display |
| 95 | + * in the app bar's trailing side. This lambda extends [RowScope], allowing flexibility in |
| 96 | + * defining the layout of the actions. |
| 97 | + */ |
| 98 | +@Composable |
| 99 | +fun BitwardenTimePickerButton( |
| 100 | + label: String, |
| 101 | + hours: Int, |
| 102 | + minutes: Int, |
| 103 | + onTimeSelect: (hour: Int, minute: Int) -> Unit, |
| 104 | + is24Hour: Boolean, |
| 105 | + cardStyle: CardStyle?, |
| 106 | + modifier: Modifier = Modifier, |
| 107 | + isEnabled: Boolean = true, |
| 108 | + supportingContent: @Composable (ColumnScope.() -> Unit)?, |
| 109 | + tooltip: TooltipData? = null, |
| 110 | + insets: PaddingValues = PaddingValues(), |
| 111 | + textFieldTestTag: String? = null, |
| 112 | + actionsPadding: PaddingValues = PaddingValues(end = 4.dp), |
| 113 | + actions: @Composable RowScope.() -> Unit = {}, |
| 114 | +) { |
| 115 | + var shouldShowDialog by rememberSaveable { mutableStateOf(value = false) } |
| 116 | + BitwardenTextSelectionButton( |
| 117 | + label = label, |
| 118 | + selectedOption = if (hours != 0 && minutes != 0) { |
| 119 | + // Since both hours and minutes are non-zero, we display both of them. |
| 120 | + stringResource( |
| 121 | + id = BitwardenString.hours_minutes_format, |
| 122 | + formatArgs = arrayOf( |
| 123 | + pluralStringResource( |
| 124 | + id = BitwardenPlurals.hours_format, |
| 125 | + count = hours, |
| 126 | + formatArgs = arrayOf(hours), |
| 127 | + ), |
| 128 | + pluralStringResource( |
| 129 | + id = BitwardenPlurals.minutes_format, |
| 130 | + count = minutes, |
| 131 | + formatArgs = arrayOf(minutes), |
| 132 | + ), |
| 133 | + ), |
| 134 | + ) |
| 135 | + } else if (hours != 0) { |
| 136 | + // Since only hours are non-zero, we only display hours. |
| 137 | + pluralStringResource( |
| 138 | + id = BitwardenPlurals.hours_format, |
| 139 | + count = hours, |
| 140 | + formatArgs = arrayOf(hours), |
| 141 | + ) |
| 142 | + } else { |
| 143 | + // We display this if there are only minutes or if both hours and minutes are 0. |
| 144 | + pluralStringResource( |
| 145 | + id = BitwardenPlurals.minutes_format, |
| 146 | + count = minutes, |
| 147 | + formatArgs = arrayOf(minutes), |
| 148 | + ) |
| 149 | + }, |
| 150 | + onClick = { shouldShowDialog = true }, |
| 151 | + cardStyle = cardStyle, |
| 152 | + enabled = isEnabled, |
| 153 | + showChevron = false, |
| 154 | + supportingContent = supportingContent, |
| 155 | + tooltip = tooltip, |
| 156 | + insets = insets, |
| 157 | + textFieldTestTag = textFieldTestTag, |
| 158 | + actionsPadding = actionsPadding, |
| 159 | + actions = actions, |
| 160 | + modifier = modifier, |
| 161 | + ) |
| 162 | + if (shouldShowDialog) { |
| 163 | + BitwardenTimePickerDialog( |
| 164 | + initialHour = hours, |
| 165 | + initialMinute = minutes, |
| 166 | + onTimeSelect = { hour, minute -> |
| 167 | + onTimeSelect(hour, minute) |
| 168 | + shouldShowDialog = false |
| 169 | + }, |
| 170 | + onDismissRequest = { shouldShowDialog = false }, |
| 171 | + is24Hour = is24Hour, |
| 172 | + ) |
| 173 | + } |
| 174 | +} |
0 commit comments