1 /*
2  * 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 
18 package com.android.customization.picker.notifications.ui.binder
19 
20 import android.annotation.SuppressLint
21 import android.view.View
22 import android.widget.Switch
23 import androidx.lifecycle.Lifecycle
24 import androidx.lifecycle.LifecycleOwner
25 import androidx.lifecycle.lifecycleScope
26 import androidx.lifecycle.repeatOnLifecycle
27 import com.android.customization.picker.notifications.ui.viewmodel.NotificationSectionViewModel
28 import com.android.themepicker.R
29 import kotlinx.coroutines.launch
30 
31 /**
32  * Binds between view and view-model for a section that lets the user control notification settings.
33  */
34 object NotificationSectionBinder {
35     @SuppressLint("UseSwitchCompatOrMaterialCode") // We're using Switch and that's okay for SysUI.
bindnull36     fun bind(
37         view: View,
38         viewModel: NotificationSectionViewModel,
39         lifecycleOwner: LifecycleOwner,
40     ) {
41         val switch: Switch = view.requireViewById(R.id.switcher)
42 
43         view.setOnClickListener { viewModel.onClicked() }
44 
45         lifecycleOwner.lifecycleScope.launch {
46             lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
47                 launch { viewModel.isSwitchOn().collect { switch.isChecked = it } }
48             }
49         }
50     }
51 }
52