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.clock.domain.interactor
19 
20 import android.text.TextUtils
21 import android.util.Log
22 import com.android.customization.picker.clock.shared.model.ClockSnapshotModel
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 
27 /** Handles state restoration for clocks. */
28 class ClockPickerSnapshotRestorer(private val interactor: ClockPickerInteractor) :
29     SnapshotRestorer {
30     private var snapshotStore: SnapshotStore = SnapshotStore.NOOP
31     private var originalOption: ClockSnapshotModel? = null
32 
33     override suspend fun setUpSnapshotRestorer(
34         store: SnapshotStore,
35     ): RestorableSnapshot {
36         snapshotStore = store
37         originalOption = interactor.getCurrentClockToRestore()
38         return snapshot(originalOption)
39     }
40 
41     override suspend fun restoreToSnapshot(snapshot: RestorableSnapshot) {
42         originalOption?.let { optionToRestore ->
43             if (
44                 TextUtils.isEmpty(optionToRestore.clockId) ||
45                     optionToRestore.clockId != snapshot.args[KEY_CLOCK_ID] ||
46                     optionToRestore.clockSize?.toString() != snapshot.args[KEY_CLOCK_SIZE] ||
47                     optionToRestore.colorToneProgress?.toString() !=
48                         snapshot.args[KEY_COLOR_TONE_PROGRESS] ||
49                     optionToRestore.seedColor?.toString() != snapshot.args[KEY_SEED_COLOR] ||
50                     optionToRestore.selectedColorId != snapshot.args[KEY_COLOR_ID]
51             ) {
52                 Log.wtf(
53                     TAG,
54                     """ Original clock option does not match snapshot option to restore to. The
55                         | current implementation doesn't support undo, only a reset back to the
56                         | original clock option."""
57                         .trimMargin(),
58                 )
59             }
60 
61             interactor.setClockOption(optionToRestore)
62         }
63     }
64 
65     fun storeSnapshot(clockSnapshotModel: ClockSnapshotModel) {
66         snapshotStore.store(snapshot(clockSnapshotModel))
67     }
68 
69     private fun snapshot(clockSnapshotModel: ClockSnapshotModel? = null): RestorableSnapshot {
70         val options =
71             if (clockSnapshotModel == null) emptyMap()
72             else
73                 buildMap {
74                     clockSnapshotModel.clockId?.let { put(KEY_CLOCK_ID, it) }
75                     clockSnapshotModel.clockSize?.let { put(KEY_CLOCK_SIZE, it.toString()) }
76                     clockSnapshotModel.selectedColorId?.let { put(KEY_COLOR_ID, it) }
77                     clockSnapshotModel.colorToneProgress?.let {
78                         put(KEY_COLOR_TONE_PROGRESS, it.toString())
79                     }
80                     clockSnapshotModel.seedColor?.let { put(KEY_SEED_COLOR, it.toString()) }
81                 }
82 
83         return RestorableSnapshot(options)
84     }
85 
86     companion object {
87         private const val TAG = "ClockPickerSnapshotRestorer"
88         private const val KEY_CLOCK_ID = "clock_id"
89         private const val KEY_CLOCK_SIZE = "clock_size"
90         private const val KEY_COLOR_ID = "color_id"
91         private const val KEY_COLOR_TONE_PROGRESS = "color_tone_progress"
92         private const val KEY_SEED_COLOR = "seed_color"
93     }
94 }
95