1 /* <lambda>null2 * Copyright (C) 2025 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.customization.ui.binder 18 19 import android.content.res.ColorStateList 20 import androidx.lifecycle.LifecycleOwner 21 import com.android.wallpaper.picker.customization.ui.binder.ColorUpdateBinder 22 import com.android.wallpaper.picker.customization.ui.viewmodel.ColorUpdateViewModel 23 import com.google.android.material.materialswitch.MaterialSwitch 24 25 object SwitchColorBinder { 26 27 private const val COLOR_TRANSPARENT = 0 28 29 interface Binding { 30 /** Destroys the binding in spite of lifecycle state. */ 31 fun destroy() 32 } 33 34 /** 35 * Binds the color of a [MaterialSwitch] using [ColorUpdateBinder] according to Material 3 36 * specs. 37 */ 38 fun bind( 39 switch: MaterialSwitch, 40 isChecked: Boolean, 41 colorUpdateViewModel: ColorUpdateViewModel, 42 shouldAnimateColor: () -> Boolean, 43 lifecycleOwner: LifecycleOwner, 44 ): Binding { 45 val bindingThumb: ColorUpdateBinder.Binding 46 val bindingTrack: ColorUpdateBinder.Binding 47 if (isChecked) { 48 switch.trackDecorationTintList = ColorStateList.valueOf(COLOR_TRANSPARENT) 49 bindingThumb = 50 ColorUpdateBinder.bind( 51 setColor = { color -> switch.thumbTintList = ColorStateList.valueOf(color) }, 52 color = colorUpdateViewModel.colorOnPrimary, 53 shouldAnimate = shouldAnimateColor, 54 lifecycleOwner = lifecycleOwner, 55 ) 56 bindingTrack = 57 ColorUpdateBinder.bind( 58 setColor = { color -> switch.trackTintList = ColorStateList.valueOf(color) }, 59 color = colorUpdateViewModel.colorPrimary, 60 shouldAnimate = shouldAnimateColor, 61 lifecycleOwner = lifecycleOwner, 62 ) 63 } else { 64 bindingThumb = 65 ColorUpdateBinder.bind( 66 setColor = { color -> 67 switch.thumbTintList = ColorStateList.valueOf(color) 68 switch.trackDecorationTintList = ColorStateList.valueOf(color) 69 }, 70 color = colorUpdateViewModel.colorOutline, 71 shouldAnimate = shouldAnimateColor, 72 lifecycleOwner = lifecycleOwner, 73 ) 74 bindingTrack = 75 ColorUpdateBinder.bind( 76 setColor = { color -> switch.trackTintList = ColorStateList.valueOf(color) }, 77 color = colorUpdateViewModel.colorSurfaceContainerHighest, 78 shouldAnimate = shouldAnimateColor, 79 lifecycleOwner = lifecycleOwner, 80 ) 81 } 82 return object : Binding { 83 override fun destroy() { 84 bindingThumb.destroy() 85 bindingTrack.destroy() 86 } 87 } 88 } 89 } 90