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 import com.android.wallpaper.config.BaseFlags
25 
26 /** This class provides the dual preview views for the small preview screen on foldable devices */
27 class DualPreviewPagerAdapter(
28     val onBindViewHolder: (View, Int) -> Unit,
29 ) : PagerAdapter() {
30 
31     inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
32 
isViewFromObjectnull33     override fun isViewFromObject(item: View, `object`: Any): Boolean {
34         return item == `object`
35     }
36 
instantiateItemnull37     override fun instantiateItem(container: ViewGroup, position: Int): Any {
38         if (BaseFlags.get().isNewPickerUi()) {
39             val view =
40                 LayoutInflater.from(container.context)
41                     .inflate(R.layout.small_preview_foldable_card_view2, container, false)
42             onBindViewHolder.invoke(view, position)
43             container.addView(
44                 view,
45                 ViewGroup.LayoutParams(
46                     ViewGroup.LayoutParams.MATCH_PARENT,
47                     ViewGroup.LayoutParams.MATCH_PARENT
48                 ),
49             )
50             return view
51         } else {
52             val view =
53                 LayoutInflater.from(container.context)
54                     .inflate(R.layout.small_preview_foldable_card_view, container, false)
55             onBindViewHolder.invoke(view, position)
56             container.addView(view)
57             return view
58         }
59     }
60 
destroyItemnull61     override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
62         container.removeView(`object` as View)
63     }
64 
getCountnull65     override fun getCount(): Int {
66         return PREVIEW_PAGER_ITEM_COUNT
67     }
68 
69     companion object {
70         const val PREVIEW_PAGER_ITEM_COUNT = 2
71     }
72 }
73