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.Vo2MaxRecord
20 import android.health.connect.datatypes.Vo2MaxRecord.Vo2MaxMeasurementMethod.MEASUREMENT_METHOD_COOPER_TEST
21 import android.health.connect.datatypes.Vo2MaxRecord.Vo2MaxMeasurementMethod.MEASUREMENT_METHOD_HEART_RATE_RATIO
22 import android.health.connect.datatypes.Vo2MaxRecord.Vo2MaxMeasurementMethod.MEASUREMENT_METHOD_METABOLIC_CART
23 import android.health.connect.datatypes.Vo2MaxRecord.Vo2MaxMeasurementMethod.MEASUREMENT_METHOD_MULTISTAGE_FITNESS_TEST
24 import android.health.connect.datatypes.Vo2MaxRecord.Vo2MaxMeasurementMethod.MEASUREMENT_METHOD_OTHER
25 import android.health.connect.datatypes.Vo2MaxRecord.Vo2MaxMeasurementMethod.MEASUREMENT_METHOD_ROCKPORT_FITNESS_TEST
26 import android.icu.text.MessageFormat
27 import androidx.annotation.StringRes
28 import com.android.healthconnect.controller.R
29 import com.android.healthconnect.controller.dataentries.formatters.shared.EntryFormatter
30 import com.android.healthconnect.controller.dataentries.units.UnitPreferences
31 import dagger.hilt.android.qualifiers.ApplicationContext
32 import javax.inject.Inject
33 
34 /** Formatter for printing Vo2MaxRecord data. */
35 class Vo2MaxFormatter @Inject constructor(@ApplicationContext private val context: Context) :
36     EntryFormatter<Vo2MaxRecord>(context) {
37 
formatValuenull38     override suspend fun formatValue(
39         record: Vo2MaxRecord,
40         unitPreferences: UnitPreferences
41     ): String {
42         return formatVo2Max(R.string.vo2_max, record)
43     }
44 
formatA11yValuenull45     override suspend fun formatA11yValue(
46         record: Vo2MaxRecord,
47         unitPreferences: UnitPreferences
48     ): String {
49         return formatVo2Max(R.string.vo2_max_long, record)
50     }
51 
formatVo2Maxnull52     private fun formatVo2Max(@StringRes res: Int, record: Vo2MaxRecord): String {
53         val vo2max =
54             MessageFormat.format(
55                 context.getString(res), mapOf("value" to record.vo2MillilitersPerMinuteKilogram))
56 
57         return if (record.measurementMethod != MEASUREMENT_METHOD_OTHER) {
58             "$vo2max ${getMeasurementMethod(record.measurementMethod)}"
59         } else {
60             vo2max
61         }
62     }
63 
getMeasurementMethodnull64     private fun getMeasurementMethod(method: Int): String {
65         return when (method) {
66             MEASUREMENT_METHOD_METABOLIC_CART -> context.getString(R.string.vo2_metabolic_cart)
67             MEASUREMENT_METHOD_HEART_RATE_RATIO -> context.getString(R.string.vo2_heart_rate_ratio)
68             MEASUREMENT_METHOD_COOPER_TEST -> context.getString(R.string.vo2_cooper_test)
69             MEASUREMENT_METHOD_MULTISTAGE_FITNESS_TEST ->
70                 context.getString(R.string.vo2_multistage_fitness_test)
71             MEASUREMENT_METHOD_ROCKPORT_FITNESS_TEST ->
72                 context.getString(R.string.vo2_rockport_fitness_test)
73             MEASUREMENT_METHOD_OTHER -> context.getString(R.string.vo2_other)
74             else -> {
75                 throw IllegalArgumentException("Unrecognised VO₂ measurement method: $method")
76             }
77         }
78     }
79 }
80