1 /**
2  * Copyright (C) 2023 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  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package com.android.healthconnect.controller.dataentries
15 
16 import android.view.LayoutInflater
17 import android.view.View
18 import android.view.ViewGroup
19 import android.widget.ImageButton
20 import android.widget.LinearLayout
21 import android.widget.RelativeLayout
22 import android.widget.TextView
23 import androidx.core.view.isVisible
24 import com.android.healthconnect.controller.R
25 import com.android.healthconnect.controller.data.entries.FormattedEntry.SeriesDataEntry
26 import com.android.healthconnect.controller.shared.recyclerview.ViewBinder
27 import com.android.healthconnect.controller.utils.logging.DataEntriesElement
28 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger
29 import com.android.healthconnect.controller.utils.logging.HealthConnectLoggerEntryPoint
30 import dagger.hilt.android.EntryPointAccessors
31 
32 /** ViewBinder for SeriesDataEntry. */
33 class SeriesDataItemViewBinder(
34     private val showSecondAction: Boolean = true,
35     private val onItemClickedListener: OnClickEntryListener?,
36     private val onDeleteEntryClicked: OnDeleteEntryListener?,
37 ) : ViewBinder<SeriesDataEntry, View> {
38 
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(
45                 context.applicationContext, HealthConnectLoggerEntryPoint::class.java)
46         logger = hiltEntryPoint.logger()
47         return LayoutInflater.from(parent.context)
48             .inflate(R.layout.item_series_data_entry, parent, false)
49     }
50 
bindnull51     override fun bind(view: View, data: SeriesDataEntry, index: Int) {
52         val container = view.findViewById<RelativeLayout>(R.id.item_data_entry_container)
53         val divider = view.findViewById<LinearLayout>(R.id.item_data_entry_divider)
54         val header = view.findViewById<TextView>(R.id.item_data_entry_header)
55         val title = view.findViewById<TextView>(R.id.item_data_entry_title)
56         val deleteButton = view.findViewById<ImageButton>(R.id.item_data_entry_delete)
57 
58         logger.logImpression(DataEntriesElement.DATA_ENTRY_VIEW)
59         logger.logImpression(DataEntriesElement.DATA_ENTRY_DELETE_BUTTON)
60         title.text = data.title
61         title.contentDescription = data.titleA11y
62         header.text = data.header
63         header.contentDescription = data.headerA11y
64         deleteButton.isVisible = showSecondAction
65         divider.isVisible = showSecondAction
66 
67         deleteButton.contentDescription =
68             view.resources.getString(
69                 R.string.data_point_action_content_description, data.headerA11y)
70         deleteButton.setOnClickListener {
71             logger.logInteraction(DataEntriesElement.DATA_ENTRY_DELETE_BUTTON)
72             onDeleteEntryClicked?.onDeleteEntry(data.uuid, data.dataType, index)
73         }
74         // if the delete button is not showing, we are in the Entry details screen and this item
75         // should not be clickable
76         if (showSecondAction) {
77             container.setOnClickListener {
78                 logger.logInteraction(DataEntriesElement.DATA_ENTRY_VIEW)
79                 onItemClickedListener?.onItemClicked(data.uuid, index)
80             }
81         } else {
82             container.isClickable = false
83         }
84     }
85 }
86