1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.customization.tool.ui.panel.header
18 
19 import android.view.View
20 import android.widget.AdapterView
21 import android.widget.ArrayAdapter
22 import android.widget.Spinner
23 import android.widget.TextView
24 import com.android.car.customization.tool.R
25 import com.android.car.customization.tool.domain.Action
26 import com.android.car.customization.tool.domain.panel.PanelHeaderItem
27 
28 internal class PanelHeaderDropDownViewHolder(view: View) : PanelHeaderItemViewHolder(view) {
29 
30     private val dropDownText = itemView.requireViewById<TextView>(R.id.panel_header_dropdown_text)
31     private val dropDown = itemView.requireViewById<Spinner>(R.id.panel_header_dropdown)
32 
bindTonull33     override fun bindTo(model: PanelHeaderItem, handleAction: (Action) -> Unit) {
34         model as PanelHeaderItem.DropDown
35 
36         dropDownText.text = model.text
37         val adapter = ArrayAdapter(
38             dropDown.context,
39             R.layout.vh_panel_header_dropdown_item_selected,
40             model.items.map { it.text }
41         )
42         adapter.setDropDownViewResource(R.layout.vh_panel_header_dropdown_item)
43         dropDown.adapter = adapter
44         dropDown.setSelection(model.items.indexOfFirst { it.isActive })
45 
46         dropDown.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
47             override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
48                 handleAction(model.items[position].action)
49             }
50 
51             override fun onNothingSelected(parent: AdapterView<*>?) {
52             }
53         }
54     }
55 }
56