• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.adapter
18 
19 import android.view.LayoutInflater
20 import android.view.View
21 import android.view.ViewGroup
22 import androidx.recyclerview.widget.RecyclerView
23 import com.android.wallpaper.R
24 import com.android.wallpaper.picker.category.ui.view.viewholder.CuratedPhotoHolder
25 import com.android.wallpaper.picker.category.ui.viewmodel.TileViewModel
26 
27 /** Custom adaptor for curated photos carousel in the categories page. */
28 class CuratedPhotosAdapter(val items: List<TileViewModel>) :
29     RecyclerView.Adapter<RecyclerView.ViewHolder>() {
30     private var visiblePosition = -1 // Track the position of the visible TextView
31 
onCreateViewHoldernull32     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
33         return createIndividualHolder(parent)
34     }
35 
getItemCountnull36     override fun getItemCount(): Int {
37         return items.size
38     }
39 
onBindViewHoldernull40     override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
41         val tile: TileViewModel = items[position]
42         (holder as CuratedPhotoHolder?)?.bind(
43             tile,
44             holder.itemView.context,
45             this.visiblePosition == position,
46         )
47     }
48 
createIndividualHoldernull49     private fun createIndividualHolder(parent: ViewGroup): RecyclerView.ViewHolder {
50         val layoutInflater = LayoutInflater.from(parent.context)
51         val view: View = layoutInflater.inflate(R.layout.curated_photo_tile, parent, false)
52         return CuratedPhotoHolder(view)
53     }
54 
setVisiblePositionnull55     fun setVisiblePosition(position: Int) {
56         val previousPosition = this.visiblePosition
57         this.visiblePosition = position
58         if (previousPosition != -1) {
59             notifyItemChanged(previousPosition)
60         }
61         notifyItemChanged(position)
62     }
63 }
64