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 package com.android.wallpaper.picker.preview.ui.view.adapters
17 
18 import android.view.LayoutInflater
19 import android.view.View
20 import android.view.ViewGroup
21 import androidx.recyclerview.widget.RecyclerView
22 import androidx.viewpager.widget.PagerAdapter
23 import com.android.wallpaper.R
24 
25 /** This class provides the dual preview views for the small preview screen on foldable devices */
26 class DualPreviewPagerAdapter(
27     val onBindViewHolder: (View, Int) -> Unit,
28 ) : PagerAdapter() {
29 
30     inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
31 
isViewFromObjectnull32     override fun isViewFromObject(item: View, `object`: Any): Boolean {
33         return item == `object`
34     }
35 
instantiateItemnull36     override fun instantiateItem(container: ViewGroup, position: Int): Any {
37         val view =
38             LayoutInflater.from(container.context)
39                 .inflate(R.layout.small_preview_foldable_card_view, container, false)
40 
41         onBindViewHolder.invoke(view, position)
42         container.addView(view)
43         return view
44     }
45 
destroyItemnull46     override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
47         container.removeView(`object` as View)
48     }
49 
getCountnull50     override fun getCount(): Int {
51         return PREVIEW_PAGER_ITEM_COUNT
52     }
53 
54     companion object {
55         const val PREVIEW_PAGER_ITEM_COUNT = 2
56     }
57 }
58