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.CervicalMucusRecord 20 import android.health.connect.datatypes.CervicalMucusRecord.CervicalMucusAppearance.APPEARANCE_CREAMY 21 import android.health.connect.datatypes.CervicalMucusRecord.CervicalMucusAppearance.APPEARANCE_DRY 22 import android.health.connect.datatypes.CervicalMucusRecord.CervicalMucusAppearance.APPEARANCE_EGG_WHITE 23 import android.health.connect.datatypes.CervicalMucusRecord.CervicalMucusAppearance.APPEARANCE_STICKY 24 import android.health.connect.datatypes.CervicalMucusRecord.CervicalMucusAppearance.APPEARANCE_UNKNOWN 25 import android.health.connect.datatypes.CervicalMucusRecord.CervicalMucusAppearance.APPEARANCE_UNUSUAL 26 import android.health.connect.datatypes.CervicalMucusRecord.CervicalMucusAppearance.APPEARANCE_WATERY 27 import android.health.connect.datatypes.CervicalMucusRecord.CervicalMucusSensation.SENSATION_HEAVY 28 import android.health.connect.datatypes.CervicalMucusRecord.CervicalMucusSensation.SENSATION_LIGHT 29 import android.health.connect.datatypes.CervicalMucusRecord.CervicalMucusSensation.SENSATION_MEDIUM 30 import android.health.connect.datatypes.CervicalMucusRecord.CervicalMucusSensation.SENSATION_UNKNOWN 31 import com.android.healthconnect.controller.R 32 import com.android.healthconnect.controller.dataentries.formatters.shared.EntryFormatter 33 import com.android.healthconnect.controller.dataentries.units.UnitPreferences 34 import dagger.hilt.android.qualifiers.ApplicationContext 35 import java.util.StringJoiner 36 import javax.inject.Inject 37 38 /** Formatter for printing CervicalMucusRecord data. */ 39 class CervicalMucusFormatter @Inject constructor(@ApplicationContext private val context: Context) : 40 EntryFormatter<CervicalMucusRecord>(context) { 41 formatValuenull42 override suspend fun formatValue( 43 record: CervicalMucusRecord, 44 unitPreferences: UnitPreferences 45 ): String { 46 val stringJoiner = StringJoiner(" ") 47 if (record.appearance != APPEARANCE_UNKNOWN) { 48 stringJoiner.add(formatAppearances(record.appearance)) 49 } 50 if (record.sensation != SENSATION_UNKNOWN) { 51 stringJoiner.add(formatSensation(record.sensation)) 52 } 53 val stringResult = stringJoiner.toString() 54 if (stringResult.isBlank()) { 55 return context.getString(R.string.unknown_type) 56 } 57 return stringResult 58 } 59 formatSensationnull60 private fun formatSensation(sensation: Int): String { 61 return when (sensation) { 62 SENSATION_LIGHT -> context.getString(R.string.mucus_light) 63 SENSATION_MEDIUM -> context.getString(R.string.mucus_medium) 64 SENSATION_HEAVY -> context.getString(R.string.mucus_heavy) 65 else -> { 66 throw java.lang.IllegalArgumentException("Unrecognised mucus sensation: $sensation") 67 } 68 } 69 } 70 formatA11yValuenull71 override suspend fun formatA11yValue( 72 record: CervicalMucusRecord, 73 unitPreferences: UnitPreferences 74 ): String { 75 return formatValue(record, unitPreferences) 76 } 77 formatAppearancesnull78 private fun formatAppearances(appearances: Int): String { 79 return when (appearances) { 80 APPEARANCE_DRY -> context.getString(R.string.mucus_dry) 81 APPEARANCE_STICKY -> context.getString(R.string.mucus_sticky) 82 APPEARANCE_CREAMY -> context.getString(R.string.mucus_creamy) 83 APPEARANCE_WATERY -> context.getString(R.string.mucus_watery) 84 APPEARANCE_EGG_WHITE -> context.getString(R.string.mucus_egg_white) 85 APPEARANCE_UNUSUAL -> context.getString(R.string.mucus_unusual) 86 else -> { 87 throw IllegalArgumentException("Unrecognised mucus texture: $appearances") 88 } 89 } 90 } 91 } 92