1 /**
<lambda>null2  * Copyright (C) 2024 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.PlannedExerciseBlock
20 import android.icu.text.MessageFormat
21 import com.android.healthconnect.controller.R
22 import com.android.healthconnect.controller.data.entries.FormattedEntry
23 import com.android.healthconnect.controller.data.entries.FormattedEntry.PlannedExerciseBlockEntry
24 import com.android.healthconnect.controller.dataentries.units.UnitPreferences
25 import dagger.hilt.android.qualifiers.ApplicationContext
26 import javax.inject.Inject
27 
28 /** Formatter for printing PlannedExerciseBlock data. */
29 class PlannedExerciseBlockFormatter
30 @Inject
31 constructor(
32     @ApplicationContext private val context: Context,
33     private val plannedExerciseStepFormatter: PlannedExerciseStepFormatter
34 ) {
35 
36     fun formatBlock(
37         block: PlannedExerciseBlock,
38     ): FormattedEntry {
39         return PlannedExerciseBlockEntry(
40             block = block, title = formatBlockTitle(block), titleA11y = formatBlockTitleA11y(block))
41     }
42 
43     private fun formatBlockTitle(block: PlannedExerciseBlock): String {
44         return context.getString(
45             R.string.planned_exercise_block_title,
46             block.description,
47             MessageFormat.format(
48                 context.getString(R.string.planned_exercise_block_repetitions),
49                 mapOf("count" to block.repetitions)))
50     }
51 
52     private fun formatBlockTitleA11y(block: PlannedExerciseBlock): String {
53         return context.getString(
54             R.string.planned_exercise_block_a11y_title,
55             block.description,
56             MessageFormat.format(
57                 context.getString(R.string.planned_exercise_block_repetitions),
58                 mapOf("count" to block.repetitions)))
59     }
60 
61     fun formatBlockDetails(
62         block: PlannedExerciseBlock,
63         unitPreferences: UnitPreferences
64     ): List<FormattedEntry> {
65         val exerciseSteps = block.steps
66         return buildList {
67             exerciseSteps.forEach { plannedExerciseStep ->
68                 add(plannedExerciseStepFormatter.formatStep(plannedExerciseStep, unitPreferences))
69                 addAll(
70                     plannedExerciseStepFormatter.formatStepDetails(
71                         plannedExerciseStep, unitPreferences))
72             }
73         }
74     }
75 }
76