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.model.themedicon.domain.interactor 19 20 import com.android.wallpaper.picker.undo.domain.interactor.SnapshotRestorer 21 import com.android.wallpaper.picker.undo.domain.interactor.SnapshotStore 22 import com.android.wallpaper.picker.undo.shared.model.RestorableSnapshot 23 24 class ThemedIconSnapshotRestorer( 25 private val isActivated: () -> Boolean, 26 private val setActivated: (isActivated: Boolean) -> Unit, 27 private val interactor: ThemedIconInteractor, 28 ) : SnapshotRestorer { 29 30 private var store: SnapshotStore = SnapshotStore.NOOP 31 setUpSnapshotRestorernull32 override suspend fun setUpSnapshotRestorer(store: SnapshotStore): RestorableSnapshot { 33 this.store = store 34 return snapshot() 35 } 36 restoreToSnapshotnull37 override suspend fun restoreToSnapshot(snapshot: RestorableSnapshot) { 38 val isActivated = snapshot.args[KEY]?.toBoolean() == true 39 setActivated(isActivated) 40 interactor.setActivated(isActivated) 41 } 42 storenull43 fun store( 44 isActivated: Boolean, 45 ) { 46 store.store(snapshot(isActivated = isActivated)) 47 } 48 snapshotnull49 private fun snapshot( 50 isActivated: Boolean? = null, 51 ): RestorableSnapshot { 52 return RestorableSnapshot( 53 args = buildMap { put(KEY, (isActivated ?: isActivated()).toString()) } 54 ) 55 } 56 57 companion object { 58 private const val KEY = "is_activated" 59 } 60 } 61