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.items
18 
19 import android.view.LayoutInflater
20 import android.view.ViewGroup
21 import androidx.recyclerview.widget.DiffUtil
22 import androidx.recyclerview.widget.ListAdapter
23 import com.android.car.customization.tool.R
24 import com.android.car.customization.tool.domain.Action
25 import com.android.car.customization.tool.domain.panel.PanelItem
26 
27 /**
28  * Adapter for the Panel RecyclerView.
29  */
30 internal class PanelItemsAdapter(
31     private val handleAction: (Action) -> Unit,
32 ) : ListAdapter<PanelItem, PanelItemViewHolder>(ItemCallback()) {
33 
onCreateViewHoldernull34     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PanelItemViewHolder {
35         val inflater = LayoutInflater.from(parent.context)
36         return when (ViewType.from(viewType)) {
37             ViewType.SECTION_TITLE -> {
38                 PanelSectionTitleItemViewHolder(
39                     inflater.inflate(
40                         R.layout.vh_panel_section_title,
41                         parent,
42                         /*attachToRoot=*/false
43                     )
44                 )
45             }
46 
47             ViewType.SWITCH -> {
48                 PanelSwitchViewHolder(
49                     inflater.inflate(
50                         R.layout.vh_panel_switch,
51                         parent,
52                         /*attachToRoot=*/false
53                     )
54                 )
55             }
56         }
57     }
58 
onBindViewHoldernull59     override fun onBindViewHolder(viewHolder: PanelItemViewHolder, position: Int) {
60         viewHolder.bindTo(getItem(position), handleAction)
61     }
62 
getItemViewTypenull63     override fun getItemViewType(position: Int): Int {
64         return when (getItem(position)) {
65             is PanelItem.SectionTitle -> ViewType.SECTION_TITLE.ordinal
66             is PanelItem.Switch -> ViewType.SWITCH.ordinal
67         }
68     }
69 
70     /**
71      * Defines which types of panel items are supported by this Adapter.
72      */
73     enum class ViewType {
74         SECTION_TITLE, SWITCH;
75 
76         companion object {
fromnull77             fun from(index: Int): ViewType {
78                 if (index >= values().size) {
79                     throw IllegalArgumentException("Type not supported")
80                 }
81                 return values()[index]
82             }
83         }
84     }
85 
86     class ItemCallback : DiffUtil.ItemCallback<PanelItem>() {
87 
areItemsTheSamenull88         override fun areItemsTheSame(p0: PanelItem, p1: PanelItem): Boolean {
89             return p0::class == p1::class
90         }
91 
areContentsTheSamenull92         override fun areContentsTheSame(p0: PanelItem, p1: PanelItem): Boolean {
93             return p0 == p1
94         }
95     }
96 }
97