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