|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.groundplatform.android.ui.datacollection.components.refactor |
| 17 | + |
| 18 | +import androidx.compose.foundation.layout.Arrangement |
| 19 | +import androidx.compose.foundation.layout.Column |
| 20 | +import androidx.compose.foundation.layout.PaddingValues |
| 21 | +import androidx.compose.foundation.layout.Row |
| 22 | +import androidx.compose.foundation.layout.padding |
| 23 | +import androidx.compose.material3.Button |
| 24 | +import androidx.compose.material3.FilledTonalButton |
| 25 | +import androidx.compose.material3.Icon |
| 26 | +import androidx.compose.material3.OutlinedButton |
| 27 | +import androidx.compose.material3.Text |
| 28 | +import androidx.compose.runtime.Composable |
| 29 | +import androidx.compose.ui.Modifier |
| 30 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 31 | +import androidx.compose.ui.res.stringResource |
| 32 | +import androidx.compose.ui.res.vectorResource |
| 33 | +import androidx.compose.ui.tooling.preview.Preview |
| 34 | +import androidx.compose.ui.unit.dp |
| 35 | +import org.groundplatform.android.ui.common.ExcludeFromJacocoGeneratedReport |
| 36 | +import org.groundplatform.android.ui.datacollection.components.ButtonAction |
| 37 | +import org.groundplatform.android.ui.theme.AppTheme |
| 38 | + |
| 39 | +@Composable |
| 40 | +fun TaskButton( |
| 41 | + modifier: Modifier = Modifier, |
| 42 | + state: ButtonActionState, |
| 43 | + onClick: (ButtonAction) -> Unit, |
| 44 | +) { |
| 45 | + when (state.action.theme) { |
| 46 | + ButtonAction.Theme.DARK_GREEN -> |
| 47 | + Button(modifier = modifier, onClick = { onClick(state.action) }, enabled = state.isEnabled) { |
| 48 | + Content(action = state.action) |
| 49 | + } |
| 50 | + ButtonAction.Theme.LIGHT_GREEN -> |
| 51 | + FilledTonalButton( |
| 52 | + modifier = modifier, |
| 53 | + onClick = { onClick(state.action) }, |
| 54 | + enabled = state.isEnabled, |
| 55 | + ) { |
| 56 | + Content(action = state.action) |
| 57 | + } |
| 58 | + ButtonAction.Theme.OUTLINED -> |
| 59 | + OutlinedButton( |
| 60 | + modifier = modifier, |
| 61 | + onClick = { onClick(state.action) }, |
| 62 | + enabled = state.isEnabled, |
| 63 | + ) { |
| 64 | + Content(action = state.action) |
| 65 | + } |
| 66 | + ButtonAction.Theme.TRANSPARENT -> |
| 67 | + OutlinedButton( |
| 68 | + modifier = modifier, |
| 69 | + border = null, |
| 70 | + onClick = { onClick(state.action) }, |
| 71 | + enabled = state.isEnabled, |
| 72 | + contentPadding = PaddingValues(horizontal = 8.dp, vertical = 4.dp), |
| 73 | + ) { |
| 74 | + Content(action = state.action) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +@Composable |
| 80 | +private fun Content(modifier: Modifier = Modifier, action: ButtonAction) { |
| 81 | + when { |
| 82 | + action.drawableId != null -> { |
| 83 | + Icon( |
| 84 | + modifier = modifier, |
| 85 | + imageVector = ImageVector.vectorResource(id = action.drawableId), |
| 86 | + contentDescription = action.contentDescription?.let { resId -> stringResource(resId) }, |
| 87 | + ) |
| 88 | + } |
| 89 | + action.textId != null -> { |
| 90 | + Text(modifier = modifier, text = stringResource(id = action.textId)) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +@Preview(showBackground = true) |
| 96 | +@Composable |
| 97 | +@ExcludeFromJacocoGeneratedReport |
| 98 | +private fun TaskButtonAllPreview() { |
| 99 | + AppTheme { |
| 100 | + Column(modifier = Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { |
| 101 | + ButtonAction.entries.forEach { action -> |
| 102 | + Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { |
| 103 | + TaskButton(state = ButtonActionState(action), onClick = {}) |
| 104 | + TaskButton(state = ButtonActionState(action), onClick = {}) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments