1
0
mirror of https://github.com/dzeiocom/OpenHealth.git synced 2025-04-23 11:22:10 +00:00

fix: Weight unit not being taken into account (#157)

This commit is contained in:
Florian Bouillon 2023-03-06 17:32:36 +01:00 committed by GitHub
parent 497cc58057
commit 6802f64c94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 28 deletions

View File

@ -12,6 +12,7 @@ import androidx.navigation.fragment.findNavController
import com.dzeio.openhealth.R import com.dzeio.openhealth.R
import com.dzeio.openhealth.core.BaseFragment import com.dzeio.openhealth.core.BaseFragment
import com.dzeio.openhealth.databinding.FragmentBrowseBinding import com.dzeio.openhealth.databinding.FragmentBrowseBinding
import com.dzeio.openhealth.units.Mass
import com.dzeio.openhealth.utils.PermissionsUtils import com.dzeio.openhealth.utils.PermissionsUtils
import com.google.android.material.card.MaterialCardView import com.google.android.material.card.MaterialCardView
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
@ -109,10 +110,11 @@ class BrowseFragment :
// display the current user's weight // display the current user's weight
viewModel.weight.observe(viewLifecycleOwner) { viewModel.weight.observe(viewLifecycleOwner) {
binding.weightText.text = String.format( updateWeight()
resources.getString(R.string.weight_current), }
String.format(resources.getString(R.string.unit_mass_kilogram_unit), it)
) viewModel.massUnit.observe(viewLifecycleOwner) {
updateWeight()
} }
} }
@ -123,4 +125,18 @@ class BrowseFragment :
} }
binding.stepsText.setText(text) 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))
)
}
} }

View File

@ -7,6 +7,7 @@ import com.dzeio.openhealth.Settings
import com.dzeio.openhealth.core.BaseViewModel import com.dzeio.openhealth.core.BaseViewModel
import com.dzeio.openhealth.data.step.StepRepository import com.dzeio.openhealth.data.step.StepRepository
import com.dzeio.openhealth.data.weight.WeightRepository import com.dzeio.openhealth.data.weight.WeightRepository
import com.dzeio.openhealth.units.Mass
import com.dzeio.openhealth.utils.Configuration import com.dzeio.openhealth.utils.Configuration
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject import javax.inject.Inject
@ -20,6 +21,9 @@ class BrowseViewModel @Inject internal constructor(
config: Configuration config: Configuration
) : BaseViewModel() { ) : BaseViewModel() {
private val _massUnit = MutableLiveData(Mass.KILOGRAM)
val massUnit: LiveData<Mass> = _massUnit
private val _steps = MutableLiveData(0) private val _steps = MutableLiveData(0)
val steps: LiveData<Int> = _steps val steps: LiveData<Int> = _steps
@ -41,5 +45,15 @@ class BrowseViewModel @Inject internal constructor(
_weight.postValue(it.weight) _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!!))
}
}
} }
} }

View File

@ -108,6 +108,12 @@ class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>(HomeViewMo
ChartUtils.materielTheme(this, requireView()) 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) { if (BuildConfig.DEBUG) {
binding.gotoTests.apply { binding.gotoTests.apply {
visibility = View.VISIBLE visibility = View.VISIBLE
@ -154,11 +160,6 @@ class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>(HomeViewMo
viewModel.goalWeight.observe(viewLifecycleOwner) { viewModel.goalWeight.observe(viewLifecycleOwner) {
updateWeightGraph() 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() { private fun updateWeightGraph() {
val values = viewModel.weights.value ?: arrayListOf() val values = viewModel.weights.value ?: arrayListOf()
val goal = viewModel.goalWeight.value val goal = viewModel.goalWeight.value
val unit = viewModel.massUnit.value
if (unit == null) { val chart = binding.weightGraph
return
}
val chart = binding.weightGraph.apply {
yAxis.onValueFormat = { getString(unit.unit, unit.fromKilogram(it)) }
}
val serie = chart.series[0] as LineSerie val serie = chart.series[0] as LineSerie
val entries: ArrayList<Entry> = values.map { val entries: ArrayList<Entry> = values.map {
Entry( Entry(
it.timestamp.toDouble(), it.timestamp.toDouble(),
it.weight * unit.modifier it.weight
) )
} as ArrayList<Entry> } as ArrayList<Entry>

View File

@ -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 { activityPreference?.apply {
val value = config.getInt(Settings.USER_ACTIVITY_LEVEL) val value = config.getInt(Settings.USER_ACTIVITY_LEVEL)
setOnPreferenceClickListener { 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 { biologicalSexPreference?.apply {
val value = config.getInt(Settings.USER_BIOLOGICAL_SEX) val value = config.getInt(Settings.USER_BIOLOGICAL_SEX)
setOnPreferenceClickListener { 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 { findPreference<IntEditTextPreference>("tmp." + Settings.WATER_INTAKE_DAILY_GOAL)?.apply {
val value = config.getInt(Settings.WATER_INTAKE_DAILY_GOAL) val value = config.getInt(Settings.WATER_INTAKE_DAILY_GOAL)
val unit = config.getInt(Settings.VOLUME_UNIT) val unit = config.getInt(Settings.VOLUME_UNIT)

View File

@ -41,7 +41,7 @@
<string name="edit_daily_goal">Modifier le but journalier</string> <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="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="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="delete">Supprimer</string>
<string name="close">Fermer</string> <string name="close">Fermer</string>
@ -65,7 +65,7 @@
<string name="export_complete">Export Réussi!</string> <string name="export_complete">Export Réussi!</string>
<string name="import_export">Importer/Exporter</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="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"> <string-array name="activity_levels">
<item>Cloué au lit</item> <item>Cloué au lit</item>
<item>Sédentaire</item> <item>Sédentaire</item>

View File

@ -78,7 +78,7 @@
<string name="export_complete">Export successful!</string> <string name="export_complete">Export successful!</string>
<string name="import_export">Import/Export</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="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="activity">Activity</string>
<string name="vitals">Vitals</string> <string name="vitals">Vitals</string>
<string name="heart_rate">Heart Rate</string> <string name="heart_rate">Heart Rate</string>

View File

@ -3,7 +3,6 @@
<PreferenceCategory android:title="@string/settings_global"> <PreferenceCategory android:title="@string/settings_global">
<ListPreference <ListPreference
android:defaultValue="Male"
android:entries="@array/biological_sex" android:entries="@array/biological_sex"
android:entryValues="@array/biological_sex" android:entryValues="@array/biological_sex"
android:key="tmp.com.dzeio.open-health.biological_sex" android:key="tmp.com.dzeio.open-health.biological_sex"
@ -39,10 +38,7 @@
android:singleLine="true" android:singleLine="true"
android:title="Goal Weight" /> android:title="Goal Weight" />
<ListPreference <ListPreference
android:defaultValue="kg" android:key="tmp.com.dzeio.open-health.unit.mass"
android:entries="@array/mass_units"
android:entryValues="@array/mass_units"
android:key="com.dzeio.open-health.unit.mass"
android:title="Mass Unit" /> android:title="Mass Unit" />
</PreferenceCategory> </PreferenceCategory>