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.color.domain.interactor 19 20 import android.util.Log 21 import com.android.customization.picker.color.shared.model.ColorOptionModel 22 import com.android.wallpaper.picker.undo.domain.interactor.SnapshotRestorer 23 import com.android.wallpaper.picker.undo.domain.interactor.SnapshotStore 24 import com.android.wallpaper.picker.undo.shared.model.RestorableSnapshot 25 26 /** Handles state restoration for the color picker system. */ 27 class ColorPickerSnapshotRestorer( 28 private val interactor: ColorPickerInteractor, 29 ) : SnapshotRestorer { 30 31 private var snapshotStore: SnapshotStore = SnapshotStore.NOOP 32 private var originalOption: ColorOptionModel? = null 33 34 fun storeSnapshot(colorOptionModel: ColorOptionModel) { 35 snapshotStore.store(snapshot(colorOptionModel)) 36 } 37 38 override suspend fun setUpSnapshotRestorer( 39 store: SnapshotStore, 40 ): RestorableSnapshot { 41 snapshotStore = store 42 originalOption = interactor.getCurrentColorOption() 43 return snapshot(originalOption) 44 } 45 46 override suspend fun restoreToSnapshot(snapshot: RestorableSnapshot) { 47 val optionPackagesFromSnapshot: String? = snapshot.args[KEY_COLOR_OPTION_PACKAGES] 48 originalOption?.let { optionToRestore -> 49 if ( 50 optionToRestore.colorOption.serializedPackages != optionPackagesFromSnapshot || 51 optionToRestore.colorOption.style.toString() != 52 snapshot.args[KEY_COLOR_OPTION_STYLE] 53 ) { 54 Log.wtf( 55 TAG, 56 """ Original packages does not match snapshot packages to restore to. The 57 | current implementation doesn't support undo, only a reset back to the 58 | original color option.""" 59 .trimMargin(), 60 ) 61 } 62 63 interactor.select(optionToRestore) 64 } 65 } 66 67 private fun snapshot(colorOptionModel: ColorOptionModel? = null): RestorableSnapshot { 68 val snapshotMap = mutableMapOf<String, String>() 69 colorOptionModel?.let { 70 snapshotMap[KEY_COLOR_OPTION_PACKAGES] = colorOptionModel.colorOption.serializedPackages 71 snapshotMap[KEY_COLOR_OPTION_STYLE] = colorOptionModel.colorOption.style.toString() 72 } 73 return RestorableSnapshot(snapshotMap) 74 } 75 76 companion object { 77 private const val TAG = "ColorPickerSnapshotRestorer" 78 private const val KEY_COLOR_OPTION_PACKAGES = "color_packages" 79 private const val KEY_COLOR_OPTION_STYLE = "color_style" 80 } 81 } 82