Body Mass calculator refactor

- Added normal weight info
- Added link to help
- Added tests
This commit is contained in:
Sad Ellie 2024-01-12 17:04:45 +03:00
parent 03836c4164
commit 7cd4d16ae0
9 changed files with 501 additions and 169 deletions

View File

@ -10,6 +10,9 @@
<string name="body_mass_imperial">Imperial</string> <string name="body_mass_imperial">Imperial</string>
<string name="body_mass_metric">Metric</string> <string name="body_mass_metric">Metric</string>
<string name="body_mass_normal">Normal weight</string> <string name="body_mass_normal">Normal weight</string>
<!-- Will be a text followed by a number like so: "Normal weight for your height - 60" -->
<string name="body_mass_normal_weight">Normal weight for your height</string>
<string name="body_mass_obese_1">Obese (Class 1)</string> <string name="body_mass_obese_1">Obese (Class 1)</string>
<string name="body_mass_obese_2">Obese (Class 2)</string> <string name="body_mass_obese_2">Obese (Class 2)</string>
<string name="body_mass_obese_3">Obese (Class 3)</string> <string name="body_mass_obese_3">Obese (Class 3)</string>

View File

@ -37,7 +37,7 @@ import androidx.window.layout.WindowMetricsCalculator
val LocalWindowSize: ProvidableCompositionLocal<WindowSizeClass> = compositionLocalOf { val LocalWindowSize: ProvidableCompositionLocal<WindowSizeClass> = compositionLocalOf {
WindowSizeClass.calculateFromSize( WindowSizeClass.calculateFromSize(
size = Size.Unspecified, size = Size.Zero,
density = defaultDensity density = defaultDensity
) )
} }

View File

