1 package com.android.healthconnect.controller.dataentries.formatters
2 
3 import android.content.Context
4 import android.health.connect.datatypes.units.Mass
5 import android.icu.text.MessageFormat.format
6 import androidx.annotation.StringRes
7 import com.android.healthconnect.controller.R
8 import com.android.healthconnect.controller.dataentries.units.WeightConverter
9 import com.android.healthconnect.controller.dataentries.units.WeightConverter.stonePoundsFromPounds
10 import com.android.healthconnect.controller.dataentries.units.WeightUnit
11 import com.android.healthconnect.controller.dataentries.units.WeightUnit.POUND
12 
13 /** Formats mass (bone mass, body weight etc). */
14 object MassFormatter {
15 
16     /** Returns formatted weight in the user's current unit with default short unit strings. */
formatValuenull17     fun formatValue(context: Context, mass: Mass, weightUnit: WeightUnit): String {
18         return getMassString(
19             context,
20             mass,
21             weightUnit,
22             R.string.kilograms_short_label,
23             R.string.pounds_short_label,
24             R.string.stone_short_label,
25             R.string.stone_pound_short_label)
26     }
27 
28     /** Returns formatted weight in the user's current unit with default long unit strings. */
formatA11yValuenull29     fun formatA11yValue(context: Context, mass: Mass, weightUnit: WeightUnit): String {
30         return getMassString(
31             context,
32             mass,
33             weightUnit,
34             R.string.kilograms_long_label,
35             R.string.pounds_long_label,
36             R.string.stone_long_label,
37             R.string.stone_pound_long_label)
38     }
39 
getMassStringnull40     private fun getMassString(
41         context: Context,
42         mass: Mass,
43         weightUnit: WeightUnit,
44         @StringRes kilogramStringId: Int,
45         @StringRes poundStringId: Int,
46         @StringRes stoneStringId: Int,
47         @StringRes stonePoundStringId: Int
48     ): String {
49         return when (weightUnit) {
50             POUND -> {
51                 val pounds = WeightConverter.convertFromGrams(POUND, mass.inGrams)
52                 val truncatedPounds = Math.round(pounds * 10) / 10.0
53                 format(context.getString(poundStringId), mapOf("count" to truncatedPounds))
54             }
55             WeightUnit.STONE -> {
56                 val pounds = WeightConverter.convertFromGrams(POUND, mass.inGrams)
57                 val stonePounds = stonePoundsFromPounds(pounds, 1)
58                 if (stonePounds.pounds > 0) {
59                     val part1 =
60                         format(
61                             context.getString(stoneStringId), mapOf("count" to stonePounds.stone))
62                     val part2 =
63                         format(
64                             context.getString(poundStringId),
65                             mapOf("count" to stonePounds.pounds, "delta_symbol" to ""))
66                     format(
67                         context.getString(stonePoundStringId),
68                         mapOf("stone_part" to part1, "pound_part" to part2))
69                 } else {
70                     format(context.getString(stoneStringId), mapOf("count" to stonePounds.stone))
71                 }
72             }
73             WeightUnit.KILOGRAM -> {
74                 val truncatedKg = Math.round(mass.inGrams / 1000 * 10) / 10.0
75                 format(context.getString(kilogramStringId), mapOf("count" to truncatedKg))
76             }
77         }
78     }
79 }
80