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.wallpaper.picker.category.ui.view.viewholder
18 
19 import android.graphics.Rect
20 import android.view.View
21 import android.widget.TextView
22 import androidx.recyclerview.widget.RecyclerView
23 import com.android.wallpaper.R
24 import com.android.wallpaper.picker.category.ui.view.adapter.CategoryAdapter
25 import com.android.wallpaper.picker.category.ui.viewmodel.SectionViewModel
26 import com.google.android.flexbox.AlignItems
27 import com.google.android.flexbox.FlexDirection
28 import com.google.android.flexbox.FlexWrap
29 import com.google.android.flexbox.FlexboxLayoutManager
30 import com.google.android.flexbox.JustifyContent
31 
32 /** This view holder caches reference to pertinent views in a [CategorySectionView] */
33 class CategorySectionViewHolder(itemView: View, val windowWidth: Int) :
34     RecyclerView.ViewHolder(itemView) {
35 
36     // recycler view for the tiles
37     private var sectionTiles: RecyclerView
38 
39     // title for the section
40     private var sectionTitle: TextView
41 
42     init {
43         sectionTiles = itemView.requireViewById(R.id.category_wallpaper_tiles)
44         sectionTitle = itemView.requireViewById(R.id.section_title)
45     }
46 
bindnull47     fun bind(item: SectionViewModel) {
48         // TODO: this probably is not necessary but if in the case the sections get updated we
49         //  should just update the adapter instead of instantiating a new instance
50         sectionTiles.adapter = CategoryAdapter(item.tileViewModels, item.columnCount, windowWidth)
51 
52         val layoutManager = FlexboxLayoutManager(itemView.context)
53 
54         // Horizontal orientation
55         layoutManager.flexDirection = FlexDirection.ROW
56 
57         // disable wrapping to make sure everything fits on a single row
58         layoutManager.flexWrap = FlexWrap.NOWRAP
59 
60         // Stretch items to fill the horizontal axis
61         layoutManager.alignItems = AlignItems.STRETCH
62 
63         // Distribute items evenly on the horizontal axis
64         layoutManager.justifyContent = JustifyContent.SPACE_AROUND
65 
66         sectionTiles.layoutManager = layoutManager as RecyclerView.LayoutManager?
67 
68         val itemDecoration =
69             HorizontalSpaceItemDecoration(
70                 itemView.context.resources
71                     .getDimension(R.dimen.creative_category_grid_padding_horizontal)
72                     .toInt()
73             )
74         sectionTiles.addItemDecoration(itemDecoration)
75 
76         if (item.sectionTitle != null) {
77             sectionTitle.text = item.sectionTitle
78             sectionTitle.visibility = View.VISIBLE
79         } else {
80             sectionTitle.visibility = View.GONE
81         }
82     }
83 
84     class HorizontalSpaceItemDecoration(private val horizontalSpace: Int) :
85         RecyclerView.ItemDecoration() {
86 
getItemOffsetsnull87         override fun getItemOffsets(
88             outRect: Rect,
89             view: View,
90             parent: RecyclerView,
91             state: RecyclerView.State
92         ) {
93             if (parent.getChildAdapterPosition(view) != 0) {
94                 outRect.left = horizontalSpace
95             }
96         }
97     }
98 }
99