1 /**
2  * 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
17 
18 import android.view.LayoutInflater
19 import android.view.View
20 import android.view.ViewGroup
21 import android.widget.ImageButton
22 import android.widget.LinearLayout
23 import android.widget.TextView
24 import androidx.core.view.isVisible
25 import com.android.healthconnect.controller.R
26 import com.android.healthconnect.controller.data.entries.FormattedEntry
27 import com.android.healthconnect.controller.shared.recyclerview.ViewBinder
28 import com.android.healthconnect.controller.utils.logging.DataEntriesElement
29 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger
30 import com.android.healthconnect.controller.utils.logging.HealthConnectLoggerEntryPoint
31 import dagger.hilt.android.EntryPointAccessors
32 
33 /** ViewBinder for PlannedExerciseSessionEntry. */
34 class PlannedExerciseSessionItemViewBinder(
35     private val showSecondAction: Boolean = true,
36     private val onItemClickedListener: OnClickEntryListener?,
37     private val onDeleteEntryClicked: OnDeleteEntryListener?,
38 ) : ViewBinder<FormattedEntry.PlannedExerciseSessionEntry, View> {
39     private lateinit var logger: HealthConnectLogger
40 
newViewnull41     override fun newView(parent: ViewGroup): View {
42         val context = parent.context.applicationContext
43         val hiltEntryPoint =
44             EntryPointAccessors.fromApplication(context, HealthConnectLoggerEntryPoint::class.java)
45         logger = hiltEntryPoint.logger()
46         return LayoutInflater.from(parent.context).inflate(R.layout.item_data_entry, parent, false)
47     }
48 
bindnull49     override fun bind(view: View, data: FormattedEntry.PlannedExerciseSessionEntry, index: Int) {
50         // TODO(b/332538555) Add implementation for telemetry
51         val container = view.findViewById<LinearLayout>(R.id.item_data_entry_container)
52         val header = view.findViewById<TextView>(R.id.item_data_entry_header)
53         val title = view.findViewById<TextView>(R.id.item_data_entry_title)
54         val deleteButton = view.findViewById<ImageButton>(R.id.item_data_entry_delete)
55         logger.logImpression(DataEntriesElement.PLANNED_EXERCISE_SESSION_ENTRY_BUTTON)
56         if (showSecondAction) {
57             logger.logImpression(DataEntriesElement.DATA_ENTRY_DELETE_BUTTON)
58         }
59         title.text = data.title
60         title.contentDescription = data.titleA11y
61         header.text = data.header
62         header.contentDescription = data.headerA11y
63         deleteButton.isVisible = showSecondAction
64 
65         deleteButton.contentDescription =
66             view.resources.getString(
67                 R.string.data_point_action_content_description, data.headerA11y)
68         deleteButton.setOnClickListener {
69             logger.logInteraction(DataEntriesElement.DATA_ENTRY_DELETE_BUTTON)
70             onDeleteEntryClicked?.onDeleteEntry(data.uuid, data.dataType, index)
71         }
72         if (showSecondAction) {
73             container.setOnClickListener {
74                 logger.logInteraction(DataEntriesElement.PLANNED_EXERCISE_SESSION_ENTRY_BUTTON)
75                 onItemClickedListener?.onItemClicked(data.uuid, index)
76             }
77         } else {
78             container.isClickable = false
79         }
80     }
81 }
82