edit ketiga
This commit is contained in:
parent
838765e98c
commit
fc61c397e1
@ -6,6 +6,7 @@ import androidx.activity.compose.setContent
|
|||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.annotation.DrawableRes
|
import androidx.annotation.DrawableRes
|
||||||
import androidx.annotation.StringRes
|
import androidx.annotation.StringRes
|
||||||
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
@ -20,11 +21,9 @@ import androidx.compose.ui.text.input.ImeAction
|
|||||||
import androidx.compose.ui.text.input.KeyboardType
|
import androidx.compose.ui.text.input.KeyboardType
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.compose.ui.graphics.Brush
|
||||||
import com.example.tiptime.ui.theme.TipTimeTheme
|
import com.example.tiptime.ui.theme.TipTimeTheme
|
||||||
import kotlin.math.pow
|
import kotlin.math.pow
|
||||||
import kotlin.math.roundToInt
|
|
||||||
import androidx.compose.foundation.background
|
|
||||||
import androidx.compose.ui.graphics.Brush
|
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
@ -44,15 +43,21 @@ class MainActivity : ComponentActivity() {
|
|||||||
fun BmiCalculatorLayout() {
|
fun BmiCalculatorLayout() {
|
||||||
var heightInput by remember { mutableStateOf("") }
|
var heightInput by remember { mutableStateOf("") }
|
||||||
var weightInput by remember { mutableStateOf("") }
|
var weightInput by remember { mutableStateOf("") }
|
||||||
|
var useMetricSystem by remember { mutableStateOf(true) } // ✅ toggle sistem satuan
|
||||||
var errorMessage by remember { mutableStateOf("") }
|
var errorMessage by remember { mutableStateOf("") }
|
||||||
|
var showResult by remember { mutableStateOf(false) } // ✅ hasil hanya tampil setelah tombol ditekan
|
||||||
// State untuk hasil BMI
|
|
||||||
var bmiResult by remember { mutableStateOf<Float?>(null) }
|
|
||||||
var bmiCategory by remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
val height = heightInput.toFloatOrNull() ?: 0f
|
val height = heightInput.toFloatOrNull() ?: 0f
|
||||||
val weight = weightInput.toFloatOrNull() ?: 0f
|
val weight = weightInput.toFloatOrNull() ?: 0f
|
||||||
val isValid = validateInput(height, weight)
|
|
||||||
|
val isValid = validateInput(height, weight, useMetricSystem)
|
||||||
|
|
||||||
|
// Konversi tinggi & berat ke sistem metrik (meter & kg)
|
||||||
|
val heightInMeters = if (useMetricSystem) height / 100f else height * 0.0254f
|
||||||
|
val weightInKg = if (useMetricSystem) weight else weight * 0.453592f
|
||||||
|
|
||||||
|
val bmi = if (isValid && showResult) weightInKg / heightInMeters.pow(2) else 0f
|
||||||
|
val category = if (isValid && showResult) getBMICategory(bmi) else ""
|
||||||
|
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@ -70,6 +75,7 @@ fun BmiCalculatorLayout() {
|
|||||||
) {
|
) {
|
||||||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||||||
|
|
||||||
|
// Judul Aplikasi
|
||||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||||
Icon(
|
Icon(
|
||||||
painter = painterResource(id = R.drawable.ic_launcher_foreground),
|
painter = painterResource(id = R.drawable.ic_launcher_foreground),
|
||||||
@ -88,6 +94,22 @@ fun BmiCalculatorLayout() {
|
|||||||
|
|
||||||
Spacer(modifier = Modifier.height(24.dp))
|
Spacer(modifier = Modifier.height(24.dp))
|
||||||
|
|
||||||
|
// Toggle Sistem Satuan
|
||||||
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.Center
|
||||||
|
) {
|
||||||
|
Text("SI (kg, cm)")
|
||||||
|
Switch(
|
||||||
|
checked = !useMetricSystem,
|
||||||
|
onCheckedChange = { useMetricSystem = !useMetricSystem }
|
||||||
|
)
|
||||||
|
Text("USC (lbs, inci)")
|
||||||
|
}
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(24.dp))
|
||||||
|
|
||||||
|
// Card Input
|
||||||
Card(
|
Card(
|
||||||
elevation = CardDefaults.cardElevation(defaultElevation = 8.dp),
|
elevation = CardDefaults.cardElevation(defaultElevation = 8.dp),
|
||||||
colors = CardDefaults.cardColors(
|
colors = CardDefaults.cardColors(
|
||||||
@ -100,7 +122,7 @@ fun BmiCalculatorLayout() {
|
|||||||
horizontalAlignment = Alignment.CenterHorizontally
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
) {
|
) {
|
||||||
EditNumberField(
|
EditNumberField(
|
||||||
label = R.string.height,
|
label = if (useMetricSystem) R.string.height else R.string.height_inch,
|
||||||
leadingIcon = R.drawable.number,
|
leadingIcon = R.drawable.number,
|
||||||
keyboardOptions = KeyboardOptions(
|
keyboardOptions = KeyboardOptions(
|
||||||
keyboardType = KeyboardType.Number,
|
keyboardType = KeyboardType.Number,
|
||||||
@ -114,7 +136,7 @@ fun BmiCalculatorLayout() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
EditNumberField(
|
EditNumberField(
|
||||||
label = R.string.weight,
|
label = if (useMetricSystem) R.string.weight else R.string.weight_lbs,
|
||||||
leadingIcon = R.drawable.number,
|
leadingIcon = R.drawable.number,
|
||||||
keyboardOptions = KeyboardOptions(
|
keyboardOptions = KeyboardOptions(
|
||||||
keyboardType = KeyboardType.Number,
|
keyboardType = KeyboardType.Number,
|
||||||
@ -129,15 +151,18 @@ fun BmiCalculatorLayout() {
|
|||||||
|
|
||||||
Spacer(modifier = Modifier.height(24.dp))
|
Spacer(modifier = Modifier.height(24.dp))
|
||||||
|
|
||||||
|
// Tombol Hitung BMI
|
||||||
Button(
|
Button(
|
||||||
onClick = {
|
onClick = {
|
||||||
if (!isValid) {
|
if (!isValid) {
|
||||||
errorMessage = "Masukkan tinggi (20–250 cm) dan berat (10–250 kg) yang valid!"
|
showResult = false
|
||||||
bmiResult = null
|
errorMessage = if (useMetricSystem)
|
||||||
|
"Masukkan tinggi (20–250 cm) dan berat (10–250 kg) yang valid!"
|
||||||
|
else
|
||||||
|
"Masukkan tinggi (≥4 inci) dan berat (5–550 lbs) yang valid!"
|
||||||
} else {
|
} else {
|
||||||
errorMessage = ""
|
errorMessage = ""
|
||||||
bmiResult = calculateBMI(weight, height)
|
showResult = true
|
||||||
bmiCategory = getBMICategory(bmiResult!!)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@ -145,9 +170,13 @@ fun BmiCalculatorLayout() {
|
|||||||
.height(56.dp),
|
.height(56.dp),
|
||||||
elevation = ButtonDefaults.elevatedButtonElevation(8.dp)
|
elevation = ButtonDefaults.elevatedButtonElevation(8.dp)
|
||||||
) {
|
) {
|
||||||
Text(text = "Hitung BMI", style = MaterialTheme.typography.titleMedium)
|
Text(
|
||||||
|
text = "Hitung BMI",
|
||||||
|
style = MaterialTheme.typography.titleMedium
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pesan error
|
||||||
if (errorMessage.isNotEmpty()) {
|
if (errorMessage.isNotEmpty()) {
|
||||||
Text(
|
Text(
|
||||||
text = errorMessage,
|
text = errorMessage,
|
||||||
@ -157,9 +186,9 @@ fun BmiCalculatorLayout() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tampilkan hasil hanya jika sudah ditekan
|
// Hasil BMI hanya tampil setelah ditekan tombol
|
||||||
bmiResult?.let { bmi ->
|
if (showResult && isValid) {
|
||||||
val categoryColor = when (bmiCategory) {
|
val categoryColor = when (category) {
|
||||||
"Kurus" -> MaterialTheme.colorScheme.tertiaryContainer
|
"Kurus" -> MaterialTheme.colorScheme.tertiaryContainer
|
||||||
"Normal" -> MaterialTheme.colorScheme.primaryContainer
|
"Normal" -> MaterialTheme.colorScheme.primaryContainer
|
||||||
"Kelebihan Berat" -> MaterialTheme.colorScheme.secondaryContainer
|
"Kelebihan Berat" -> MaterialTheme.colorScheme.secondaryContainer
|
||||||
@ -182,7 +211,7 @@ fun BmiCalculatorLayout() {
|
|||||||
style = MaterialTheme.typography.titleLarge
|
style = MaterialTheme.typography.titleLarge
|
||||||
)
|
)
|
||||||
Text(
|
Text(
|
||||||
text = "Kategori: $bmiCategory",
|
text = "Kategori: $category",
|
||||||
style = MaterialTheme.typography.bodyLarge
|
style = MaterialTheme.typography.bodyLarge
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -192,11 +221,6 @@ fun BmiCalculatorLayout() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fungsi untuk membuat TextField input angka
|
|
||||||
*/
|
|
||||||
@Composable
|
@Composable
|
||||||
fun EditNumberField(
|
fun EditNumberField(
|
||||||
@StringRes label: Int,
|
@StringRes label: Int,
|
||||||
@ -217,18 +241,6 @@ fun EditNumberField(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Menghitung nilai BMI berdasarkan berat (kg) dan tinggi (cm)
|
|
||||||
* Rumus: BMI = berat / (tinggi/100)^2
|
|
||||||
*/
|
|
||||||
fun calculateBMI(weight: Float, height: Float): Float {
|
|
||||||
if (height <= 0) return 0f
|
|
||||||
return weight / (height / 100).pow(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Menentukan kategori BMI
|
|
||||||
*/
|
|
||||||
fun getBMICategory(bmi: Float): String {
|
fun getBMICategory(bmi: Float): String {
|
||||||
return when {
|
return when {
|
||||||
bmi < 18.5 -> "Kurus"
|
bmi < 18.5 -> "Kurus"
|
||||||
@ -238,11 +250,11 @@ fun getBMICategory(bmi: Float): String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
fun validateInput(height: Float, weight: Float, useMetric: Boolean): Boolean {
|
||||||
* Validasi input agar nilai masuk akal
|
return if (useMetric)
|
||||||
*/
|
height in 20f..250f && weight in 10f..250f
|
||||||
fun validateInput(height: Float, weight: Float): Boolean {
|
else
|
||||||
return height in 20f..250f && weight in 10f..250f
|
height >= 4f && height <= 100f && weight in 5f..550f
|
||||||
}
|
}
|
||||||
|
|
||||||
@Preview(showBackground = true)
|
@Preview(showBackground = true)
|
||||||
|
|||||||
@ -22,4 +22,6 @@
|
|||||||
<string name="use_usc">Gunakan Unit USC (lbs/in)?</string>
|
<string name="use_usc">Gunakan Unit USC (lbs/in)?</string>
|
||||||
<string name="bmi_calculation">BMI Anda: %s</string>
|
<string name="bmi_calculation">BMI Anda: %s</string>
|
||||||
<string name="bmi_category">Kategori: %s</string>
|
<string name="bmi_category">Kategori: %s</string>
|
||||||
|
<string name="height_inch">Tinggi Badan</string>
|
||||||
|
<string name="weight_lbs">Berat Badan</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user