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.res.R
25 
26 class StructureAdapter(
27     private val models: List<StructureContainer>,
28     private val currentUserId: Int,
29 ) : RecyclerView.Adapter<StructureAdapter.StructureHolder>() {
30 
onCreateViewHoldernull31     override fun onCreateViewHolder(parent: ViewGroup, p1: Int): StructureHolder {
32         val layoutInflater = LayoutInflater.from(parent.context)
33         return StructureHolder(
34             layoutInflater.inflate(R.layout.controls_structure_page, parent, false),
35             currentUserId,
36         )
37     }
38 
getItemCountnull39     override fun getItemCount() = models.size
40 
41     override fun onBindViewHolder(holder: StructureHolder, index: Int) {
42         holder.bind(models[index].model)
43     }
44 
45     class StructureHolder(view: View, currentUserId: Int) :
46             RecyclerView.ViewHolder(view) {
47 
48         private val recyclerView: RecyclerView
49         private val controlAdapter: ControlAdapter
50 
<lambda>null51         init {
52             recyclerView = itemView.requireViewById<RecyclerView>(R.id.listAll)
53             val elevation = itemView.context.resources.getFloat(R.dimen.control_card_elevation)
54             controlAdapter = ControlAdapter(elevation, currentUserId)
55             setUpRecyclerView()
56         }
57 
bindnull58         fun bind(model: ControlsModel) {
59             controlAdapter.changeModel(model)
60         }
61 
setUpRecyclerViewnull62         private fun setUpRecyclerView() {
63             val margin = itemView.context.resources
64                 .getDimensionPixelSize(R.dimen.controls_card_margin)
65             val itemDecorator = MarginItemDecorator(margin, margin)
66             val spanCount = ControlAdapter.findMaxColumns(itemView.resources)
67 
68             recyclerView.apply {
69                 this.adapter = controlAdapter
70                 layoutManager = GridLayoutManager(recyclerView.context, spanCount).apply {
71                     spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
72                         override fun getSpanSize(position: Int): Int {
73                             return if (adapter?.getItemViewType(position)
74                                     != ControlAdapter.TYPE_CONTROL) spanCount else 1
75                         }
76                     }
77                 }
78                 addItemDecoration(itemDecorator)
79             }
80         }
81     }
82 }