1 /**
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * ```
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * ```
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.healthconnect.controller.dataentries.formatters
17 
18 import android.content.Context
19 import android.health.connect.datatypes.HeightRecord
20 import android.health.connect.datatypes.units.Length
21 import android.icu.text.MessageFormat.format
22 import androidx.annotation.StringRes
23 import com.android.healthconnect.controller.R
24 import com.android.healthconnect.controller.dataentries.formatters.shared.EntryFormatter
25 import com.android.healthconnect.controller.dataentries.units.HeightUnit
26 import com.android.healthconnect.controller.dataentries.units.HeightUnit.CENTIMETERS
27 import com.android.healthconnect.controller.dataentries.units.HeightUnit.FEET
28 import com.android.healthconnect.controller.dataentries.units.LengthConverter.convertHeightFromMeters
29 import com.android.healthconnect.controller.dataentries.units.UnitPreferences
30 import dagger.hilt.android.qualifiers.ApplicationContext
31 import javax.inject.Inject
32 import kotlin.math.roundToInt
33 
34 /** Formatter for printing Height data. */
35 class HeightFormatter @Inject constructor(@ApplicationContext private val context: Context) :
36     EntryFormatter<HeightRecord>(context) {
37 
38     companion object {
39         private const val FEET_IN_INCHES = 12
40     }
41 
formatValuenull42     override suspend fun formatValue(
43         record: HeightRecord,
44         unitPreferences: UnitPreferences
45     ): String {
46         return formatHeight(
47             R.string.height_ft_compacted,
48             R.string.height_in_compacted,
49             R.string.height_cm,
50             R.string.feet_inches_format,
51             unitPreferences.getHeightUnit(),
52             record.height)
53     }
54 
formatA11yValuenull55     override suspend fun formatA11yValue(
56         record: HeightRecord,
57         unitPreferences: UnitPreferences
58     ): String {
59         return formatHeight(
60             R.string.height_ft_long,
61             R.string.height_in_long,
62             R.string.height_cm_long,
63             R.string.feet_inches_format_long,
64             unitPreferences.getHeightUnit(),
65             record.height)
66     }
67 
formatHeightnull68     private fun formatHeight(
69         @StringRes feetId: Int,
70         @StringRes inchId: Int,
71         @StringRes cmId: Int,
72         @StringRes feetInchFormatId: Int,
73         heightUnit: HeightUnit,
74         length: Length
75     ): String {
76         return when (heightUnit) {
77             CENTIMETERS -> {
78                 val heightInCentimeters = convertHeightFromMeters(heightUnit, length.inMeters)
79                 format(context.getString(cmId), mapOf("height" to heightInCentimeters.roundToInt()))
80             }
81             FEET -> {
82                 val heightInInches =
83                     convertHeightFromMeters(heightUnit, length.inMeters).roundToInt()
84                 val feet = heightInInches / FEET_IN_INCHES
85                 val inches = heightInInches % FEET_IN_INCHES
86                 context.getString(
87                     feetInchFormatId,
88                     format(context.getString(feetId), mapOf("height" to feet)),
89                     format(context.getString(inchId), mapOf("height" to inches)))
90             }
91         }
92     }
93 }
94