Casi finalizando el primer ejercicio
This commit is contained in:
parent
0ec210ac45
commit
cf96628dfb
|
|
@ -1,11 +1,16 @@
|
|||
package ies.teis.genol_salatiel_ex2tdist.ui.exerciseOne
|
||||
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.*
|
||||
import androidx.compose.runtime.*
|
||||
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.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
|
|
@ -24,19 +29,36 @@ fun ExerciseOneCompose() {
|
|||
})
|
||||
}) {
|
||||
if (!viewModel.showLazy) {
|
||||
InputCount(Modifier.padding(paddingValues = it))
|
||||
InputCount(
|
||||
modifier = Modifier.padding(paddingValues = it),
|
||||
onClick = { returnValue ->
|
||||
viewModel.setNumCounters(value = returnValue)
|
||||
}
|
||||
)
|
||||
} else {
|
||||
ButtonsCounter()
|
||||
LazyCounters(
|
||||
viewModel.counters,
|
||||
onIncrement = {
|
||||
viewModel.increment(it)
|
||||
},
|
||||
onDecrement = {
|
||||
viewModel.decrement(it)
|
||||
},
|
||||
onBack = { viewModel.changeShowLazy() }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun InputCount(modifier: Modifier = Modifier) {
|
||||
var inputState by remember { mutableStateOf("") }
|
||||
fun InputCount(
|
||||
modifier: Modifier = Modifier,
|
||||
onClick: (Int) -> Unit
|
||||
) {
|
||||
var inputState by rememberSaveable { mutableStateOf("") }
|
||||
|
||||
Column(
|
||||
Modifier
|
||||
modifier
|
||||
.padding(top = 45.dp)
|
||||
.fillMaxSize(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
|
|
@ -44,10 +66,10 @@ fun InputCount(modifier: Modifier = Modifier) {
|
|||
OutlinedTextField(
|
||||
value = inputState,
|
||||
onValueChange = { inputState = it },
|
||||
placeholder = { Text(text = "Número de contadores:")},
|
||||
placeholder = { Text(text = "Número de contadores:") },
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
|
||||
)
|
||||
Button(onClick = { /*TODO*/ }) {
|
||||
Button(onClick = { onClick(inputState.toInt()) }) {
|
||||
Text(text = "Mostrar")
|
||||
}
|
||||
}
|
||||
|
|
@ -55,6 +77,49 @@ fun InputCount(modifier: Modifier = Modifier) {
|
|||
}
|
||||
|
||||
@Composable
|
||||
fun ButtonsCounter() {
|
||||
fun LazyCounters(
|
||||
counters: List<CounterNumber>,
|
||||
onIncrement: (CounterNumber) -> Unit,
|
||||
onDecrement: (CounterNumber) -> Unit,
|
||||
onBack: () -> Unit
|
||||
) {
|
||||
BackHandler(onBack = onBack)
|
||||
LazyColumn(
|
||||
Modifier
|
||||
.padding(top = 45.dp)
|
||||
.fillMaxSize(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
items(counters) {
|
||||
ButtonCounter(
|
||||
valor = it.num,
|
||||
onIncrement = { onIncrement(it) },
|
||||
onDecrement = { onDecrement(it) }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ButtonCounter(
|
||||
modifier: Modifier = Modifier,
|
||||
valor: Int,
|
||||
onIncrement: () -> Unit,
|
||||
onDecrement: () -> Unit,
|
||||
) {
|
||||
Row(
|
||||
modifier,
|
||||
horizontalArrangement = Arrangement.spacedBy(15.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Button(onClick = onIncrement) {
|
||||
Text(text = "Incrementar")
|
||||
}
|
||||
|
||||
Text(text = valor.toString())
|
||||
|
||||
Button(onClick = onDecrement) {
|
||||
Text(text = "Decrementar")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +1,47 @@
|
|||
package ies.teis.genol_salatiel_ex2tdist.ui.exerciseOne
|
||||
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.ViewModel
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
data class CounterNumber(val id: Int, var num: Int)
|
||||
class ExerciseOneViewModel: ViewModel() {
|
||||
private val _counters = mutableStateListOf<CounterNumber>()
|
||||
val counters: List<CounterNumber>
|
||||
get() = _counters
|
||||
|
||||
private val idAutoincrement: AtomicInteger = AtomicInteger()
|
||||
|
||||
var showLazy by mutableStateOf(false)
|
||||
private set
|
||||
|
||||
fun onChangeShowLazy(){
|
||||
var numberOfCounters by mutableStateOf(0)
|
||||
private set
|
||||
|
||||
fun setNumCounters(value: Int){
|
||||
for (i in 0 .. numberOfCounters){
|
||||
_counters.add(element = CounterNumber(
|
||||
id = idAutoincrement.getAndIncrement(),
|
||||
num = 0
|
||||
))
|
||||
}
|
||||
/*numberOfCounters = value*/
|
||||
|
||||
changeShowLazy()
|
||||
}
|
||||
|
||||
fun changeShowLazy(){
|
||||
showLazy = !showLazy
|
||||
}
|
||||
|
||||
fun increment(element: CounterNumber){
|
||||
element.num++
|
||||
}
|
||||
|
||||
fun decrement(element: CounterNumber){
|
||||
element.num--
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue