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.picker.customization.ui.binder
18 
19 import android.view.View
20 import android.widget.Button
21 import android.widget.FrameLayout
22 import android.widget.Toolbar
23 import androidx.appcompat.content.res.AppCompatResources
24 import androidx.core.graphics.drawable.DrawableCompat
25 import androidx.lifecycle.Lifecycle
26 import androidx.lifecycle.LifecycleOwner
27 import androidx.lifecycle.lifecycleScope
28 import androidx.lifecycle.repeatOnLifecycle
29 import com.android.wallpaper.R
30 import com.android.wallpaper.picker.customization.ui.viewmodel.ColorUpdateViewModel
31 import com.android.wallpaper.picker.customization.ui.viewmodel.CustomizationOptionsViewModel
32 import javax.inject.Inject
33 import javax.inject.Singleton
34 import kotlinx.coroutines.launch
35 
36 @Singleton
37 class DefaultToolbarBinder @Inject constructor() : ToolbarBinder {
38 
39     override fun bind(
40         navButton: FrameLayout,
41         toolbar: Toolbar,
42         applyButton: Button,
43         viewModel: CustomizationOptionsViewModel,
44         colorUpdateViewModel: ColorUpdateViewModel,
45         lifecycleOwner: LifecycleOwner,
46         onNavBack: () -> Unit,
47     ) {
48         val appContext = navButton.context.applicationContext
49         val navButtonIcon = navButton.requireViewById<View>(R.id.nav_button_icon)
50 
51         navButton.setOnClickListener { onNavBack.invoke() }
52 
53         ColorUpdateBinder.bind(
54             setColor = { color -> toolbar.setTitleTextColor(color) },
55             color = colorUpdateViewModel.colorOnSurface,
56             shouldAnimate = { true },
57             lifecycleOwner = lifecycleOwner,
58         )
59 
60         ColorUpdateBinder.bind(
61             setColor = { color ->
62                 DrawableCompat.setTint(DrawableCompat.wrap(navButton.background), color)
63             },
64             color = colorUpdateViewModel.colorSurfaceContainerHighest,
65             shouldAnimate = { true },
66             lifecycleOwner = lifecycleOwner,
67         )
68 
69         ColorUpdateBinder.bind(
70             setColor = { color ->
71                 DrawableCompat.setTint(DrawableCompat.wrap(navButtonIcon.background), color)
72             },
73             color = colorUpdateViewModel.colorOnSurfaceVariant,
74             shouldAnimate = { true },
75             lifecycleOwner = lifecycleOwner,
76         )
77 
78         lifecycleOwner.lifecycleScope.launch {
79             lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
80                 launch {
81                     viewModel.selectedOption.collect {
82                         if (it == null) {
83                             navButtonIcon.background =
84                                 AppCompatResources.getDrawable(
85                                     appContext,
86                                     R.drawable.ic_arrow_back_24dp,
87                                 )
88                         } else {
89                             navButtonIcon.background =
90                                 AppCompatResources.getDrawable(appContext, R.drawable.ic_close_24dp)
91                         }
92                     }
93                 }
94             }
95         }
96     }
97 }
98