1 /* <lambda>null2 * 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.content.Context 20 import android.graphics.Point 21 import android.view.View 22 import android.widget.ImageView 23 import android.widget.TextView 24 import androidx.cardview.widget.CardView 25 import androidx.recyclerview.widget.RecyclerView 26 import com.android.wallpaper.R 27 import com.android.wallpaper.picker.category.ui.view.SectionCardinality 28 import com.android.wallpaper.picker.category.ui.viewmodel.TileViewModel 29 import com.android.wallpaper.util.ResourceUtils 30 import com.android.wallpaper.util.SizeCalculator 31 32 /** Caches and binds [TileViewHolder] to a [WallpaperTileView] */ 33 class TileViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { 34 35 private var title: TextView 36 private var categorySubtitle: TextView 37 private var wallpaperCategoryImage: ImageView 38 private var categoryCardView: CardView 39 40 init { 41 title = itemView.requireViewById(R.id.tile_title) 42 categorySubtitle = itemView.requireViewById(R.id.category_title) 43 wallpaperCategoryImage = itemView.requireViewById(R.id.image) 44 categoryCardView = itemView.requireViewById(R.id.category) 45 } 46 47 fun bind( 48 item: TileViewModel, 49 context: Context, 50 columnCount: Int, 51 tileCount: Int, 52 windowWidth: Int 53 ) { 54 title.visibility = View.GONE 55 56 var tileSize: Point 57 var tileRadius: Int 58 // calculate the height 59 if (columnCount == 1 && tileCount == 1) { 60 // sections that take 1 column and have 1 tile 61 tileSize = SizeCalculator.getCategoryTileSize(itemView.context, windowWidth) 62 tileRadius = context.resources.getDimension(R.dimen.grid_item_all_radius_small).toInt() 63 } else if ( 64 columnCount > 1 && 65 tileCount == 1 && 66 item.maxCategoriesInRow == SectionCardinality.Single 67 ) { 68 // sections with more than 1 column and 1 tile 69 tileSize = SizeCalculator.getFeaturedCategoryTileSize(itemView.context, windowWidth) 70 tileRadius = tileSize.y 71 } else { 72 // sections witch take more than 1 column and have more than 1 tile 73 tileSize = SizeCalculator.getFeaturedCategoryTileSize(itemView.context, windowWidth) 74 tileSize.y /= 2 75 tileRadius = context.resources.getDimension(R.dimen.grid_item_all_radius).toInt() 76 } 77 78 wallpaperCategoryImage.getLayoutParams().height = tileSize.y 79 categoryCardView.radius = tileRadius.toFloat() 80 81 if (item.thumbnailAsset != null) { 82 val placeHolderColor = 83 ResourceUtils.getColorAttr(context, android.R.attr.colorSecondary) 84 item.thumbnailAsset.loadDrawable(context, wallpaperCategoryImage, placeHolderColor) 85 } else { 86 wallpaperCategoryImage.setImageDrawable(item.defaultDrawable) 87 wallpaperCategoryImage.setBackgroundColor( 88 context.resources.getColor(R.color.myphoto_background_color) 89 ) 90 } 91 categorySubtitle.text = item.text 92 93 // bind the tile action to the button 94 itemView.setOnClickListener { _ -> item.onClicked?.invoke() } 95 } 96 } 97