Skip to content

Commit

Permalink
feat: createNumericalQuestionCard (#34)
Browse files Browse the repository at this point in the history
* fix: createNumericalQuestionCard

* chore: adding textfield, rememberSaveable, keyboardOptions, toFloat and remove toFloat or null and BasicTextField

* chore: adding new string invalid_numerical_input_value and trade card's colors

* chore: using materialTheme
  • Loading branch information
Francisco-Paulino-Arruda-Filho authored Dec 1, 2024
1 parent 266ee33 commit caa1077
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package io.github.sustainow.presentation.ui.components

import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.HorizontalDivider
import androidx.compose.ui.unit.dp
import androidx.compose.ui.platform.LocalContext
import io.github.sustainow.domain.model.Question
import io.github.sustainow.domain.model.QuestionAlternative
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.tooling.preview.Preview
import io.github.sustainow.presentation.theme.AppTheme
import kotlin.time.Duration
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import io.github.sustainow.R

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NumericalSelectQuestionCard(
question: Question.Numerical,
onAlternativeSelected: (QuestionAlternative) -> Unit,
) {
var textFieldValue by rememberSaveable { mutableStateOf("") }
var isError by rememberSaveable { mutableStateOf(false) }
var errorMessage by rememberSaveable { mutableStateOf("") }
val context = LocalContext.current

Card(
modifier = Modifier
.width(360.dp)
.padding(vertical = 10.dp)
.shadow(
elevation = 8.dp,
shape = RoundedCornerShape(0.dp),
)
.padding(horizontal = 10.dp),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.onSurface, // Cor do fundo do Card
),
) {
Column(
modifier = Modifier
.padding(10.dp)
.fillMaxWidth()
.wrapContentHeight(),
) {
// Cabeçalho com nome e texto da questão
Text(
text = question.name,
style = MaterialTheme.typography.titleMedium.copy(MaterialTheme.colorScheme.scrim),
modifier = Modifier.padding(bottom = 4.dp),
)
Text(
text = question.text,
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.scrim),
modifier = Modifier.padding(bottom = 8.dp)
)

HorizontalDivider(
modifier = Modifier.padding(bottom = 10.dp),
thickness = 1.dp,
color = MaterialTheme.colorScheme.scrim.copy(alpha = 0.5f),
)

// Campo de texto numérico
TextField(
value = textFieldValue,
onValueChange = { newValue ->
textFieldValue = newValue
try {
val floatValue = newValue.toFloat()
isError = false
errorMessage = ""
val alternative = question.alternatives.firstOrNull()
if (alternative != null) {
onAlternativeSelected(
alternative.copy(value = floatValue)
)
}
} catch (e: NumberFormatException) {
isError = true
errorMessage = context.getString(R.string.invalid_numerial_input_value)
}
},
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 10.dp),
colors = TextFieldDefaults.colors(
focusedContainerColor = MaterialTheme.colorScheme.surfaceContainerLow,
unfocusedContainerColor = MaterialTheme.colorScheme.surfaceContainerLow,
),
textStyle = MaterialTheme.typography.bodyLarge.copy(MaterialTheme.colorScheme.scrim),
singleLine = true,
isError = isError,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
supportingText = {
if (isError) {
Text(
text = errorMessage,
style = MaterialTheme.typography.bodySmall.copy(color = MaterialTheme.colorScheme.error),
)
}
}
)
}
}
}


@Composable
@Preview
fun NumericalSelectQuestionCardPreview() {
val question = Question.Numerical(
area = "carbon",
name = "Plastic Bags Usage",
text = "How many plastic bags do you use in a week?",
alternatives = listOf(
QuestionAlternative(
"carbon",
text = "Number of bags",
value = 0f,
timePeriod = Duration.ZERO,
unit = "bags"
)
),
dependencies = emptyList()
)
AppTheme {
NumericalSelectQuestionCard(
question = question,
onAlternativeSelected = {
Log.i("Numerical Question Preview", "Selected value: ${it.value}")
}
)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@
<string name="consume_route_text">Consumo</string>
<string name="colective_actions_route_text">Ações</string>
<string name="routines_route_text">Rotina</string>
<string name="invalid_numerial_input_value">Por favor, insira um número válido</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@
<string name="consume_route_text">Consumption</string>
<string name="colective_actions_route_text">Actions</string>
<string name="routines_route_text">Routine</string>
<string name="invalid_numerial_input_value">Please enter a valid number</string>
</resources>

0 comments on commit caa1077

Please sign in to comment.