Terminado ejercicio 1

This commit is contained in:
Salatiel Genol Rodríguez 2023-03-24 17:47:16 +01:00
parent cf96628dfb
commit 49aef16922
2 changed files with 48 additions and 24 deletions

View File

@ -1,11 +1,15 @@
package ies.teis.genol_salatiel_ex2tdist.ui.exerciseOne
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
@ -18,17 +22,33 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
@Preview
@Composable
fun ExerciseOneCompose() {
val viewModel: ExerciseOneViewModel = viewModel()
Scaffold(topBar = {
TopAppBar(title = {
Text(text = "Contadores", modifier = Modifier.padding(horizontal = 10.dp))
Row() {
Text(
text = "Contadores",
modifier = Modifier
.padding(horizontal = 10.dp)
.weight(1f)
)
if (viewModel.counters.isNotEmpty()) {
Icon(
Icons.Default.Refresh,
contentDescription = "Reload the app",
modifier = Modifier
.padding(horizontal = 15.dp)
.clickable { viewModel.resetAll() }
)
}
}
})
}) {
if (!viewModel.showLazy) {
}) { it ->
if (viewModel.counters.isEmpty()) {
InputCount(
modifier = Modifier.padding(paddingValues = it),
onClick = { returnValue ->
@ -38,13 +58,13 @@ fun ExerciseOneCompose() {
} else {
LazyCounters(
viewModel.counters,
onIncrement = {
viewModel.increment(it)
onIncrement = { CounterNumber ->
viewModel.increment(CounterNumber)
},
onDecrement = {
viewModel.decrement(it)
onDecrement = { CounterNumber ->
viewModel.decrement(CounterNumber)
},
onBack = { viewModel.changeShowLazy() }
onBack = { viewModel.resetAll() }
)
}
}
@ -78,21 +98,24 @@ fun InputCount(
@Composable
fun LazyCounters(
counters: List<CounterNumber>,
counter: List<CounterNumber>,
onIncrement: (CounterNumber) -> Unit,
onDecrement: (CounterNumber) -> Unit,
onBack: () -> Unit
) {
val listState = rememberLazyListState()
BackHandler(onBack = onBack)
LazyColumn(
Modifier
.padding(top = 45.dp)
.fillMaxSize(),
state = listState,
horizontalAlignment = Alignment.CenterHorizontally
) {
items(counters) {
items(counter) {
ButtonCounter(
valor = it.num,
valor = it.numState,
onIncrement = { onIncrement(it) },
onDecrement = { onDecrement(it) }
)

View File

@ -7,7 +7,10 @@ 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 CounterNumber(val id: Int, var num: Int){
var numState by mutableStateOf(num)
}
class ExerciseOneViewModel: ViewModel() {
private val _counters = mutableStateListOf<CounterNumber>()
val counters: List<CounterNumber>
@ -18,30 +21,28 @@ class ExerciseOneViewModel: ViewModel() {
var showLazy by mutableStateOf(false)
private set
var numberOfCounters by mutableStateOf(0)
private set
fun setNumCounters(value: Int){
for (i in 0 .. numberOfCounters){
for (i in 1 .. value){
_counters.add(element = CounterNumber(
id = idAutoincrement.getAndIncrement(),
num = 0
))
}
/*numberOfCounters = value*/
changeShowLazy()
/* showLazy = !showLazy*/
}
fun changeShowLazy(){
showLazy = !showLazy
fun resetAll(){
_counters.removeAll(counters)
/*showLazy = !showLazy*/
}
fun increment(element: CounterNumber){
element.num++
element.numState++
}
fun decrement(element: CounterNumber){
element.num--
if(element.numState > 0){
element.numState--
}
}
}