1 /*
<lambda>null2  * 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.customization.picker.quickaffordance.domain.interactor
19 
20 import com.android.systemui.shared.customization.data.content.CustomizationProviderClient
21 import com.android.wallpaper.picker.undo.domain.interactor.SnapshotRestorer
22 import com.android.wallpaper.picker.undo.domain.interactor.SnapshotStore
23 import com.android.wallpaper.picker.undo.shared.model.RestorableSnapshot
24 
25 /** Handles state restoration for the quick affordances system. */
26 class KeyguardQuickAffordanceSnapshotRestorer(
27     private val interactor: KeyguardQuickAffordancePickerInteractor,
28     private val client: CustomizationProviderClient,
29 ) : SnapshotRestorer {
30 
31     private var snapshotStore: SnapshotStore = SnapshotStore.NOOP
32 
33     suspend fun storeSnapshot() {
34         snapshotStore.store(snapshot())
35     }
36 
37     override suspend fun setUpSnapshotRestorer(
38         store: SnapshotStore,
39     ): RestorableSnapshot {
40         snapshotStore = store
41         return snapshot()
42     }
43 
44     override suspend fun restoreToSnapshot(snapshot: RestorableSnapshot) {
45         // reset all current selections
46         interactor.unselectAll()
47 
48         val allSelections = checkNotNull(snapshot.args[KEY_SELECTIONS])
49         if (allSelections.isEmpty()) return
50 
51         val selections: List<Pair<String, String>> =
52             allSelections.split(SELECTION_SEPARATOR).map { selection ->
53                 val (slotId, affordanceId) = selection.split(SLOT_AFFORDANCE_SEPARATOR)
54                 slotId to affordanceId
55             }
56 
57         selections.forEach { (slotId, affordanceId) ->
58             interactor.select(
59                 slotId,
60                 affordanceId,
61             )
62         }
63     }
64 
65     private suspend fun snapshot(): RestorableSnapshot {
66         return RestorableSnapshot(
67             mapOf(
68                 KEY_SELECTIONS to
69                     client.querySelections().joinToString(SELECTION_SEPARATOR) { selection ->
70                         "${selection.slotId}${SLOT_AFFORDANCE_SEPARATOR}${selection.affordanceId}"
71                     }
72             )
73         )
74     }
75 
76     companion object {
77         private const val KEY_SELECTIONS = "selections"
78         private const val SLOT_AFFORDANCE_SEPARATOR = "->"
79         private const val SELECTION_SEPARATOR = "|"
80     }
81 }
82