Menyesuaikan Kalkulasi agar bisa memilih Standar Perhitungan BMI
This commit is contained in:
parent
eb0720c592
commit
59db5c2b0c
@ -50,6 +50,7 @@ import androidx.compose.material3.Switch
|
|||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.material3.TextField
|
import androidx.compose.material3.TextField
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
@ -90,6 +91,12 @@ fun BMICalculatorLayout() {
|
|||||||
var weightInput by remember { mutableStateOf("") }
|
var weightInput by remember { mutableStateOf("") }
|
||||||
var unitUSC by remember { mutableStateOf(false) }
|
var unitUSC by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
// Reset Nilai Input
|
||||||
|
LaunchedEffect(unitUSC) {
|
||||||
|
heightInput = ""
|
||||||
|
weightInput = ""
|
||||||
|
}
|
||||||
|
|
||||||
val BmiHeight = heightInput.toDoubleOrNull() ?: 0.0
|
val BmiHeight = heightInput.toDoubleOrNull() ?: 0.0
|
||||||
val BmiWeight = weightInput.toDoubleOrNull() ?: 0.0
|
val BmiWeight = weightInput.toDoubleOrNull() ?: 0.0
|
||||||
val bmi = calculateBMI(BmiHeight, BmiWeight, unitUSC)
|
val bmi = calculateBMI(BmiHeight, BmiWeight, unitUSC)
|
||||||
@ -111,7 +118,7 @@ fun BMICalculatorLayout() {
|
|||||||
.align(alignment = Alignment.Start)
|
.align(alignment = Alignment.Start)
|
||||||
)
|
)
|
||||||
EditNumberField(
|
EditNumberField(
|
||||||
label = R.string.height,
|
label = if (unitUSC == true) R.string.heightInch else R.string.heightCm,
|
||||||
leadingIcon = R.drawable.number,
|
leadingIcon = R.drawable.number,
|
||||||
keyboardOptions = KeyboardOptions.Default.copy(
|
keyboardOptions = KeyboardOptions.Default.copy(
|
||||||
keyboardType = KeyboardType.Number,
|
keyboardType = KeyboardType.Number,
|
||||||
@ -122,7 +129,7 @@ fun BMICalculatorLayout() {
|
|||||||
modifier = Modifier.padding(bottom = 32.dp).fillMaxWidth(),
|
modifier = Modifier.padding(bottom = 32.dp).fillMaxWidth(),
|
||||||
)
|
)
|
||||||
EditNumberField(
|
EditNumberField(
|
||||||
label = R.string.weight,
|
label = if (unitUSC) R.string.weightPound else R.string.weightKg,
|
||||||
leadingIcon = R.drawable.number,
|
leadingIcon = R.drawable.number,
|
||||||
keyboardOptions = KeyboardOptions.Default.copy(
|
keyboardOptions = KeyboardOptions.Default.copy(
|
||||||
keyboardType = KeyboardType.Number,
|
keyboardType = KeyboardType.Number,
|
||||||
@ -206,12 +213,10 @@ private fun calculateBMI(BmiHeight: Double, BmiWeight: Double, unitUSC: Boolean)
|
|||||||
}
|
}
|
||||||
|
|
||||||
val heightInMeter = BmiHeight/100 // konversi centimeter ke meter
|
val heightInMeter = BmiHeight/100 // konversi centimeter ke meter
|
||||||
val heightInInch = BmiHeight * 0.3937 // konversi centimeter ke inci
|
|
||||||
val weightInPound = BmiWeight * 2.2046 // konversi berat badan ke pound (lbs)
|
|
||||||
|
|
||||||
var bmi = BmiWeight / heightInMeter.pow(2)
|
var bmi = BmiWeight / heightInMeter.pow(2)
|
||||||
if (unitUSC) {
|
if (unitUSC == true) {
|
||||||
bmi = 703 * (weightInPound / heightInInch.pow(2))
|
bmi = 703 * (BmiWeight / BmiHeight.pow(2))
|
||||||
}
|
}
|
||||||
|
|
||||||
val df = DecimalFormat("#.#")
|
val df = DecimalFormat("#.#")
|
||||||
|
|||||||
@ -17,8 +17,10 @@
|
|||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">BMI Calculator</string>
|
<string name="app_name">BMI Calculator</string>
|
||||||
<string name="calculate_bmi">Calculate BMI</string>
|
<string name="calculate_bmi">Calculate BMI</string>
|
||||||
<string name="height">Tinggi Badan</string>
|
<string name="heightCm">Tinggi Badan (cm)</string>
|
||||||
<string name="weight">Berat Badan</string>
|
<string name="weightKg">Berat Badan (kg)</string>
|
||||||
|
<string name="heightInch">Tinggi Badan (inch)</string>
|
||||||
|
<string name="weightPound">Berat Badan (lbs)</string>
|
||||||
<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>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user