Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/src/main/java/es/fpsumma/dam2/intro/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import com.ivancorrales.basicjetpackcompose.ui.theme.IntroTheme
import es.fpsumma.dam2.intro.ui.screens.CounterScreen
import es.fpsumma.dam2.intro.ui.screens.InputDemoScreen

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -19,7 +20,7 @@ class MainActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
CounterScreen()
InputDemoScreen()
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/es/fpsumma/dam2/intro/ui/components/NameField.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package es.fpsumma.dam2.intro.ui.components

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.ImeAction

@Composable
fun NameField(
value: String,
onValueChange: (String) -> Unit,
error: String?, // Estado del error a mostrar
onDone: () -> Unit, // Callback para el evento de teclado (tecla 'Done')
modifier: Modifier = Modifier
) {
OutlinedTextField(
value = value,
onValueChange = onValueChange,
label = { Text("Tu nombre") }, // Usar stringResource()
isError = error != null,
supportingText = {
if (error != null) {
// Muestra el mensaje de error con el color de error
Text(error!!, color = MaterialTheme.colorScheme.error)
}
},
singleLine = true,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
// Ejecuta el callback onDone cuando se presiona la acción de teclado 'Done'
keyboardActions = KeyboardActions(onDone = { onDone() }),
modifier = modifier.fillMaxWidth()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,56 @@ import androidx.compose.material.icons.outlined.Refresh

import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import es.fpsumma.dam2.intro.R


@Composable
fun CounterScreen() {
var count by remember { mutableStateOf(0) }

var count by rememberSaveable { mutableStateOf(5) }
val sizeBoton= (count*8).dp
Column(
modifier = Modifier
.fillMaxSize()
.padding(24.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Text("Hola mi amig@!", style = MaterialTheme.typography.headlineMedium)
Text(text=stringResource(id= R.string.Good_Morning), style = MaterialTheme.typography.headlineMedium)
Text("Contador: $count", style = MaterialTheme.typography.titleLarge)
Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) {
Button(
onClick = {},
onClick = {count++},

modifier = Modifier.semantics { contentDescription = "Incrementar" }
) { Text("+1") }

FilledTonalButton(
onClick = {},
onClick = {if(count > 0 )count--},
modifier = Modifier.semantics { contentDescription = "Decrementar" }
) { Text("-1") }

IconButton(
onClick = {},
onClick = {count=0},
modifier = Modifier.size(48.dp)
) {
Icon(Icons.Outlined.Refresh, contentDescription = "Reiniciar contador")
}
}
Button(
onClick = {},
enabled = false,
modifier=Modifier.width(sizeBoton)

){}
}
}

Expand All @@ -53,3 +64,4 @@ fun CounterScreen() {
private fun CounterPreview() {
CounterScreen()
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import es.fpsumma.dam2.intro.ui.components.NameField

@Composable
fun InputDemoScreen() {
Expand All @@ -19,43 +20,57 @@ fun InputDemoScreen() {
var name by rememberSaveable { mutableStateOf("") }
var greeting by remember { mutableStateOf<String?>(null) }

// Calcula el error en función del texto (sin estado extra) Pista -> derivedStateOf
// TODO: valida name: no vacío, mínimo 3 caracteres (trim).
val nameToGreet = name.trim()

// Lógica de validación (Puntos 2 y 3 implementados)
val error: String? by remember(name) {
derivedStateOf {
null
if (name.isEmpty()) {
"El nombre no puede estar vacío"
} else if (nameToGreet.length < 3) {
"Mínimo 3 caracteres"
} else {
null
}
}
}

val isButtonEnabled = error == null

fun submit() {
if (error == null) {
greeting = "Hola, 👋"
if (isButtonEnabled) {
greeting = "Hola, $nameToGreet 👋"
focusManager.clearFocus()
}
}

Column(Modifier.fillMaxSize().padding(24.dp)) {
OutlinedTextField(

// ------------------------------------------------------------------

NameField(
value = name,
onValueChange = { name = it },
label = { Text("Tu nombre") },
isError = error != null,
supportingText = { if (error != null) Text(error!!) },
singleLine = true,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(onDone = { submit() }),
modifier = Modifier.fillMaxWidth()
onValueChange = {
name = it // Recibe el nuevo valor y actualiza el estado
greeting = null // Reinicia el saludo
},
error = error, // Pasa el estado del error
onDone = ::submit // Pasa la referencia a la función submit
)
// ------------------------------------------------------------------

Spacer(Modifier.height(16.dp))
Button(
onClick = { submit() },
enabled = isButtonEnabled,
modifier = Modifier.fillMaxWidth()
) {
Text("Saludar")
Text("Saludar") // Usar stringResource()
}
Spacer(Modifier.height(16.dp))
if (error == null && name.isNotBlank()) {
Text(greeting?: "" )

if (greeting != null) {
Text(greeting!!, style = MaterialTheme.typography.headlineSmall)
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/es/fpsumma/dam2/intro/ui/theme/Color.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ivancorrales.basicjetpackcompose.ui.theme
package es.fpsumma.dam2.intro.ui.theme

import androidx.compose.ui.graphics.Color

Expand All @@ -8,4 +8,6 @@ val Pink80 = Color(0xFFEFB8C8)

val Purple40 = Color(0xFF6650a4)
val PurpleGrey40 = Color(0xFF625b71)
val Pink40 = Color(0xFF7D5260)
val Pink40 = Color(0xFF7D5260)

val Green= Color(0xFF1B5E20)
17 changes: 9 additions & 8 deletions app/src/main/java/es/fpsumma/dam2/intro/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.ivancorrales.basicjetpackcompose.ui.theme

import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import com.ivancorrales.basicjetpackcompose.ui.theme.LightColorScheme
import es.fpsumma.dam2.intro.ui.theme.Green
import es.fpsumma.dam2.intro.ui.theme.Pink40
import es.fpsumma.dam2.intro.ui.theme.Pink80
import es.fpsumma.dam2.intro.ui.theme.Purple40
import es.fpsumma.dam2.intro.ui.theme.PurpleGrey40
import es.fpsumma.dam2.intro.ui.theme.PurpleGrey80

private val DarkColorScheme = darkColorScheme(
primary = Purple80,
primary = Green,
secondary = PurpleGrey80,
tertiary = Pink80
)
Expand All @@ -28,8 +29,8 @@ private val LightColorScheme = lightColorScheme(

@Composable
fun IntroTheme(
darkTheme: Boolean = false,
dynamicColor: Boolean = true,
darkTheme: Boolean = true,
dynamicColor: Boolean = false,
content: @Composable () -> Unit
) {
val colorScheme = when {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">Intro</string>
<string name="Good_Morning">Buenos dias</string>
</resources>