1 /*
<lambda>null2  * Copyright (C) 2022 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 
18 package com.android.customization.picker.quickaffordance.ui.binder
19 
20 import android.view.View
21 import android.widget.ImageView
22 import android.widget.TextView
23 import androidx.core.view.isVisible
24 import androidx.lifecycle.Lifecycle
25 import androidx.lifecycle.LifecycleOwner
26 import androidx.lifecycle.flowWithLifecycle
27 import androidx.lifecycle.lifecycleScope
28 import com.android.customization.picker.quickaffordance.ui.viewmodel.KeyguardQuickAffordancePickerViewModel
29 import com.android.themepicker.R
30 import com.android.wallpaper.picker.common.icon.ui.viewbinder.IconViewBinder
31 import com.android.wallpaper.picker.common.text.ui.viewbinder.TextViewBinder
32 import kotlinx.coroutines.flow.collectLatest
33 import kotlinx.coroutines.launch
34 
35 object KeyguardQuickAffordanceSectionViewBinder {
36     fun bind(
37         view: View,
38         viewModel: KeyguardQuickAffordancePickerViewModel,
39         lifecycleOwner: LifecycleOwner,
40         onClicked: () -> Unit,
41     ) {
42         view.setOnClickListener { onClicked() }
43 
44         val descriptionView: TextView =
45             view.requireViewById(R.id.keyguard_quick_affordance_description)
46         val icon1: ImageView = view.requireViewById(R.id.icon_1)
47         val icon2: ImageView = view.requireViewById(R.id.icon_2)
48 
49         lifecycleOwner.lifecycleScope.launch {
50             viewModel.summary
51                 .flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
52                 .collectLatest { summary ->
53                     TextViewBinder.bind(
54                         view = descriptionView,
55                         viewModel = summary.description,
56                     )
57 
58                     if (summary.icon1 != null) {
59                         IconViewBinder.bind(
60                             view = icon1,
61                             viewModel = summary.icon1,
62                         )
63                     }
64                     icon1.isVisible = summary.icon1 != null
65 
66                     if (summary.icon2 != null) {
67                         IconViewBinder.bind(
68                             view = icon2,
69                             viewModel = summary.icon2,
70                         )
71                     }
72                     icon2.isVisible = summary.icon2 != null
73                 }
74         }
75     }
76 }
77