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.PlannedExerciseSessionRecord
20 import com.android.healthconnect.controller.R
21 import com.android.healthconnect.controller.data.entries.FormattedEntry
22 import com.android.healthconnect.controller.data.entries.FormattedEntry.FormattedSectionContent
23 import com.android.healthconnect.controller.data.entries.FormattedEntry.ItemDataEntrySeparator
24 import com.android.healthconnect.controller.data.entries.FormattedEntry.SessionHeader
25 import com.android.healthconnect.controller.dataentries.formatters.ExerciseSessionFormatter.Companion.getExerciseType
26 import com.android.healthconnect.controller.dataentries.formatters.shared.BaseFormatter
27 import com.android.healthconnect.controller.dataentries.formatters.shared.RecordDetailsFormatter
28 import com.android.healthconnect.controller.dataentries.units.UnitPreferences
29 import com.android.healthconnect.controller.shared.DataType
30 import dagger.hilt.android.qualifiers.ApplicationContext
31 import javax.inject.Inject
32 
33 /** Formatter for printing PlannedExerciseSessionRecord data. */
34 class PlannedExerciseSessionRecordFormatter
35 @Inject
36 constructor(
37     @ApplicationContext private val context: Context,
38     private val plannedExerciseBlockFormatter: PlannedExerciseBlockFormatter
39 ) :
40     BaseFormatter<PlannedExerciseSessionRecord>(context),
41     RecordDetailsFormatter<PlannedExerciseSessionRecord> {
42 
43     override suspend fun formatRecord(
44         record: PlannedExerciseSessionRecord,
45         header: String,
46         headerA11y: String,
47         unitPreferences: UnitPreferences
48     ): FormattedEntry {
49         return FormattedEntry.PlannedExerciseSessionEntry(
50             uuid = record.metadata.id,
51             header = header,
52             headerA11y = headerA11y,
53             title = formatTitle(record),
54             titleA11y = formatTitle(record),
55             notes = getNotes(record),
56             dataType = DataType.PLANNED_EXERCISE)
57     }
58 
59     fun formatTitle(record: PlannedExerciseSessionRecord): String {
60         return context.getString(
61             R.string.planned_exercise_session_title,
62             getExerciseType(context, record.exerciseType),
63             record.title)
64     }
65 
66     private fun getNotes(record: PlannedExerciseSessionRecord): String? {
67         return record.notes?.toString()
68     }
69 
70     override suspend fun formatRecordDetails(
71         record: PlannedExerciseSessionRecord
72     ): List<FormattedEntry> {
73         val exerciseBlock = record.blocks
74         return buildList {
75             if (!record.notes.isNullOrBlank()) {
76                 add(ItemDataEntrySeparator())
77                 add(SessionHeader(context.getString(R.string.planned_exercise_session_notes_title)))
78                 add(FormattedSectionContent(record.notes.toString()))
79                 add(ItemDataEntrySeparator())
80             }
81             exerciseBlock.forEach { plannedExerciseBlock ->
82                 add(plannedExerciseBlockFormatter.formatBlock(plannedExerciseBlock))
83                 addAll(
84                     plannedExerciseBlockFormatter.formatBlockDetails(
85                         plannedExerciseBlock, unitPreferences))
86                 add(ItemDataEntrySeparator())
87             }
88         }
89     }
90 }
91