Compare commits
6 Commits
35a4bb1547
...
c89b2083df
| Author | SHA1 | Date |
|---|---|---|
|
|
c89b2083df | |
|
|
4b1d224620 | |
|
|
49aef16922 | |
|
|
cf96628dfb | |
|
|
0ec210ac45 | |
|
|
42e2855620 |
|
|
@ -49,12 +49,12 @@ android {
|
|||
dependencies {
|
||||
|
||||
implementation 'androidx.core:core-ktx:1.9.0'
|
||||
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.0'
|
||||
implementation 'androidx.activity:activity-compose:1.6.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
|
||||
implementation 'androidx.activity:activity-compose:1.7.0'
|
||||
implementation "androidx.compose.ui:ui:$compose_ui_version"
|
||||
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
|
||||
implementation 'androidx.compose.material:material:1.3.1'
|
||||
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.0"
|
||||
implementation 'androidx.compose.material:material:1.4.0'
|
||||
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1"
|
||||
implementation "androidx.navigation:navigation-compose:2.5.3"
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||
|
|
|
|||
|
|
@ -1,9 +1,149 @@
|
|||
package ies.teis.genol_salatiel_ex2tdist.ui.exerciseOne
|
||||
|
||||
import androidx.compose.material.Text
|
||||
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
|
||||
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.res.stringResource
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import ies.teis.genol_salatiel_ex2tdist.R
|
||||
|
||||
@Composable
|
||||
fun ExerciseOneCompose(){
|
||||
Text(text = "1")
|
||||
fun ExerciseOneCompose() {
|
||||
val viewModel: ExerciseOneViewModel = viewModel()
|
||||
|
||||
Scaffold(topBar = {
|
||||
TopAppBar(title = {
|
||||
Row() {
|
||||
Text(
|
||||
text = stringResource(R.string.ej1_contadores),
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 10.dp)
|
||||
.weight(1f)
|
||||
)
|
||||
if (viewModel.counters.isNotEmpty()) {
|
||||
Icon(
|
||||
Icons.Default.Refresh,
|
||||
contentDescription = stringResource(R.string.ej1_reload),
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 15.dp)
|
||||
.clickable { viewModel.resetAll() }
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}) { it ->
|
||||
if (viewModel.counters.isEmpty()) {
|
||||
InputCount(
|
||||
modifier = Modifier.padding(paddingValues = it),
|
||||
onClick = { returnValue ->
|
||||
viewModel.setNumCounters(value = returnValue)
|
||||
}
|
||||
)
|
||||
} else {
|
||||
LazyCounters(
|
||||
viewModel.counters,
|
||||
onIncrement = { CounterNumber ->
|
||||
viewModel.increment(CounterNumber)
|
||||
},
|
||||
onDecrement = { CounterNumber ->
|
||||
viewModel.decrement(CounterNumber)
|
||||
},
|
||||
onBack = { viewModel.resetAll() }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun InputCount(
|
||||
modifier: Modifier = Modifier,
|
||||
onClick: (Int) -> Unit
|
||||
) {
|
||||
var inputState by rememberSaveable { mutableStateOf("") }
|
||||
|
||||
Column(
|
||||
modifier
|
||||
.padding(top = 45.dp)
|
||||
.fillMaxSize(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
OutlinedTextField(
|
||||
value = inputState,
|
||||
onValueChange = { inputState = it },
|
||||
placeholder = { Text(text = stringResource(R.string.ej1_num_contadores)) },
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
|
||||
)
|
||||
Button(onClick = { onClick(inputState.toInt()) }) {
|
||||
Text(text = stringResource(R.string.ej1_mostrar))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun LazyCounters(
|
||||
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(counter) {
|
||||
ButtonCounter(
|
||||
valor = it.numState,
|
||||
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 = stringResource(R.string.ej1_incrementar))
|
||||
}
|
||||
|
||||
Text(text = valor.toString())
|
||||
|
||||
Button(onClick = onDecrement) {
|
||||
Text(text = stringResource(R.string.ej1_decrementar))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
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.setValue
|
||||
import androidx.lifecycle.ViewModel
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
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>
|
||||
get() = _counters
|
||||
|
||||
private val idAutoincrement: AtomicInteger = AtomicInteger()
|
||||
|
||||
|
||||
fun setNumCounters(value: Int){
|
||||
for (i in 1 .. value){
|
||||
_counters.add(element = CounterNumber(
|
||||
id = idAutoincrement.getAndIncrement(),
|
||||
num = 0
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fun resetAll(){
|
||||
_counters.removeAll(counters)
|
||||
}
|
||||
|
||||
fun increment(element: CounterNumber){
|
||||
element.numState++
|
||||
}
|
||||
|
||||
fun decrement(element: CounterNumber){
|
||||
if(element.numState > 0){
|
||||
element.numState--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,103 @@
|
|||
package ies.teis.genol_salatiel_ex2tdist.ui.exerciseTwo
|
||||
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.foundation.background
|
||||
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.material.*
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
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.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
|
||||
@Composable
|
||||
fun ExerciseTwoComposable(){
|
||||
Text(text = "2")
|
||||
fun ExerciseTwoComposable() {
|
||||
|
||||
val viewModel: ExerciseTwoViewModel = viewModel()
|
||||
var inputState by rememberSaveable { mutableStateOf("") }
|
||||
val listState = rememberLazyListState()
|
||||
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(title = {
|
||||
Row() {
|
||||
Text(
|
||||
text = "Contadores",
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 10.dp)
|
||||
.weight(1f)
|
||||
)
|
||||
Text(text = "Global: ")
|
||||
Text(
|
||||
text = viewModel.counters.sumOf {
|
||||
it.numCounter
|
||||
}.toString(),
|
||||
modifier = Modifier.padding(end = 10.dp)
|
||||
)
|
||||
}
|
||||
})
|
||||
}) { paddingValues ->
|
||||
|
||||
Column(Modifier.padding(paddingValues)) {
|
||||
Row(
|
||||
Modifier
|
||||
.padding(top = 15.dp)
|
||||
.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceEvenly,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
TextField(value = inputState, onValueChange = { inputState = it })
|
||||
Button(onClick = { viewModel.addCounter(inputState) }) {
|
||||
Text(text = "Añadir")
|
||||
}
|
||||
}
|
||||
LazyColumn(state = listState) {
|
||||
items(viewModel.counters) { counterModel ->
|
||||
Card(
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(10.dp)
|
||||
) {
|
||||
Column(Modifier.background(MaterialTheme.colors.secondary)) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Text(
|
||||
text = counterModel.counterName,
|
||||
Modifier
|
||||
.weight(1f)
|
||||
.padding(start = 16.dp)
|
||||
)
|
||||
|
||||
Text(text = counterModel.numCounter.toString())
|
||||
|
||||
IconButton(onClick = { viewModel.eliminar(counterModel) }) {
|
||||
Icon(Icons.Filled.Close, contentDescription = "Close")
|
||||
}
|
||||
}
|
||||
Row(
|
||||
Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceEvenly
|
||||
) {
|
||||
Text(text = "Sumar", modifier = Modifier.clickable { viewModel.sumar(element = counterModel) })
|
||||
Text(text = "Restar", modifier = Modifier.clickable { viewModel.restar(element = counterModel)})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -37,6 +37,7 @@ class LauncherActivity : ComponentActivity() {
|
|||
modifier = Modifier.fillMaxSize(),
|
||||
color = MaterialTheme.colors.background
|
||||
) {
|
||||
/* Los colores de los botones cambian intencionadamente */
|
||||
LauncherNavController()
|
||||
}
|
||||
}
|
||||
|
|
@ -81,14 +82,14 @@ fun ExerciseList(navController: NavController) {
|
|||
label = stringResource(R.string.launcher_button_two),
|
||||
{ navController.navigate(route = LauncherModel.ExerciseTwo.path) }
|
||||
)
|
||||
LauncherButton(
|
||||
/* LauncherButton(
|
||||
label = stringResource(R.string.launcher_button_three),
|
||||
{ navController.navigate(route = LauncherModel.ExerciseThree.path) }
|
||||
)
|
||||
LauncherButton(
|
||||
label = stringResource(R.string.launcher_button_four),
|
||||
{ navController.navigate(route = LauncherModel.ExerciseFour.path) }
|
||||
)
|
||||
)*/
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,4 +5,10 @@
|
|||
<string name="launcher_button_two">Exercise Two</string>
|
||||
<string name="launcher_button_three">Exercise Three</string>
|
||||
<string name="launcher_button_four">Exercise Four</string>
|
||||
<string name="ej1_contadores">Contadores</string>
|
||||
<string name="ej1_reload">Reload the app</string>
|
||||
<string name="ej1_num_contadores">Número de contadores:</string>
|
||||
<string name="ej1_mostrar">Mostrar</string>
|
||||
<string name="ej1_incrementar">Incrementar</string>
|
||||
<string name="ej1_decrementar">Decrementar</string>
|
||||
</resources>
|
||||
Loading…
Reference in New Issue