Añadido ejemplo calculadora
This commit is contained in:
parent
267ed3e06a
commit
d5e2aa3578
|
|
@ -0,0 +1,397 @@
|
||||||
|
package es.genol.genol_salatiel_ex1tdist.ejemplosCodigo
|
||||||
|
|
||||||
|
import android.content.res.Configuration
|
||||||
|
import androidx.compose.foundation.background
|
||||||
|
import androidx.compose.foundation.clickable
|
||||||
|
import androidx.compose.foundation.layout.*
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
|
import androidx.compose.material.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.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.draw.clip
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.platform.LocalConfiguration
|
||||||
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.compose.ui.unit.sp
|
||||||
|
import es.genol.genol_salatiel_ex1tdist.MainActivity
|
||||||
|
import es.genol.genol_salatiel_ex1tdist.ui.theme.OrangeMine
|
||||||
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
|
/*
|
||||||
|
Clase principal del programa, donde se inicializan todos los componentes, y se realizan algunas
|
||||||
|
operaciones simples mediante estados mutables y funciones personalizadas.
|
||||||
|
|
||||||
|
La orientación del dispositivo modifica los margenes para que se vea decentemente en cualquier
|
||||||
|
situación.
|
||||||
|
|
||||||
|
La lógica de programación es muy rudimentaria, es algo que he de mejorar.
|
||||||
|
*/
|
||||||
|
|
||||||
|
val numActualSB = StringBuilder()
|
||||||
|
val numHistoricoSB = StringBuilder()
|
||||||
|
var operador : Char = ' '
|
||||||
|
var huboResultado = false
|
||||||
|
|
||||||
|
@Preview
|
||||||
|
@Composable
|
||||||
|
fun Principal() {
|
||||||
|
var numHistorico by rememberSaveable{ mutableStateOf( "" ) }
|
||||||
|
var numActual by rememberSaveable{ mutableStateOf( "" ) }
|
||||||
|
val configuration = LocalConfiguration.current
|
||||||
|
|
||||||
|
val modificador: Modifier = when (configuration.orientation) {
|
||||||
|
Configuration.ORIENTATION_LANDSCAPE -> {
|
||||||
|
Modifier.padding(all = 0.dp)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
Modifier.padding(start = 8.dp, end = 8.dp, top = 50.dp, bottom = 50.dp)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.then(modificador)
|
||||||
|
){
|
||||||
|
Column (
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth(),
|
||||||
|
verticalArrangement = Arrangement.spacedBy(10.dp),
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
|
){
|
||||||
|
Display(historico = numHistorico, numActual = numActual)
|
||||||
|
Botonera(accionPulsar = {
|
||||||
|
numActual = operacion(pulsador = it)
|
||||||
|
numHistorico = historico()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun operacion(pulsador: String): String {
|
||||||
|
when (pulsador) {
|
||||||
|
"+" -> sumar()
|
||||||
|
"-" -> restar()
|
||||||
|
"×" -> multiplicar()
|
||||||
|
"÷" -> dividir()
|
||||||
|
"." -> numActualSB.append(pulsador)
|
||||||
|
"=" -> resultado()
|
||||||
|
"DEL" -> if (numActualSB.isNotEmpty()) numActualSB.deleteCharAt(numActualSB.lastIndex)
|
||||||
|
"AC" -> {
|
||||||
|
numActualSB.clear()
|
||||||
|
numHistoricoSB.clear()
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
if (huboResultado) {
|
||||||
|
numActualSB.clear()
|
||||||
|
numActualSB.append(pulsador)
|
||||||
|
huboResultado = false
|
||||||
|
}else{
|
||||||
|
numActualSB.append(pulsador)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return numActualSB.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun sumar() {
|
||||||
|
if (numHistoricoSB.isBlank()) {
|
||||||
|
numHistoricoSB.append(numActualSB.toString())
|
||||||
|
numActualSB.clear()
|
||||||
|
operador = '+'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun restar() {
|
||||||
|
if (numHistoricoSB.isBlank()) {
|
||||||
|
numHistoricoSB.append(numActualSB.toString())
|
||||||
|
numActualSB.clear()
|
||||||
|
operador = '-'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun multiplicar() {
|
||||||
|
if (numHistoricoSB.isBlank()) {
|
||||||
|
numHistoricoSB.append(numActualSB.toString())
|
||||||
|
numActualSB.clear()
|
||||||
|
operador = '×'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun dividir() {
|
||||||
|
if (numHistoricoSB.isBlank()) {
|
||||||
|
numHistoricoSB.append(numActualSB.toString())
|
||||||
|
numActualSB.clear()
|
||||||
|
operador = '÷'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun resultado() {
|
||||||
|
var resultado = 0
|
||||||
|
|
||||||
|
if (numActualSB.isNotBlank() && numHistoricoSB.isNotBlank()) {
|
||||||
|
when (operador) {
|
||||||
|
'+' -> resultado = numHistoricoSB.toString().toInt() + numActualSB.toString().toInt()
|
||||||
|
'-' -> resultado = numHistoricoSB.toString().toInt() - numActualSB.toString().toInt()
|
||||||
|
'×' -> resultado = numHistoricoSB.toString().toInt() * numActualSB.toString().toInt()
|
||||||
|
'÷' -> resultado = numHistoricoSB.toString().toInt() / numActualSB.toString().toInt()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
numHistoricoSB.clear()
|
||||||
|
numActualSB.clear()
|
||||||
|
huboResultado = true
|
||||||
|
numActualSB.append(resultado)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun historico(): String {
|
||||||
|
return numHistoricoSB.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Composable que imita un display de una calculadora con dos lineas de texto con diferentes
|
||||||
|
tamaños.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun Display(historico: String, numActual: String){
|
||||||
|
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.clip(shape = RoundedCornerShape(20))
|
||||||
|
.background(Color.DarkGray)
|
||||||
|
.fillMaxWidth(),
|
||||||
|
horizontalAlignment = Alignment.End,
|
||||||
|
verticalArrangement = Arrangement.Center
|
||||||
|
) {
|
||||||
|
val separacion = 15.dp
|
||||||
|
Text(text = historico,
|
||||||
|
fontWeight = FontWeight.ExtraBold,
|
||||||
|
fontSize = 20.sp,
|
||||||
|
color = Color.White,
|
||||||
|
modifier = Modifier.padding(end = separacion))
|
||||||
|
Text(
|
||||||
|
text = numActual,
|
||||||
|
fontWeight = FontWeight.ExtraBold,
|
||||||
|
fontSize = 50.sp,
|
||||||
|
color = Color.White,
|
||||||
|
modifier = Modifier.padding(end = separacion)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Composable que define el pulsador y al que se le puede modificar el color de fondo, del texto,
|
||||||
|
el propio texto, y se le puede pasar un modificador completo si fuese necesario.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun Pulsador(
|
||||||
|
simbolo: String,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
color: Color = Color.Yellow,
|
||||||
|
colorFuente: Color = Color.Black,
|
||||||
|
onClick: () -> Unit //Evento al que se le pasará una lambda
|
||||||
|
){
|
||||||
|
Box(modifier = Modifier
|
||||||
|
.clip(shape = RoundedCornerShape(30))
|
||||||
|
.background(color)
|
||||||
|
.clickable { onClick() }
|
||||||
|
.then(modifier),
|
||||||
|
contentAlignment = Alignment.Center) {
|
||||||
|
Text(
|
||||||
|
text = simbolo,
|
||||||
|
fontWeight = FontWeight.ExtraBold,
|
||||||
|
fontSize = 30.sp,
|
||||||
|
color = colorFuente
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Composable que crea una columna de varias filas donde se generan los diferentes pulsadores de
|
||||||
|
la calculadora, pasándole los parámetros necesarios.
|
||||||
|
|
||||||
|
La función recibe una lambda a la que devuelve un string. En este caso, el símbolo del pulsador.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun Botonera(accionPulsar: (String) -> Unit){
|
||||||
|
val simple = 1f
|
||||||
|
val doble = 2f
|
||||||
|
val colorOperandos = Color.LightGray
|
||||||
|
Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||||
|
Row(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.weight(1f),
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(10.dp)
|
||||||
|
) {
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "OFF",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
color = Color.Black,
|
||||||
|
colorFuente = Color.Yellow
|
||||||
|
) {
|
||||||
|
val activity = MainActivity()
|
||||||
|
activity.finish()
|
||||||
|
exitProcess(0)
|
||||||
|
}
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "DEL",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
color = Color.Blue
|
||||||
|
) { accionPulsar("DEL") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "AC",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
color = Color.Red
|
||||||
|
) { accionPulsar("AC") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "÷",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
color = colorOperandos
|
||||||
|
) { accionPulsar("÷") }
|
||||||
|
}
|
||||||
|
Row(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.weight(1f),
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(10.dp)
|
||||||
|
) {
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "7",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
) { accionPulsar("7") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "8",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
) { accionPulsar("8") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "9",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
/*.aspectRatio(simple),*/
|
||||||
|
) { accionPulsar("9") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "×",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
color = colorOperandos
|
||||||
|
) { accionPulsar("×") }
|
||||||
|
}
|
||||||
|
Row(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.weight(1f),
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(10.dp)
|
||||||
|
) {
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "4",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
) { accionPulsar("4") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "5",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
) { accionPulsar("5") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "6",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
) { accionPulsar("6") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "-",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
color = colorOperandos
|
||||||
|
) { accionPulsar("-") }
|
||||||
|
}
|
||||||
|
Row(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.weight(1f),
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(10.dp)
|
||||||
|
) {
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "1",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
) { accionPulsar("1") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "2",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
) { accionPulsar("2") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "3",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
) { accionPulsar("3") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "+",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
color = colorOperandos
|
||||||
|
) { accionPulsar("+") }
|
||||||
|
}
|
||||||
|
Row(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.weight(1f),
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(10.dp)
|
||||||
|
) {
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "0",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
) { accionPulsar("0") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = ".",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(simple),
|
||||||
|
) { accionPulsar(".") }
|
||||||
|
Pulsador(
|
||||||
|
simbolo = "=",
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxHeight()
|
||||||
|
.weight(doble),
|
||||||
|
color = OrangeMine
|
||||||
|
) { accionPulsar("=") }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,3 +10,4 @@ val PantoneLightGray = Color(0xFFD9D7C8)
|
||||||
val PantoneSloganDarkGray = Color(0xFF40444F)
|
val PantoneSloganDarkGray = Color(0xFF40444F)
|
||||||
val PantoneProcessBlue = Color(0xFF0085CA)
|
val PantoneProcessBlue = Color(0xFF0085CA)
|
||||||
val PantoneClassicGreen = Color(0xFF009F53)
|
val PantoneClassicGreen = Color(0xFF009F53)
|
||||||
|
val OrangeMine = Color(0xFFFF5722)
|
||||||
Loading…
Reference in New Issue