1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * 17 */ 18 19 package com.android.healthconnect.controller.dataentries.formatters.shared 20 21 import android.content.Context 22 import android.health.connect.datatypes.units.Length 23 import android.icu.text.MessageFormat 24 import androidx.annotation.StringRes 25 import com.android.healthconnect.controller.R 26 import com.android.healthconnect.controller.dataentries.units.DistanceUnit 27 import com.android.healthconnect.controller.dataentries.units.LengthConverter 28 import com.android.healthconnect.controller.dataentries.units.UnitPreferences 29 30 object LengthFormatter { 31 formatValuenull32 fun formatValue(context: Context, length: Length?, unitPreferences: UnitPreferences): String { 33 if (length == null) { 34 return "" 35 } 36 val res = 37 when (unitPreferences.getDistanceUnit()) { 38 DistanceUnit.KILOMETERS -> R.string.distance_km 39 DistanceUnit.MILES -> R.string.distance_miles 40 } 41 return formatSample(context, res, length, unitPreferences) 42 } 43 formatA11yValuenull44 fun formatA11yValue( 45 context: Context, 46 length: Length?, 47 unitPreferences: UnitPreferences 48 ): String { 49 if (length == null) { 50 return "" 51 } 52 val res = 53 when (unitPreferences.getDistanceUnit()) { 54 DistanceUnit.KILOMETERS -> R.string.distance_km_long 55 DistanceUnit.MILES -> R.string.distance_miles_long 56 } 57 return formatSample(context, res, length, unitPreferences) 58 } 59 formatSamplenull60 private fun formatSample( 61 context: Context, 62 @StringRes res: Int, 63 length: Length, 64 unitPreferences: UnitPreferences 65 ): String { 66 val value = 67 LengthConverter.convertDistanceFromMeters( 68 unitPreferences.getDistanceUnit(), length.inMeters) 69 return MessageFormat.format(context.getString(res), mapOf("dist" to value)) 70 } 71 } 72