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.systemui.volume.dialog.settings.ui.binder
18 
19 import android.view.View
20 import com.android.systemui.lifecycle.WindowLifecycleState
21 import com.android.systemui.lifecycle.repeatWhenAttached
22 import com.android.systemui.lifecycle.setSnapshotBinding
23 import com.android.systemui.lifecycle.viewModel
24 import com.android.systemui.res.R
25 import com.android.systemui.volume.dialog.dagger.scope.VolumeDialogScope
26 import com.android.systemui.volume.dialog.settings.ui.viewmodel.VolumeDialogSettingsButtonViewModel
27 import javax.inject.Inject
28 import kotlinx.coroutines.awaitCancellation
29 import kotlinx.coroutines.flow.launchIn
30 import kotlinx.coroutines.flow.onEach
31 
32 @VolumeDialogScope
33 class VolumeDialogSettingsButtonViewBinder
34 @Inject
35 constructor(private val viewModelFactory: VolumeDialogSettingsButtonViewModel.Factory) {
36 
37     fun bind(view: View) {
38         with(view) {
39             val button = requireViewById<View>(R.id.volume_dialog_settings)
40             repeatWhenAttached {
41                 viewModel(
42                     traceName = "VolumeDialogViewBinder",
43                     minWindowLifecycleState = WindowLifecycleState.ATTACHED,
44                     factory = { viewModelFactory.create() },
45                 ) { viewModel ->
46                     setSnapshotBinding {
47                         viewModel.isVisible
48                             .onEach { isVisible ->
49                                 visibility = if (isVisible) View.VISIBLE else View.GONE
50                             }
51                             .launchIn(this)
52 
53                         button.setOnClickListener { viewModel.onButtonClicked() }
54                     }
55 
56                     awaitCancellation()
57                 }
58             }
59         }
60     }
61 }
62