• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.slider.Slider
24 
25 object SliderColorBinder {
26 
27     interface Binding {
28         /** Destroys the binding in spite of lifecycle state. */
29         fun destroy()
30     }
31 
32     /** Binds the color of a [Slider] using [ColorUpdateBinder] according to Material 3 specs. */
33     fun bind(
34         slider: Slider,
35         colorUpdateViewModel: ColorUpdateViewModel,
36         shouldAnimateColor: () -> Boolean,
37         lifecycleOwner: LifecycleOwner,
38     ): Binding {
39         val bindingPrimary =
40             ColorUpdateBinder.bind(
41                 setColor = { color ->
42                     slider.apply {
43                         trackActiveTintList = ColorStateList.valueOf(color)
44                         thumbTintList = ColorStateList.valueOf(color)
45                         tickInactiveTintList = ColorStateList.valueOf(color)
46                     }
47                 },
48                 color = colorUpdateViewModel.colorPrimary,
49                 shouldAnimate = shouldAnimateColor,
50                 lifecycleOwner = lifecycleOwner,
51             )
52         val bindingSurfaceContainerHighest =
53             ColorUpdateBinder.bind(
54                 setColor = { color ->
55                     slider.apply {
56                         trackInactiveTintList = ColorStateList.valueOf(color)
57                         tickActiveTintList = ColorStateList.valueOf(color)
58                     }
59                 },
60                 color = colorUpdateViewModel.colorSurfaceContainerHighest,
61                 shouldAnimate = shouldAnimateColor,
62                 lifecycleOwner = lifecycleOwner,
63             )
64         return object : Binding {
65             override fun destroy() {
66                 bindingPrimary.destroy()
67                 bindingSurfaceContainerHighest.destroy()
68             }
69         }
70     }
71 }
72