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.domain.interactor 19 20 import com.android.customization.picker.notifications.shared.model.NotificationSettingsModel 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 25 /** Handles state restoration for notification settings. */ 26 class NotificationsSnapshotRestorer( 27 private val interactor: NotificationsInteractor, 28 ) : SnapshotRestorer { 29 30 private var snapshotStore: SnapshotStore = SnapshotStore.NOOP 31 storeSnapshotnull32 fun storeSnapshot(model: NotificationSettingsModel) { 33 snapshotStore.store(snapshot(model)) 34 } 35 setUpSnapshotRestorernull36 override suspend fun setUpSnapshotRestorer( 37 store: SnapshotStore, 38 ): RestorableSnapshot { 39 snapshotStore = store 40 return snapshot(interactor.getSettings()) 41 } 42 restoreToSnapshotnull43 override suspend fun restoreToSnapshot(snapshot: RestorableSnapshot) { 44 val isShowNotificationsOnLockScreenEnabled = 45 snapshot.args[KEY_IS_SHOW_NOTIFICATIONS_ON_LOCK_SCREEN_ENABLED]?.toBoolean() ?: false 46 interactor.setSettings( 47 NotificationSettingsModel( 48 isShowNotificationsOnLockScreenEnabled = isShowNotificationsOnLockScreenEnabled, 49 ) 50 ) 51 } 52 snapshotnull53 private fun snapshot(model: NotificationSettingsModel): RestorableSnapshot { 54 return RestorableSnapshot( 55 mapOf( 56 KEY_IS_SHOW_NOTIFICATIONS_ON_LOCK_SCREEN_ENABLED to 57 model.isShowNotificationsOnLockScreenEnabled.toString(), 58 ) 59 ) 60 } 61 62 companion object { 63 private const val KEY_IS_SHOW_NOTIFICATIONS_ON_LOCK_SCREEN_ENABLED = 64 "is_show_notifications_on_lock_screen_enabled" 65 } 66 } 67