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.TextView
22 import androidx.core.view.isVisible
23 import com.android.healthconnect.controller.R
24 import com.android.healthconnect.controller.data.entries.FormattedEntry.ExerciseSessionEntry
25 import com.android.healthconnect.controller.shared.RoundView
26 import com.android.healthconnect.controller.shared.map.MapView
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 ExerciseSessionEntry. */
34 class ExerciseSessionItemViewBinder(
35     private val showSecondAction: Boolean = true,
36     private val onItemClickedListener: OnClickEntryListener?,
37     private val onDeleteEntryClicked: OnDeleteEntryListener?,
38 ) : ViewBinder<ExerciseSessionEntry, View> {
39 
40     private lateinit var logger: HealthConnectLogger
41 
newViewnull42     override fun newView(parent: ViewGroup): View {
43         val context = parent.context.applicationContext
44         val hiltEntryPoint =
45             EntryPointAccessors.fromApplication(
46                 context.applicationContext, HealthConnectLoggerEntryPoint::class.java)
47         logger = hiltEntryPoint.logger()
48         return LayoutInflater.from(parent.context)
49             .inflate(R.layout.item_exercise_session_entry, parent, false)
50     }
51 
bindnull52     override fun bind(view: View, data: ExerciseSessionEntry, index: Int) {
53         val container = view.findViewById<LinearLayout>(R.id.item_data_entry_container)
54         val divider = view.findViewById<LinearLayout>(R.id.item_data_entry_divider)
55         val header = view.findViewById<TextView>(R.id.item_data_entry_header)
56         val title = view.findViewById<TextView>(R.id.item_data_entry_title)
57         val notes = view.findViewById<TextView>(R.id.item_data_entry_notes)
58         val deleteButton = view.findViewById<ImageButton>(R.id.item_data_entry_delete)
59         val mapView = view.findViewById<MapView>(R.id.map_view)
60         val mapContainer = view.findViewById<RoundView>(R.id.map_round_view)
61         logger.logImpression(DataEntriesElement.EXERCISE_SESSION_ENTRY_BUTTON)
62         if (showSecondAction) {
63             logger.logImpression(DataEntriesElement.DATA_ENTRY_DELETE_BUTTON)
64         }
65         title.text = data.title
66         title.contentDescription = data.titleA11y
67         header.text = data.header
68         header.contentDescription = data.headerA11y
69         notes.isVisible = !data.notes.isNullOrBlank()
70         notes.text = data.notes
71         deleteButton.isVisible = showSecondAction
72         divider.isVisible = showSecondAction
73         mapContainer.isVisible = (data.route != null)
74         if (data.route != null) {
75             mapView.setRoute(data.route)
76             logger.logImpression(DataEntriesElement.EXERCISE_SESSION_MAP_VIEW)
77         }
78 
79         deleteButton.contentDescription =
80             view.resources.getString(
81                 R.string.data_point_action_content_description, data.headerA11y)
82         deleteButton.setOnClickListener {
83             logger.logInteraction(DataEntriesElement.DATA_ENTRY_DELETE_BUTTON)
84             onDeleteEntryClicked?.onDeleteEntry(data.uuid, data.dataType, index)
85         }
86         if (showSecondAction) {
87             container.setOnClickListener {
88                 logger.logInteraction(DataEntriesElement.EXERCISE_SESSION_ENTRY_BUTTON)
89                 onItemClickedListener?.onItemClicked(data.uuid, index)
90             }
91         } else {
92             container.isClickable = false
93         }
94     }
95 }
96