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.SleepSessionEntry
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 SleepSessionEntry. */
33 class SleepSessionItemViewBinder(
34     private val showSecondAction: Boolean = true,
35     private val onItemClickedListener: OnClickEntryListener?,
36     private val onDeleteEntryListenerClicked: OnDeleteEntryListener?,
37 ) : ViewBinder<SleepSessionEntry, 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_sleep_session_entry, parent, false)
49     }
50 
bindnull51     override fun bind(view: View, data: SleepSessionEntry, 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 notes = view.findViewById<TextView>(R.id.item_data_entry_notes)
57         val deleteButton = view.findViewById<ImageButton>(R.id.item_data_entry_delete)
58         logger.logImpression(DataEntriesElement.SLEEP_SESSION_ENTRY_BUTTON)
59         logger.logImpression(DataEntriesElement.DATA_ENTRY_DELETE_BUTTON)
60 
61         title.text = data.title
62         title.contentDescription = data.titleA11y
63         header.text = data.header
64         header.contentDescription = data.headerA11y
65         notes.isVisible = !data.notes.isNullOrBlank()
66         notes.text = data.notes
67         deleteButton.isVisible = showSecondAction
68         divider.isVisible = showSecondAction
69 
70         deleteButton.setOnClickListener {
71             logger.logInteraction(DataEntriesElement.DATA_ENTRY_DELETE_BUTTON)
72             onDeleteEntryListenerClicked?.onDeleteEntry(data.uuid, data.dataType, index)
73         }
74         deleteButton.contentDescription =
75             view.resources.getString(
76                 R.string.data_point_action_content_description, data.headerA11y)
77         if (showSecondAction) {
78             container.setOnClickListener {
79                 logger.logInteraction(DataEntriesElement.SLEEP_SESSION_ENTRY_BUTTON)
80                 onItemClickedListener?.onItemClicked(data.uuid, index)
81             }
82         } else {
83             container.isClickable = false
84         }
85     }
86 }
87