mirror of
https://github.com/dzeiocom/OpenHealth.git
synced 2025-04-22 10:52:13 +00:00
fix: Weight unit not being taken into account (#157)
This commit is contained in:
parent
497cc58057
commit
6802f64c94
@ -12,6 +12,7 @@ import androidx.navigation.fragment.findNavController
|
||||
import com.dzeio.openhealth.R
|
||||
import com.dzeio.openhealth.core.BaseFragment
|
||||
import com.dzeio.openhealth.databinding.FragmentBrowseBinding
|
||||
import com.dzeio.openhealth.units.Mass
|
||||
import com.dzeio.openhealth.utils.PermissionsUtils
|
||||
import com.google.android.material.card.MaterialCardView
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
@ -109,10 +110,11 @@ class BrowseFragment :
|
||||
|
||||
// display the current user's weight
|
||||
viewModel.weight.observe(viewLifecycleOwner) {
|
||||
binding.weightText.text = String.format(
|
||||
resources.getString(R.string.weight_current),
|
||||
String.format(resources.getString(R.string.unit_mass_kilogram_unit), it)
|
||||
)
|
||||
updateWeight()
|
||||
}
|
||||
|
||||
viewModel.massUnit.observe(viewLifecycleOwner) {
|
||||
updateWeight()
|
||||
}
|
||||
}
|
||||
|
||||
@ -123,4 +125,18 @@ class BrowseFragment :
|
||||
}
|
||||
binding.stepsText.setText(text)
|
||||
}
|
||||
|
||||
private fun updateWeight() {
|
||||
val weight = viewModel.weight.value
|
||||
val unit = viewModel.massUnit.value ?: Mass.KILOGRAM
|
||||
|
||||
if (weight == null) {
|
||||
return
|
||||
}
|
||||
|
||||
binding.weightText.text = getString(
|
||||
R.string.weight_current,
|
||||
getString(unit.unit, unit.fromKilogram(weight))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import com.dzeio.openhealth.Settings
|
||||
import com.dzeio.openhealth.core.BaseViewModel
|
||||
import com.dzeio.openhealth.data.step.StepRepository
|
||||
import com.dzeio.openhealth.data.weight.WeightRepository
|
||||
import com.dzeio.openhealth.units.Mass
|
||||
import com.dzeio.openhealth.utils.Configuration
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
@ -20,6 +21,9 @@ class BrowseViewModel @Inject internal constructor(
|
||||
config: Configuration
|
||||
) : BaseViewModel() {
|
||||
|
||||
private val _massUnit = MutableLiveData(Mass.KILOGRAM)
|
||||
val massUnit: LiveData<Mass> = _massUnit
|
||||
|
||||
private val _steps = MutableLiveData(0)
|
||||
val steps: LiveData<Int> = _steps
|
||||
|
||||
@ -41,5 +45,15 @@ class BrowseViewModel @Inject internal constructor(
|
||||
_weight.postValue(it.weight)
|
||||
}
|
||||
}
|
||||
|
||||
config.getInt(Settings.MASS_UNIT).apply {
|
||||
addObserver {
|
||||
if (it == null) return@addObserver
|
||||
_massUnit.postValue(Mass.find(it))
|
||||
}
|
||||
if (value != null) {
|
||||
_massUnit.postValue(Mass.find(value!!))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -108,6 +108,12 @@ class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>(HomeViewMo
|
||||
ChartUtils.materielTheme(this, requireView())
|
||||
}
|
||||
|
||||
// update the graph when the weight unit change
|
||||
viewModel.massUnit.observe(viewLifecycleOwner) { unit ->
|
||||
binding.weightGraph.yAxis.onValueFormat = { getString(unit.unit, unit.fromKilogram(it)) }
|
||||
binding.weightGraph.refresh()
|
||||
}
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
binding.gotoTests.apply {
|
||||
visibility = View.VISIBLE
|
||||
@ -154,11 +160,6 @@ class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>(HomeViewMo
|
||||
viewModel.goalWeight.observe(viewLifecycleOwner) {
|
||||
updateWeightGraph()
|
||||
}
|
||||
|
||||
// update the graph when the weight unit change
|
||||
viewModel.massUnit.observe(viewLifecycleOwner) {
|
||||
updateWeightGraph()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -167,21 +168,14 @@ class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>(HomeViewMo
|
||||
private fun updateWeightGraph() {
|
||||
val values = viewModel.weights.value ?: arrayListOf()
|
||||
val goal = viewModel.goalWeight.value
|
||||
val unit = viewModel.massUnit.value
|
||||
|
||||
if (unit == null) {
|
||||
return
|
||||
}
|
||||
|
||||
val chart = binding.weightGraph.apply {
|
||||
yAxis.onValueFormat = { getString(unit.unit, unit.fromKilogram(it)) }
|
||||
}
|
||||
val chart = binding.weightGraph
|
||||
val serie = chart.series[0] as LineSerie
|
||||
|
||||
val entries: ArrayList<Entry> = values.map {
|
||||
Entry(
|
||||
it.timestamp.toDouble(),
|
||||
it.weight * unit.modifier
|
||||
it.weight
|
||||
)
|
||||
} as ArrayList<Entry>
|
||||
|
||||
|
@ -96,7 +96,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
||||
}
|
||||
}
|
||||
|
||||
val activityPreference = findPreference<ListPreference>("tmp.com.dzeio.open-health.activitylevel")
|
||||
val activityPreference = findPreference<ListPreference>("tmp." + Settings.USER_ACTIVITY_LEVEL)
|
||||
activityPreference?.apply {
|
||||
val value = config.getInt(Settings.USER_ACTIVITY_LEVEL)
|
||||
setOnPreferenceClickListener {
|
||||
@ -111,7 +111,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
||||
}
|
||||
}
|
||||
|
||||
val biologicalSexPreference = findPreference<ListPreference>("tmp.com.dzeio.open-health.biological_sex")
|
||||
val biologicalSexPreference = findPreference<ListPreference>("tmp." + Settings.USER_BIOLOGICAL_SEX)
|
||||
biologicalSexPreference?.apply {
|
||||
val value = config.getInt(Settings.USER_BIOLOGICAL_SEX)
|
||||
setOnPreferenceClickListener {
|
||||
@ -151,6 +151,23 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
||||
}
|
||||
}
|
||||
|
||||
findPreference<ListPreference>("tmp." + Settings.MASS_UNIT)?.apply {
|
||||
entries = Mass.values().map { getString(it.singular) }.toTypedArray()
|
||||
entryValues = Mass.values().map { it.ordinal.toString() }.toTypedArray()
|
||||
val unit = config.getInt(Settings.MASS_UNIT)
|
||||
|
||||
setOnPreferenceClickListener {
|
||||
if (unit.value != null) setValueIndex(unit.value!!)
|
||||
return@setOnPreferenceClickListener true
|
||||
}
|
||||
|
||||
setOnPreferenceChangeListener { _, newValue ->
|
||||
val nv = (newValue as String).toIntOrNull()
|
||||
unit.value = nv
|
||||
return@setOnPreferenceChangeListener false
|
||||
}
|
||||
}
|
||||
|
||||
findPreference<IntEditTextPreference>("tmp." + Settings.WATER_INTAKE_DAILY_GOAL)?.apply {
|
||||
val value = config.getInt(Settings.WATER_INTAKE_DAILY_GOAL)
|
||||
val unit = config.getInt(Settings.VOLUME_UNIT)
|
||||
|
@ -41,7 +41,7 @@
|
||||
<string name="edit_daily_goal">Modifier le but journalier</string>
|
||||
<string name="permission_declined">Vous avez décliné une permission, vous ne pouvez pas utiliser cette extension suaf si vous réactivez la permission manuellement</string>
|
||||
<string name="steps">Pas</string>
|
||||
<string name="weight_current">Poid actuel: %1$s%2$s</string>
|
||||
<string name="weight_current">Poid actuel: %1$s</string>
|
||||
<string name="delete">Supprimer</string>
|
||||
<string name="close">Fermer</string>
|
||||
|
||||
@ -65,7 +65,7 @@
|
||||
<string name="export_complete">Export Réussi!</string>
|
||||
<string name="import_export">Importer/Exporter</string>
|
||||
<string name="days_until_goal_is_achieved">Jours avant d\'atteindre son but: ~%1$d jours</string>
|
||||
<string name="weight_item">Date: %1$s\nBMI: %2$.2f\nEau corporelle: %3$.2f\nMuscles: %4$.2f\nMasse maigre: %5$.2f\nMasse grasse: %6$.2f\nMasse osseuse: %7$.2f\nGraisse viscérale: %8$.2f\nMétabolisme basal: %9$d\nDépense énergétique quotidienne totale: %10$d</string>
|
||||
<string name="weight_item">Date: %1$s\nBMI: %2$.2f\nEau corporelle: %3$.2f%%\nMuscles: %4$.2f%%\nMasse maigre: %5$.2f%%\nMasse grasse: %6$.2f%%\nMasse osseuse: %7$.2f%%\nGraisse viscérale: %8$.2f%%\nMétabolisme basal: %9$d\nDépense énergétique quotidienne totale: %10$d</string>
|
||||
<string-array name="activity_levels">
|
||||
<item>Cloué au lit</item>
|
||||
<item>Sédentaire</item>
|
||||
|
@ -78,7 +78,7 @@
|
||||
<string name="export_complete">Export successful!</string>
|
||||
<string name="import_export">Import/Export</string>
|
||||
<string name="days_until_goal_is_achieved">Days until goal is achieved: ~%1$d days</string>
|
||||
<string name="weight_item">Date: %1$s\nBMI: %2$.2f\nBody water: %3$.2f\nMuscles: %4$.2f\nLean body mass: %5$.2f\nBody fat: %6$.2f\nBone mass: %7$.2f\nVisceral fat: %8$.2f\nBasal metabolic rate: %9$d\nTotal daily energy expendure: %10$d\n</string>
|
||||
<string name="weight_item">Date: %1$s\nBMI: %2$.2f\nBody water: %3$.2f%%\nMuscles: %4$.2f%%\nLean body mass: %5$.2f%%\nBody fat: %6$.2f%%\nBone mass: %7$.2f%%\nVisceral fat: %8$.2f%%\nBasal metabolic rate: %9$d\nTotal daily energy expendure: %10$d\n</string>
|
||||
<string name="activity">Activity</string>
|
||||
<string name="vitals">Vitals</string>
|
||||
<string name="heart_rate">Heart Rate</string>
|
||||
|
@ -3,7 +3,6 @@
|
||||
|
||||
<PreferenceCategory android:title="@string/settings_global">
|
||||
<ListPreference
|
||||
android:defaultValue="Male"
|
||||
android:entries="@array/biological_sex"
|
||||
android:entryValues="@array/biological_sex"
|
||||
android:key="tmp.com.dzeio.open-health.biological_sex"
|
||||
@ -39,10 +38,7 @@
|
||||
android:singleLine="true"
|
||||
android:title="Goal Weight" />
|
||||
<ListPreference
|
||||
android:defaultValue="kg"
|
||||
android:entries="@array/mass_units"
|
||||
android:entryValues="@array/mass_units"
|
||||
android:key="com.dzeio.open-health.unit.mass"
|
||||
android:key="tmp.com.dzeio.open-health.unit.mass"
|
||||
android:title="Mass Unit" />
|
||||
</PreferenceCategory>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user