@ -18,7 +18,6 @@
package com.sadellie.unitto.feature.bodymass package com.sadellie.unitto.feature.bodymass
import androidx.annotation.StringRes
import androidx.compose.animation.AnimatedContent import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.Crossfade import androidx.compose.animation.Crossfade
import androidx.compose.animation.SizeTransform import androidx.compose.animation.SizeTransform
@ -32,33 +31,25 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.verticalScroll
import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.input.VisualTransformation import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.sadellie.unitto.core.base.OutputFormat
import com.sadellie.unitto.core.base.R import com.sadellie.unitto.core.base.R
import com.sadellie.unitto.core.base.Token
import com.sadellie.unitto.core.ui.common.MenuButton import com.sadellie.unitto.core.ui.common.MenuButton
import com.sadellie.unitto.core.ui.common.SegmentedButton import com.sadellie.unitto.core.ui.common.SegmentedButton
import com.sadellie.unitto.core.ui.common.SegmentedButtonsRow import com.sadellie.unitto.core.ui.common.SegmentedButtonsRow
@ -66,10 +57,11 @@ import com.sadellie.unitto.core.ui.common.SettingsButton
import com.sadellie.unitto.core.ui.common.UnittoEmptyScreen import com.sadellie.unitto.core.ui.common.UnittoEmptyScreen
import com.sadellie.unitto.core.ui.common.UnittoScreenWithTopBar import com.sadellie.unitto.core.ui.common.UnittoScreenWithTopBar
import com.sadellie.unitto.core.ui.common.textfield.ExpressionTransformer import com.sadellie.unitto.core.ui.common.textfield.ExpressionTransformer
import com.sadellie.unitto.core.ui.common.textfield.formatExpression import com.sadellie.unitto.core.ui.common.textfield.FormatterSymbols
import com.sadellie.unitto.data.common.format import com.sadellie.unitto.core.ui.openLink
import com.sadellie.unitto.data.common.isEqualTo import com.sadellie.unitto.data.common.isEqualTo
import com.sadellie.unitto.data.common.isLessThan import com.sadellie.unitto.feature.bodymass.components.BodyMassResult
import com.sadellie.unitto.feature.bodymass.components.BodyMassTextField
import java.math.BigDecimal import java.math.BigDecimal
@Composable @Composable
@ -106,15 +98,10 @@ private fun BodyMassScreen(
val expressionTransformer = remember(uiState.formatterSymbols) { val expressionTransformer = remember(uiState.formatterSymbols) {
ExpressionTransformer(uiState.formatterSymbols) ExpressionTransformer(uiState.formatterSymbols)
} }
val weightLabel = remember(uiState.isMetric) { val weightShortLabel = remember(uiState.isMetric) {
val s1 = mContext.resources.getString(R.string.body_mass_weight) mContext.resources.getString(
val s2 = if (uiState.isMetric) { if (uiState.isMetric) R.string.unit_kilogram_short else R.string.unit_pound_short
mContext.resources.getString(R.string.unit_kilogram_short) )
} else {
mContext.resources.getString(R.string.unit_pound_short)
}
"$s1, $s2"
} }
UnittoScreenWithTopBar( UnittoScreenWithTopBar(
@ -124,10 +111,12 @@ private fun BodyMassScreen(
) { paddingValues -> ) { paddingValues ->
Column( Column(
modifier = Modifier modifier = Modifier
.verticalScroll(rememberScrollState())
.padding(paddingValues) .padding(paddingValues)
.padding(16.dp) .padding(16.dp)
.fillMaxSize(), .fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(16.dp) verticalArrangement = Arrangement.spacedBy(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) { ) {
SegmentedButtonsRow( SegmentedButtonsRow(
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
@ -193,7 +182,7 @@ private fun BodyMassScreen(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
value = uiState.weight, value = uiState.weight,
onValueChange = updateWeight, onValueChange = updateWeight,
label = weightLabel, label = "${stringResource(R.string.body_mass_weight)}, $weightShortLabel",
expressionFormatter = expressionTransformer, expressionFormatter = expressionTransformer,
imeAction = ImeAction.Done imeAction = ImeAction.Done
) )
@ -208,112 +197,44 @@ private fun BodyMassScreen(
) { targetState -> ) { targetState ->
if (targetState.isEqualTo(BigDecimal.ZERO)) return@AnimatedContent if (targetState.isEqualTo(BigDecimal.ZERO)) return@AnimatedContent
val value = remember(targetState) { BodyMassResult(
targetState value = targetState,
.format(3, OutputFormat.PLAIN) range = uiState.normalWeightRange,
.formatExpression(uiState.formatterSymbols) rangeSuffix = weightShortLabel,
} formatterSymbols = uiState.formatterSymbols
)
}
val classification = remember(targetState) { ElevatedButton(
getBodyMassData(targetState) onClick = {
} openLink(mContext, "https://sadellie.github.io/unitto/help#body-mass-index")
Column(
modifier = Modifier
.clip(RoundedCornerShape(32.dp))
.background(classification.color)
.padding(16.dp, 32.dp)
.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
// TODO Link to web
Text(
text = stringResource(classification.classification),
style = MaterialTheme.typography.displaySmall,
)
Text(
text = value,
style = MaterialTheme.typography.bodyLarge
)
} }
) {
Text(text = stringResource(R.string.time_zone_no_results_button)) // TODO Rename
} }
} }
} }
} }
@Preview
@Composable @Composable
private fun BodyMassTextField( fun PreviewBodyMassScreen() {
modifier: Modifier, BodyMassScreen(
value: TextFieldValue, uiState = UIState.Ready(
onValueChange: (TextFieldValue) -> Unit, isMetric = false,
label: String, height1 = TextFieldValue(),
expressionFormatter: VisualTransformation, height2 = TextFieldValue(),
imeAction: ImeAction weight = TextFieldValue(),
) { normalWeightRange = BigDecimal(30) to BigDecimal(50),
val focusManager = LocalFocusManager.current result = BigDecimal(18.5),
OutlinedTextField( allowVibration = false,
modifier = modifier, formatterSymbols = FormatterSymbols.Spaces
value = value, ),
onValueChange = { updateHeight1 = {},
val cleanText = it.text updateHeight2 = {},
.replace(",", ".") updateWeight = {},
.filter { char -> updateIsMetric = {},
Token.Digit.allWithDot.contains(char.toString()) openDrawer = {},
} navigateToSettings = {}
onValueChange(it.copy(cleanText))
},
label = { AnimatedContent(label) { Text(it) } },
singleLine = true,
visualTransformation = expressionFormatter,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal, imeAction = imeAction),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() })
) )
} }
@Immutable
private data class BodyMassData(
val color: Color,
@StringRes val classification: Int
)
@Stable
private fun getBodyMassData(value: BigDecimal): BodyMassData = when {
value.isLessThan(BigDecimal("18.5")) -> {
BodyMassData(
color = Color(0x800EACDD),
classification = R.string.body_mass_underweight
)
}
value.isLessThan(BigDecimal("25")) -> {
BodyMassData(
color = Color(0x805BF724),
classification = R.string.body_mass_normal
)
}
value.isLessThan(BigDecimal("30")) -> {
BodyMassData(
color = Color(0x80DBEC18),
classification = R.string.body_mass_overweight
)
}
value.isLessThan(BigDecimal("35")) -> {
BodyMassData(
color = Color(0x80FF9634),
classification = R.string.body_mass_obese_1
)
}
value.isLessThan(BigDecimal("40")) -> {
BodyMassData(
color = Color(0x80F85F31),
classification = R.string.body_mass_obese_2
)
}
else -> {
BodyMassData(
color = Color(0x80FF2323),
classification = R.string.body_mass_obese_3
)
}
}

