• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 package com.android.customization.picker.notifications.ui.viewmodel
19 
20 import androidx.annotation.VisibleForTesting
21 import androidx.lifecycle.ViewModel
22 import androidx.lifecycle.ViewModelProvider
23 import androidx.lifecycle.viewModelScope
24 import com.android.customization.picker.notifications.domain.interactor.NotificationsInteractor
25 import kotlinx.coroutines.flow.Flow
26 import kotlinx.coroutines.flow.map
27 import kotlinx.coroutines.launch
28 
29 /** Models UI state for a section that lets the user control the notification settings. */
30 class NotificationSectionViewModel
31 @VisibleForTesting
32 constructor(
33     private val interactor: NotificationsInteractor,
34 ) : ViewModel() {
35 
36     /** Whether the switch should be on. */
37     val isSwitchOn: Flow<Boolean> =
38         interactor.settings.map { model -> model.isShowNotificationsOnLockScreenEnabled }
39 
40     /** Notifies that the section has been clicked. */
41     fun onClicked() {
42         viewModelScope.launch { interactor.toggleShowNotificationsOnLockScreenEnabled() }
43     }
44 
45     class Factory(
46         private val interactor: NotificationsInteractor,
47     ) : ViewModelProvider.Factory {
48         @Suppress("UNCHECKED_CAST")
49         override fun <T : ViewModel> create(modelClass: Class<T>): T {
50             return NotificationSectionViewModel(
51                 interactor = interactor,
52             )
53                 as T
54         }
55     }
56 }
57