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 package com.android.customization.picker.notifications.domain.interactor
18 
19 import com.android.systemui.shared.notifications.domain.interactor.NotificationSettingsInteractor
20 import com.android.wallpaper.picker.di.modules.BackgroundDispatcher
21 import com.android.wallpaper.picker.undo.domain.interactor.SnapshotRestorer
22 import com.android.wallpaper.picker.undo.domain.interactor.SnapshotStore
23 import com.android.wallpaper.picker.undo.shared.model.RestorableSnapshot
24 import kotlinx.coroutines.CoroutineScope
25 import kotlinx.coroutines.launch
26 
27 /** Handles state restoration for notification settings. */
28 class NotificationsSnapshotRestorer(
29     private val interactor: NotificationSettingsInteractor,
30     @BackgroundDispatcher private val backgroundScope: CoroutineScope,
31 ) : SnapshotRestorer {
32 
33     private var snapshotStore: SnapshotStore = SnapshotStore.NOOP
34 
storeSnapshotnull35     private fun storeSnapshot(model: NotificationSnapshotModel) {
36         snapshotStore.store(snapshot(model))
37     }
38 
setUpSnapshotRestorernull39     override suspend fun setUpSnapshotRestorer(
40         store: SnapshotStore,
41     ): RestorableSnapshot {
42         snapshotStore = store
43         // The initial snapshot should be returned and stored before storing additional snapshots.
44         return snapshot(
45                 NotificationSnapshotModel(interactor.isShowNotificationsOnLockScreenEnabled().value)
46             )
47             .also {
48                 backgroundScope.launch {
49                     interactor.isShowNotificationsOnLockScreenEnabled().collect {
50                         storeSnapshot(
51                             NotificationSnapshotModel(isShowNotificationsOnLockScreenEnabled = it)
52                         )
53                     }
54                 }
55             }
56     }
57 
restoreToSnapshotnull58     override suspend fun restoreToSnapshot(snapshot: RestorableSnapshot) {
59         val isShowNotificationsOnLockScreenEnabled =
60             snapshot.args[KEY_IS_SHOW_NOTIFICATIONS_ON_LOCK_SCREEN_ENABLED]?.toBoolean() ?: false
61         interactor.setShowNotificationsOnLockscreenEnabled(isShowNotificationsOnLockScreenEnabled)
62     }
63 
snapshotnull64     private fun snapshot(model: NotificationSnapshotModel): RestorableSnapshot {
65         return RestorableSnapshot(
66             mapOf(
67                 KEY_IS_SHOW_NOTIFICATIONS_ON_LOCK_SCREEN_ENABLED to
68                     model.isShowNotificationsOnLockScreenEnabled.toString(),
69             )
70         )
71     }
72 
73     companion object {
74         private const val KEY_IS_SHOW_NOTIFICATIONS_ON_LOCK_SCREEN_ENABLED =
75             "is_show_notifications_on_lock_screen_enabled"
76     }
77 }
78 
79 /** Snapshot of notification settings relevant to the theme picker. */
80 private data class NotificationSnapshotModel(
81     /** Whether notifications are shown on the lock screen. */
82     val isShowNotificationsOnLockScreenEnabled: Boolean = false,
83 )
84