View File

@ -0,0 +1,112 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2024 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.feature.bodymass
import com.sadellie.unitto.core.base.MAX_PRECISION
import java.math.BigDecimal
import java.math.RoundingMode
/**
* Calculates BMI value for metric system.
*
* @param heightCm Height in centimeters.
* @param weightKg Weight in kilograms.
* @return BMI Value with [MAX_PRECISION].
*/
internal fun calculateMetric(
heightCm: BigDecimal,
weightKg: BigDecimal,
): BigDecimal {
val heightMeters = heightCm
.divide(cmToMFactor, MAX_PRECISION, RoundingMode.HALF_EVEN)
return weightKg
.divide(heightMeters.pow(2), MAX_PRECISION, RoundingMode.HALF_EVEN)
}
/**
* Calculates BMI value for imperial system.
*
* @param heightFt Height in feet.
* @param heightIn Height in inches.
* @param weightLbs Weight in pounds.
* @return BMI Value with [MAX_PRECISION].
*/
internal fun calculateImperial(
heightFt: BigDecimal,
heightIn: BigDecimal,
weightLbs: BigDecimal
): BigDecimal {
val heightInches = heightFt
.multiply(footToInchFactor)
.plus(heightIn)
return weightLbs
.divide(heightInches.pow(2), MAX_PRECISION, RoundingMode.HALF_EVEN)
.multiply(metricToImperialFactor) // Approximate lbs/inch^2 to kg/m^2
}
/**
* Calculates weight (kilograms) range for normal BMI values.
*
* @param heightCm Height in centimeters.
* @return [Pair] of [BigDecimal]. First value is lowest weight. Second value is highest value.
*/
internal fun calculateNormalWeightMetric(
heightCm: BigDecimal
): Pair<BigDecimal, BigDecimal> {
val heightMetres2 = heightCm
.divide(cmToMFactor, MAX_PRECISION, RoundingMode.HALF_EVEN)
.pow(2)
val lowest = lowestNormalIndex.multiply(heightMetres2)
val highest = highestNormalIndex.multiply(heightMetres2)
return lowest to highest
}
/**
* Calculates weight (pounds) range for normal BMI values.
*
* @param heightFt Height in feet.
* @param heightIn Height in inches.
* @return [Pair] of [BigDecimal]. First value is lowest weight. Second value is highest value.
*/
internal fun calculateNormalWeightImperial(
heightFt: BigDecimal,
heightIn: BigDecimal,
): Pair<BigDecimal, BigDecimal> {
val heightInches2 = heightFt
.multiply(footToInchFactor)
.plus(heightIn)
.pow(2)
.divide(metricToImperialFactor, MAX_PRECISION, RoundingMode.HALF_EVEN)
val lowest = lowestNormalIndex.multiply(heightInches2)
val highest = highestNormalIndex.multiply(heightInches2)
return lowest to highest
}
private val cmToMFactor = BigDecimal("100")
private val footToInchFactor = BigDecimal("12")
private val metricToImperialFactor = BigDecimal("703")
private val lowestNormalIndex = BigDecimal("18.5")
private val highestNormalIndex = BigDecimal("25")

