1 /* <lambda>null2 * Copyright (C) 2023 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.widget.floatingsheetcontent 17 18 import android.view.LayoutInflater 19 import android.view.View 20 import android.view.ViewGroup 21 import android.widget.Switch 22 import android.widget.TextView 23 import com.android.internal.widget.RecyclerView 24 import com.android.wallpaper.R 25 import com.android.wallpaper.model.WallpaperAction 26 27 /** 28 * This class adapts the [WallpaperActionToggle] model to the WallpaperActionSelectionBottomSheet's 29 * recycler view. 30 */ 31 class WallpaperActionsToggleAdapter( 32 private val actionToggles: List<WallpaperAction>, 33 private val wallpaperEffectSwitchListener: WallpaperEffectSwitchListener 34 ) : RecyclerView.Adapter<WallpaperActionsToggleAdapter.ViewHolder>() { 35 36 class ViewHolder(v: View) : RecyclerView.ViewHolder(v) { 37 val textView: TextView 38 val switchView: Switch 39 init { 40 textView = v.requireViewById(R.id.wallpaper_action_switch_label) 41 switchView = v.requireViewById(R.id.wallpaper_action_switch) 42 } 43 } 44 45 override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder { 46 val v = 47 LayoutInflater.from(viewGroup.context) 48 .inflate(R.layout.wallpaper_action_toggle, viewGroup, false) 49 50 return ViewHolder(v) 51 } 52 53 // Replace the contents of a view (invoked by the layout manager). 54 override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { 55 viewHolder.textView.text = actionToggles[position].label 56 viewHolder.switchView.setOnCheckedChangeListener(null) 57 viewHolder.switchView.isChecked = actionToggles[position].toggled 58 59 viewHolder.switchView.setOnCheckedChangeListener { _, isChecked -> 60 if (isChecked) { 61 // Notify all observers that a switch has been flipped 62 wallpaperEffectSwitchListener.onEffectSwitchChanged(position) 63 } else { 64 // Initiate query to disable all effects on this wallpaper 65 wallpaperEffectSwitchListener.onEffectSwitchChanged(-1) 66 } 67 notifyDataSetChanged() 68 } 69 } 70 71 override fun getItemCount() = actionToggles.size 72 73 companion object { 74 private val TAG = "WallpaperActionsToggleAdapter" 75 } 76 77 interface WallpaperEffectSwitchListener { 78 /** 79 * This is called when a wallpaper effect toggle switch has flipped 80 * 81 * @param checkedItem is the index of the item that is checked and -1 if nothing is checked 82 */ 83 fun onEffectSwitchChanged(checkedItem: Int) 84 } 85 } 86