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