1 /*
2  * Copyright (C) 2020 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.systemui.controls.management
18 
19 import android.view.LayoutInflater
20 import android.view.View
21 import android.view.ViewGroup
22 import androidx.recyclerview.widget.GridLayoutManager
23 import androidx.recyclerview.widget.RecyclerView
24 import com.android.systemui.R
25 
26 class StructureAdapter(
27     private val models: List<StructureContainer>
28 ) : RecyclerView.Adapter<StructureAdapter.StructureHolder>() {
29 
onCreateViewHoldernull30     override fun onCreateViewHolder(parent: ViewGroup, p1: Int): StructureHolder {
31         val layoutInflater = LayoutInflater.from(parent.context)
32         return StructureHolder(
33             layoutInflater.inflate(R.layout.controls_structure_page, parent, false)
34         )
35     }
36 
getItemCountnull37     override fun getItemCount() = models.size
38 
39     override fun onBindViewHolder(holder: StructureHolder, index: Int) {
40         holder.bind(models[index].model)
41     }
42 
43     class StructureHolder(view: View) : RecyclerView.ViewHolder(view) {
44 
45         private val recyclerView: RecyclerView
46         private val controlAdapter: ControlAdapter
47 
48         init {
49             recyclerView = itemView.requireViewById<RecyclerView>(R.id.listAll)
50             val elevation = itemView.context.resources.getFloat(R.dimen.control_card_elevation)
51             controlAdapter = ControlAdapter(elevation)
52             setUpRecyclerView()
53         }
54 
bindnull55         fun bind(model: ControlsModel) {
56             controlAdapter.changeModel(model)
57         }
58 
setUpRecyclerViewnull59         private fun setUpRecyclerView() {
60             val margin = itemView.context.resources
61                 .getDimensionPixelSize(R.dimen.controls_card_margin)
62             val itemDecorator = MarginItemDecorator(margin, margin)
63 
64             recyclerView.apply {
65                 this.adapter = controlAdapter
66                 layoutManager = GridLayoutManager(recyclerView.context, 2).apply {
67                     spanSizeLookup = controlAdapter.spanSizeLookup
68                 }
69                 addItemDecoration(itemDecorator)
70             }
71         }
72     }
73 }