• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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 
17 package com.android.wallpaper.customization.ui.binder
18 
19 import android.widget.Button
20 import android.widget.FrameLayout
21 import android.widget.Toolbar
22 import androidx.core.graphics.ColorUtils
23 import androidx.core.graphics.drawable.DrawableCompat
24 import androidx.core.view.isInvisible
25 import androidx.lifecycle.Lifecycle
26 import androidx.lifecycle.LifecycleOwner
27 import androidx.lifecycle.lifecycleScope
28 import androidx.lifecycle.repeatOnLifecycle
29 import com.android.themepicker.R as ThemePickerR
30 import com.android.wallpaper.R
31 import com.android.wallpaper.customization.ui.util.ThemePickerCustomizationOptionUtil.ThemePickerHomeCustomizationOption.APP_SHAPE_GRID
32 import com.android.wallpaper.customization.ui.util.ThemePickerCustomizationOptionUtil.ThemePickerHomeCustomizationOption.COLORS
33 import com.android.wallpaper.customization.ui.util.ThemePickerCustomizationOptionUtil.ThemePickerLockCustomizationOption.CLOCK
34 import com.android.wallpaper.customization.ui.util.ThemePickerCustomizationOptionUtil.ThemePickerLockCustomizationOption.SHORTCUTS
35 import com.android.wallpaper.customization.ui.viewmodel.ThemePickerCustomizationOptionsViewModel
36 import com.android.wallpaper.picker.customization.ui.binder.ColorUpdateBinder
37 import com.android.wallpaper.picker.customization.ui.binder.DefaultToolbarBinder
38 import com.android.wallpaper.picker.customization.ui.binder.ToolbarBinder
39 import com.android.wallpaper.picker.customization.ui.viewmodel.ColorUpdateViewModel
40 import com.android.wallpaper.picker.customization.ui.viewmodel.CustomizationOptionsViewModel
41 import javax.inject.Inject
42 import javax.inject.Singleton
43 import kotlinx.coroutines.flow.combine
44 import kotlinx.coroutines.launch
45 
46 @Singleton
47 class ThemePickerToolbarBinder
48 @Inject
49 constructor(private val defaultToolbarBinder: DefaultToolbarBinder) : ToolbarBinder {
50 
51     override fun bind(
52         navButton: FrameLayout,
53         toolbar: Toolbar,
54         applyButton: Button,
55         viewModel: CustomizationOptionsViewModel,
56         colorUpdateViewModel: ColorUpdateViewModel,
57         lifecycleOwner: LifecycleOwner,
58         onNavBack: () -> Unit,
59     ) {
60         defaultToolbarBinder.bind(
61             navButton,
62             toolbar,
63             applyButton,
64             viewModel,
65             colorUpdateViewModel,
66             lifecycleOwner,
67             onNavBack,
68         )
69 
70         if (viewModel !is ThemePickerCustomizationOptionsViewModel) {
71             throw IllegalArgumentException(
72                 "viewModel $viewModel is not a ThemePickerCustomizationOptionsViewModel."
73             )
74         }
75 
76         ColorUpdateBinder.bind(
77             setColor = { color ->
78                 DrawableCompat.setTint(DrawableCompat.wrap(applyButton.background), color)
79             },
80             color = colorUpdateViewModel.colorPrimary,
81             shouldAnimate = { true },
82             lifecycleOwner = lifecycleOwner,
83         )
84 
85         ColorUpdateBinder.bind(
86             setColor = { color -> applyButton.setTextColor(color) },
87             color =
88                 combine(
89                     viewModel.isApplyButtonEnabled,
90                     colorUpdateViewModel.colorOnPrimary,
91                     colorUpdateViewModel.colorOnSurface,
92                 ) { enabled, onPrimary, onSurface ->
93                     if (enabled) {
94                         onPrimary
95                     } else {
96                         ColorUtils.setAlphaComponent(onSurface, 97) // 97 for 38% transparent
97                     }
98                 },
99             shouldAnimate = { false },
100             lifecycleOwner = lifecycleOwner,
101         )
102 
103         lifecycleOwner.lifecycleScope.launch {
104             lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
105                 launch {
106                     viewModel.onApplyButtonClicked.collect { onApplyButtonClicked ->
107                         applyButton.setOnClickListener { onApplyButtonClicked?.invoke(onNavBack) }
108                     }
109                 }
110 
111                 launch { viewModel.isApplyButtonVisible.collect { applyButton.isInvisible = !it } }
112 
113                 launch {
114                     viewModel.isApplyButtonEnabled.collect {
115                         applyButton.isEnabled = it
116                         applyButton.background.alpha =
117                             if (it) 255 else 31 // 255 for 100%, 31 for 12% transparent
118                     }
119                 }
120 
121                 launch {
122                     viewModel.selectedOption.collect {
123                         val stringResId =
124                             when (it) {
125                                 COLORS -> ThemePickerR.string.system_colors_title
126                                 APP_SHAPE_GRID -> ThemePickerR.string.shape_and_grid_title
127                                 CLOCK -> ThemePickerR.string.clock_title
128                                 SHORTCUTS ->
129                                     ThemePickerR.string.keyguard_quick_affordance_section_title
130                                 else -> R.string.app_name
131                             }
132                         toolbar.title = toolbar.resources.getString(stringResId)
133                     }
134                 }
135             }
136         }
137     }
138 
139     companion object {
140         private const val ANIMATION_DURATION = 200L
141     }
142 }
143