• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(block: PlannedExerciseBlock): FormattedEntry {
37         return PlannedExerciseBlockEntry(
38             block = block,
39             title = formatBlockTitle(block),
40             titleA11y = formatBlockTitleA11y(block),
41         )
42     }
43 
44     private fun formatBlockTitle(block: PlannedExerciseBlock): String {
45         return if (block.description != null) {
46             context.getString(
47                 R.string.planned_exercise_block_title,
48                 block.description,
49                 MessageFormat.format(
50                     context.getString(R.string.planned_exercise_block_repetitions),
51                     mapOf("count" to block.repetitions),
52                 ),
53             )
54         } else {
55             MessageFormat.format(
56                 context.getString(R.string.planned_exercise_block_repetitions),
57                 mapOf("count" to block.repetitions),
58             )
59         }
60     }
61 
62     private fun formatBlockTitleA11y(block: PlannedExerciseBlock): String {
63         return if (block.description != null) {
64             context.getString(
65                 R.string.planned_exercise_block_a11y_title,
66                 block.description,
67                 MessageFormat.format(
68                     context.getString(R.string.planned_exercise_block_repetitions),
69                     mapOf("count" to block.repetitions),
70                 ),
71             )
72         } else {
73             MessageFormat.format(
74                 context.getString(R.string.planned_exercise_block_repetitions),
75                 mapOf("count" to block.repetitions),
76             )
77         }
78     }
79 
80     fun formatBlockDetails(
81         block: PlannedExerciseBlock,
82         unitPreferences: UnitPreferences,
83     ): List<FormattedEntry> {
84         val exerciseSteps = block.steps
85         return buildList {
86             exerciseSteps.forEach { plannedExerciseStep ->
87                 add(plannedExerciseStepFormatter.formatStep(plannedExerciseStep, unitPreferences))
88                 addAll(
89                     plannedExerciseStepFormatter.formatStepDetails(
90                         plannedExerciseStep,
91                         unitPreferences,
92                     )
93                 )
94             }
95         }
96     }
97 }
98