1 /*
<lambda>null2  * Copyright (C) 2023 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.preview.ui.binder
18 
19 import android.widget.Button
20 import android.widget.CheckBox
21 import androidx.lifecycle.Lifecycle
22 import androidx.lifecycle.LifecycleOwner
23 import androidx.lifecycle.lifecycleScope
24 import androidx.lifecycle.repeatOnLifecycle
25 import com.android.wallpaper.picker.di.modules.MainDispatcher
26 import com.android.wallpaper.picker.preview.ui.viewmodel.WallpaperPreviewViewModel
27 import kotlinx.coroutines.CoroutineScope
28 import kotlinx.coroutines.launch
29 
30 /** Binds the set wallpaper button on small preview. */
31 object ApplyWallpaperScreenBinder {
32 
33     fun bind(
34         applyButton: Button,
35         cancelButton: Button,
36         homeCheckbox: CheckBox,
37         lockCheckbox: CheckBox,
38         viewModel: WallpaperPreviewViewModel,
39         lifecycleOwner: LifecycleOwner,
40         @MainDispatcher mainScope: CoroutineScope,
41         onWallpaperSet: () -> Unit,
42     ) {
43         lifecycleOwner.lifecycleScope.launch {
44             lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
45                 launch {
46                     viewModel.onCancelButtonClicked.collect { onClicked ->
47                         cancelButton.setOnClickListener { onClicked() }
48                     }
49                 }
50 
51                 launch { viewModel.isApplyButtonEnabled.collect { applyButton.isEnabled = it } }
52 
53                 launch { viewModel.isHomeCheckBoxChecked.collect { homeCheckbox.isChecked = it } }
54 
55                 launch { viewModel.isLockCheckBoxChecked.collect { lockCheckbox.isChecked = it } }
56 
57                 launch {
58                     viewModel.onHomeCheckBoxChecked.collect {
59                         homeCheckbox.setOnClickListener { it() }
60                     }
61                 }
62 
63                 launch {
64                     viewModel.onLockCheckBoxChecked.collect {
65                         lockCheckbox.setOnClickListener { it() }
66                     }
67                 }
68 
69                 launch {
70                     viewModel.setWallpaperDialogOnConfirmButtonClicked.collect { onClicked ->
71                         applyButton.setOnClickListener {
72                             mainScope.launch {
73                                 onClicked()
74                                 onWallpaperSet()
75                             }
76                         }
77                     }
78                 }
79             }
80         }
81     }
82 }
83