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 package com.android.car.customization.tool.ui.panel.header
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.PanelHeaderItem
26 
27 /**
28  * Adapter for the Panel RecyclerView.
29  */
30 internal class PanelHeaderAdapter(
31     private val handleAction: (Action) -> Unit,
32 ) : ListAdapter<PanelHeaderItem, PanelHeaderItemViewHolder>(ItemCallback()) {
33 
onCreateViewHoldernull34     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PanelHeaderItemViewHolder {
35         val inflater = LayoutInflater.from(parent.context)
36         return when (ViewType.from(viewType)) {
37             ViewType.HEADER_CLOSE -> {
38                 PanelHeaderCloseViewHolder(
39                     inflater.inflate(
40                         R.layout.vh_panel_header_close,
41                         parent,
42                         /*attachToRoot=*/false
43                     )
44                 )
45             }
46 
47             ViewType.HEADER_SEARCH -> {
48                 PanelHeaderSearchBoxViewHolder(
49                     inflater.inflate(
50                         R.layout.vh_panel_header_search,
51                         parent,
52                         /*attachToRoot=*/false
53                     )
54                 )
55             }
56 
57             ViewType.HEADER_DROPDOWN -> PanelHeaderDropDownViewHolder(
58                 inflater.inflate(
59                     R.layout.vh_panel_header_dropdown,
60                     parent,
61                     /*attachToRoot=*/false
62                 )
63             )
64         }
65     }
66 
onBindViewHoldernull67     override fun onBindViewHolder(viewHolder: PanelHeaderItemViewHolder, position: Int) {
68         viewHolder.bindTo(getItem(position), handleAction)
69     }
70 
getItemViewTypenull71     override fun getItemViewType(position: Int): Int {
72         return when (getItem(position)) {
73             is PanelHeaderItem.SearchBox -> ViewType.HEADER_SEARCH.ordinal
74             is PanelHeaderItem.CloseButton -> ViewType.HEADER_CLOSE.ordinal
75             is PanelHeaderItem.DropDown -> ViewType.HEADER_DROPDOWN.ordinal
76         }
77     }
78 
79     /**
80      * Defines which types of panel items are supported by this Adapter.
81      */
82     enum class ViewType {
83         HEADER_SEARCH, HEADER_CLOSE, HEADER_DROPDOWN;
84 
85         companion object {
fromnull86             fun from(index: Int): ViewType {
87                 if (index >= values().size) {
88                     throw IllegalArgumentException("Type not supported")
89                 }
90                 return values()[index]
91             }
92         }
93     }
94 
95     class ItemCallback : DiffUtil.ItemCallback<PanelHeaderItem>() {
96 
areItemsTheSamenull97         override fun areItemsTheSame(p0: PanelHeaderItem, p1: PanelHeaderItem): Boolean {
98             return p0::class == p1::class
99         }
100 
areContentsTheSamenull101         override fun areContentsTheSame(p0: PanelHeaderItem, p1: PanelHeaderItem): Boolean {
102             return p0 == p1
103         }
104     }
105 }
106