View File

@ -24,7 +24,6 @@ import android.os.Build
import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.sadellie.unitto.core.base.MAX_PRECISION
import com.sadellie.unitto.core.ui.common.textfield.AllFormatterSymbols import com.sadellie.unitto.core.ui.common.textfield.AllFormatterSymbols
import com.sadellie.unitto.data.common.combine import com.sadellie.unitto.data.common.combine
import com.sadellie.unitto.data.common.stateIn import com.sadellie.unitto.data.common.stateIn
@ -36,7 +35,6 @@ import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.math.BigDecimal import java.math.BigDecimal
import java.math.RoundingMode
import javax.inject.Inject import javax.inject.Inject
@HiltViewModel @HiltViewModel
@ -48,9 +46,7 @@ internal class BodyMassViewModel @Inject constructor(
private val _height2 = MutableStateFlow(TextFieldValue()) private val _height2 = MutableStateFlow(TextFieldValue())
private val _weight = MutableStateFlow(TextFieldValue()) private val _weight = MutableStateFlow(TextFieldValue())
private val _result = MutableStateFlow<BigDecimal>(BigDecimal.ZERO) private val _result = MutableStateFlow<BigDecimal>(BigDecimal.ZERO)
private val cmToMFactor = BigDecimal("10000") private val _normalWeightRange = MutableStateFlow<Pair<BigDecimal, BigDecimal>>(BigDecimal.ZERO to BigDecimal.ZERO)
private val footToInchFactor = BigDecimal("12")
private val metricToImperialFactor = BigDecimal("703")
val uiState = combine( val uiState = combine(
userPreferencesRepository.bodyMassPrefs, userPreferencesRepository.bodyMassPrefs,
@ -58,35 +54,41 @@ internal class BodyMassViewModel @Inject constructor(
_height1, _height1,
_height2, _height2,
_weight, _weight,
_result _result,
) { userPrefs, isMetric, height1, height2, weight, result -> _normalWeightRange
) { userPrefs, isMetric, height1, height2, weight, result, normalWeightRange ->
UIState.Ready( UIState.Ready(
isMetric = isMetric, isMetric = isMetric,
height1 = height1, height1 = height1,
height2 = height2, height2 = height2,
weight = weight, weight = weight,
result = result, result = result,
normalWeightRange = normalWeightRange,
allowVibration = userPrefs.enableVibrations, allowVibration = userPrefs.enableVibrations,
formatterSymbols = AllFormatterSymbols.getById(userPrefs.separator) formatterSymbols = AllFormatterSymbols.getById(userPrefs.separator)
) )
} }
.mapLatest { ui -> .mapLatest { ui ->
val newResult: BigDecimal = try { withContext(Dispatchers.Default) {
val height1 = BigDecimal(ui.height1.text.ifEmpty { "0" }) try {
val weight = BigDecimal(ui.weight.text.ifEmpty { "0" }) val height1 = BigDecimal(ui.height1.text.ifEmpty { "0" })
val weight = BigDecimal(ui.weight.text.ifEmpty { "0" })
if (ui.isMetric) { if (ui.isMetric) {
calculateMetric(height1, weight) _result.update { calculateMetric(height1, weight) }
} else { _normalWeightRange.update { calculateNormalWeightMetric(height1) }
val height2 = BigDecimal(ui.height2.text.ifEmpty { "0" }) } else {
calculateImperial(height1, height2, weight) val height2 = BigDecimal(ui.height2.text.ifEmpty { "0" })
_result.update { calculateImperial(height1, height2, weight) }
_normalWeightRange.update { calculateNormalWeightImperial(height1, height2) }
}
} catch (e: Exception) {
_result.update { BigDecimal.ZERO }
_normalWeightRange.update { BigDecimal.ZERO to BigDecimal.ZERO }
} }
} catch (e: Exception) {
BigDecimal.ZERO
} }
_result.update { newResult }
ui ui
} }
.stateIn(viewModelScope, UIState.Loading) .stateIn(viewModelScope, UIState.Loading)
@ -96,29 +98,6 @@ internal class BodyMassViewModel @Inject constructor(
fun updateWeight(textFieldValue: TextFieldValue) = _weight.update { textFieldValue } fun updateWeight(textFieldValue: TextFieldValue) = _weight.update { textFieldValue }
fun updateIsMetric(isMetric: Boolean) = _isMetric.update { isMetric } fun updateIsMetric(isMetric: Boolean) = _isMetric.update { isMetric }
private suspend fun calculateMetric(
height: BigDecimal,
weight: BigDecimal,
) = withContext(Dispatchers.Default) {
return@withContext weight
.divide(height.pow(2), MAX_PRECISION, RoundingMode.HALF_EVEN)
.multiply(cmToMFactor)
}
private suspend fun calculateImperial(
height1: BigDecimal,
height2: BigDecimal,
weight: BigDecimal
) = withContext(Dispatchers.Default) {
val height = height1
.multiply(footToInchFactor)
.plus(height2)
return@withContext weight
.divide(height.pow(2), MAX_PRECISION, RoundingMode.HALF_EVEN)
.multiply(metricToImperialFactor) // Approximate lbs/inch^2 to kg/m^2
}
private fun getInitialIsMetric(): Boolean { private fun getInitialIsMetric(): Boolean {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) return true if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) return true
return LocaleData.getMeasurementSystem(ULocale.getDefault()) != LocaleData.MeasurementSystem.US return LocaleData.getMeasurementSystem(ULocale.getDefault()) != LocaleData.MeasurementSystem.US

View File

@ -30,6 +30,7 @@ internal sealed class UIState {
val height1: TextFieldValue, val height1: TextFieldValue,
val height2: TextFieldValue, val height2: TextFieldValue,
val weight: TextFieldValue, val weight: TextFieldValue,
val normalWeightRange: Pair<BigDecimal, BigDecimal>,
val result: BigDecimal, val result: BigDecimal,
val allowVibration: Boolean, val allowVibration: Boolean,
val formatterSymbols: FormatterSymbols, val formatterSymbols: FormatterSymbols,

View File

@ -0,0 +1,176 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2024 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.feature.bodymass.components
import androidx.annotation.StringRes
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.sadellie.unitto.core.base.OutputFormat
import com.sadellie.unitto.core.base.R
import com.sadellie.unitto.core.ui.common.textfield.FormatterSymbols
import com.sadellie.unitto.core.ui.common.textfield.formatExpression
import com.sadellie.unitto.data.common.format
import java.math.BigDecimal
@Composable
internal fun BodyMassResult(
modifier: Modifier = Modifier,
value: BigDecimal,
range: Pair<BigDecimal, BigDecimal>,
rangeSuffix: String,
formatterSymbols: FormatterSymbols,
) {
val formattedValue = remember(value, formatterSymbols) {
value
.format(3, OutputFormat.PLAIN)
.formatExpression(formatterSymbols)
}
val formattedRange = remember(range, formatterSymbols) {
val low = range.first
.format(0, OutputFormat.PLAIN)
.formatExpression(formatterSymbols)
val high = range.second
.format(0, OutputFormat.PLAIN)
.formatExpression(formatterSymbols)
"$low $high $rangeSuffix"
}
val classification = remember(value) {
getBodyMassData(value)
}
Column(
modifier = modifier,
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Column(
modifier = Modifier
.clip(RoundedCornerShape(32.dp))
.background(classification.color)
.padding(16.dp, 32.dp)
.fillMaxWidth(),
) {
Text(
text = stringResource(classification.classification),
style = MaterialTheme.typography.displaySmall,
color = MaterialTheme.colorScheme.onSecondaryContainer
)
Text(
text = formattedValue,
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSecondaryContainer
)
}
Column(
modifier = Modifier
.clip(RoundedCornerShape(32.dp))
.background(MaterialTheme.colorScheme.secondaryContainer)
.padding(16.dp, 32.dp)
.fillMaxWidth(),
) {
Text(
text = formattedRange,
style = MaterialTheme.typography.displaySmall,
color = MaterialTheme.colorScheme.onSecondaryContainer
)
Text(
text = stringResource(R.string.body_mass_normal_weight),
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSecondaryContainer
)
}
}
}
@Immutable
private data class BodyMassData(
val minValue: BigDecimal,
val color: Color,
@StringRes val classification: Int
)
@Stable
private fun getBodyMassData(value: BigDecimal): BodyMassData =
indexes.first { value >= it.minValue }
private val indexes by lazy {
listOf(
BodyMassData(
minValue = BigDecimal("40"),
color = Color(0x80FF2323),
classification = R.string.body_mass_obese_3
),
BodyMassData(
minValue = BigDecimal("35"),
color = Color(0x80F85F31),
classification = R.string.body_mass_obese_2
),
BodyMassData(
minValue = BigDecimal("30"),
color = Color(0x80FF9634),
classification = R.string.body_mass_obese_1
),
BodyMassData(
minValue = BigDecimal("25"),
color = Color(0x80DBEC18),
classification = R.string.body_mass_overweight
),
BodyMassData(
minValue = BigDecimal("18.5"),
color = Color(0x805BF724),
classification = R.string.body_mass_normal
),
BodyMassData(
minValue = BigDecimal("0"),
color = Color(0x800EACDD),
classification = R.string.body_mass_underweight
),
)
}
@Preview
@Composable
fun PreviewBodyMassResult() {
BodyMassResult(
value = BigDecimal(18.5),
range = BigDecimal(50) to BigDecimal(80),
rangeSuffix = "kg",
formatterSymbols = FormatterSymbols.Spaces,
)
}

View File

@ -0,0 +1,68 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2024 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.feature.bodymass.components
import androidx.compose.animation.AnimatedContent
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.text.style.TextAlign
import com.sadellie.unitto.core.base.Token
@Composable
internal fun BodyMassTextField(
modifier: Modifier,
value: TextFieldValue,
onValueChange: (TextFieldValue) -> Unit,
label: String,
expressionFormatter: VisualTransformation,
imeAction: ImeAction
) {
val focusManager = LocalFocusManager.current
OutlinedTextField(
modifier = modifier,
value = value,
onValueChange = {
val cleanText = it.text
.replace(",", ".")
.filter { char ->
Token.Digit.allWithDot.contains(char.toString())
}
onValueChange(it.copy(cleanText))
},
label = { AnimatedContent(label) { Text(it) } },
singleLine = true,
visualTransformation = expressionFormatter,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Decimal,
imeAction = imeAction
),
textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.End),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() })
)
}

