1 /*
2  * Copyright (C) 2022 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.wallpaper.picker.undo.shared.model
19 
20 /** Models a snapshot of the state of an undo-supporting feature at a given time. */
21 data class RestorableSnapshot(
22     val args: Map<String, String>,
23 ) {
24     /**
25      * Returns a copy of the [RestorableSnapshot] but with the [block] applied to its arguments.
26      *
27      * Sample usage:
28      * ```
29      * val previousSnapshot: RestorableSnapshot = ...
30      * val nextSnapshot = previousSnapshot { args ->
31      *     args.put("one", "true")
32      *     args.remove("two")
33      * }
34      *
35      * // Now, nextSnapshot is exactly like previousSnapshot but with its args having "one" mapped
36      * // to "true" and without "two", since it was removed.
37      * ```
38      *
39      * @param block A function that receives the original [args] from the current
40      *   [RestorableSnapshot] and can edit them for inclusion into the returned
41      *   [RestorableSnapshot].
42      */
copynull43     fun copy(
44         block: (MutableMap<String, String>) -> Unit,
45     ): RestorableSnapshot {
46         val mutableArgs = args.toMutableMap()
47         block(mutableArgs)
48         return RestorableSnapshot(
49             args = mutableArgs.toMap(),
50         )
51     }
52 }
53