• 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.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 dagger.hilt.android.qualifiers.ApplicationContext
30 import javax.inject.Inject
31 
32 /** Formatter for printing PlannedExerciseSessionRecord data. */
33 class PlannedExerciseSessionRecordFormatter
34 @Inject
35 constructor(
36     @ApplicationContext private val context: Context,
37     private val plannedExerciseBlockFormatter: PlannedExerciseBlockFormatter,
38 ) :
39     BaseFormatter<PlannedExerciseSessionRecord>(context),
40     RecordDetailsFormatter<PlannedExerciseSessionRecord> {
41 
42     override suspend fun formatRecord(
43         record: PlannedExerciseSessionRecord,
44         header: String,
45         headerA11y: String,
46         unitPreferences: UnitPreferences,
47     ): FormattedEntry {
48         return FormattedEntry.PlannedExerciseSessionEntry(
49             uuid = record.metadata.id,
50             header = header,
51             headerA11y = headerA11y,
52             title = formatTitle(record),
53             titleA11y = formatTitle(record),
54             notes = getNotes(record),
55             dataType = PlannedExerciseSessionRecord::class,
56         )
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 ?: context.getString(R.string.unknown_type),
64         )
65     }
66 
67     private fun getNotes(record: PlannedExerciseSessionRecord): String? {
68         return record.notes?.toString()
69     }
70 
71     override suspend fun formatRecordDetails(
72         record: PlannedExerciseSessionRecord
73     ): List<FormattedEntry> {
74         val exerciseBlock = record.blocks
75         return buildList {
76             if (!record.notes.isNullOrBlank()) {
77                 add(ItemDataEntrySeparator())
78                 add(SessionHeader(context.getString(R.string.planned_exercise_session_notes_title)))
79                 add(FormattedSectionContent(record.notes.toString()))
80                 add(ItemDataEntrySeparator())
81             }
82             exerciseBlock.forEach { plannedExerciseBlock ->
83                 add(plannedExerciseBlockFormatter.formatBlock(plannedExerciseBlock))
84                 addAll(
85                     plannedExerciseBlockFormatter.formatBlockDetails(
86                         plannedExerciseBlock,
87                         unitPreferences,
88                     )
89                 )
90                 add(ItemDataEntrySeparator())
91             }
92         }
93     }
94 }
95