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.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.module.logging.ThemesUserEventLogger
25 import com.android.systemui.shared.notifications.domain.interactor.NotificationSettingsInteractor
26 import kotlinx.coroutines.flow.Flow
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: NotificationSettingsInteractor,
34     private val logger: ThemesUserEventLogger,
35 ) : ViewModel() {
36 
37     /** Whether the switch should be on. */
isSwitchOnnull38     suspend fun isSwitchOn(): Flow<Boolean> = interactor.isShowNotificationsOnLockScreenEnabled()
39 
40     /** Notifies that the section has been clicked. */
41     fun onClicked() {
42         viewModelScope.launch {
43             interactor.toggleShowNotificationsOnLockscreenEnabled()
44             logger.logLockScreenNotificationApplied(
45                 interactor.isShowNotificationsOnLockScreenEnabled().value
46             )
47         }
48     }
49 
50     class Factory(
51         private val interactor: NotificationSettingsInteractor,
52         private val logger: ThemesUserEventLogger,
53     ) : ViewModelProvider.Factory {
54         @Suppress("UNCHECKED_CAST")
createnull55         override fun <T : ViewModel> create(modelClass: Class<T>): T {
56             return NotificationSectionViewModel(
57                 interactor = interactor,
58                 logger = logger,
59             )
60                 as T
61         }
62     }
63 }
64