1 /*
2  * Copyright (C) 2023 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 
18 package com.android.customization.picker.color.ui.adapter
19 
20 import android.view.LayoutInflater
21 import android.view.View
22 import android.view.ViewGroup
23 import android.widget.TextView
24 import androidx.recyclerview.widget.RecyclerView
25 import com.android.customization.picker.color.ui.viewmodel.ColorTypeTabViewModel
26 import com.android.themepicker.R
27 
28 /** Adapts between color type items and views. */
29 class ColorTypeTabAdapter : RecyclerView.Adapter<ColorTypeTabAdapter.ViewHolder>() {
30 
31     private val items = mutableListOf<ColorTypeTabViewModel>()
32 
setItemsnull33     fun setItems(items: List<ColorTypeTabViewModel>) {
34         this.items.clear()
35         this.items.addAll(items)
36         notifyDataSetChanged()
37     }
38 
getItemCountnull39     override fun getItemCount(): Int {
40         return items.size
41     }
42 
onCreateViewHoldernull43     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
44         return ViewHolder(
45             LayoutInflater.from(parent.context)
46                 .inflate(
47                     R.layout.picker_fragment_tab,
48                     parent,
49                     false,
50                 )
51         )
52     }
53 
onBindViewHoldernull54     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
55         val item = items[position]
56         holder.itemView.isSelected = item.isSelected
57         holder.textView.text = item.name
58         holder.itemView.setOnClickListener(
59             if (item.onClick != null) {
60                 View.OnClickListener { item.onClick.invoke() }
61             } else {
62                 null
63             }
64         )
65     }
66 
67     class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
68         val textView: TextView = itemView.requireViewById(R.id.text)
69     }
70 }
71