View File

@ -0,0 +1,72 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2024 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.feature.bodymass
import org.junit.Assert.assertEquals
import org.junit.Test
import java.math.BigDecimal
import java.math.RoundingMode
class BodyMassTest {
@Test
fun testCalculateMetric() {
val height = BigDecimal("190")
val weight = BigDecimal("80")
val expected = BigDecimal("22.161").scaleDown()
val actual = calculateMetric(height, weight).scaleDown()
assertEquals(expected, actual)
}
@Test
fun testCalculateImperial() {
val height1 = BigDecimal("6")
val height2 = BigDecimal("5")
val weight = BigDecimal("150")
val expected = BigDecimal("17.785")
val actual = calculateImperial(height1, height2, weight).scaleDown()
assertEquals(expected, actual)
}
@Test
fun testCalculateNormalWeightMetric() {
val height = BigDecimal("190")
val expectedWeight1 = BigDecimal("66.785")
val expectedWeight2 = BigDecimal("90.250")
val actualWeight = calculateNormalWeightMetric(height)
assertEquals(expectedWeight1, actualWeight.first.scaleDown())
assertEquals(expectedWeight2, actualWeight.second.scaleDown())
}
@Test
fun testCalculateNormalWeightImperial() {
val heightFt = BigDecimal("6")
val heightIn = BigDecimal("5")
val expectedWeight1 = BigDecimal("156.026")
val expectedWeight2 = BigDecimal("210.846")
val actualWeight = calculateNormalWeightImperial(heightFt, heightIn)
assertEquals(expectedWeight1, actualWeight.first.scaleDown())
assertEquals(expectedWeight2, actualWeight.second.scaleDown())
}
private fun BigDecimal.scaleDown(): BigDecimal = setScale(3, RoundingMode.HALF_EVEN)
}