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.data.repository
19 
20 import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerAffordanceModel as AffordanceModel
21 import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSelectionModel as SelectionModel
22 import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSlotModel as SlotModel
23 import com.android.systemui.shared.customization.data.content.CustomizationProviderClient
24 import com.android.wallpaper.picker.di.modules.MainDispatcher
25 import javax.inject.Inject
26 import javax.inject.Singleton
27 import kotlinx.coroutines.CoroutineScope
28 import kotlinx.coroutines.flow.Flow
29 import kotlinx.coroutines.flow.SharingStarted
30 import kotlinx.coroutines.flow.map
31 import kotlinx.coroutines.flow.shareIn
32 
33 /**
34  * Abstracts access to application state related to functionality for selecting, picking, or setting
35  * lock screen quick affordances.
36  */
37 @Singleton
38 class KeyguardQuickAffordancePickerRepository
39 @Inject
40 constructor(client: CustomizationProviderClient, @MainDispatcher mainScope: CoroutineScope) {
41     /** List of slots available on the device. */
42     val slots: Flow<List<SlotModel>> =
43         client.observeSlots().map { slots -> slots.map { slot -> slot.toModel() } }
44 
45     /** List of all available quick affordances. */
46     val affordances: Flow<List<AffordanceModel>> =
47         client
48             .observeAffordances()
49             .map { affordances -> affordances.map { affordance -> affordance.toModel() } }
50             .shareIn(mainScope, replay = 1, started = SharingStarted.Lazily)
51 
52     /** List of slot-affordance pairs, modeling what the user has currently chosen for each slot. */
53     val selections: Flow<List<SelectionModel>> =
54         client
55             .observeSelections()
56             .map { selections -> selections.map { selection -> selection.toModel() } }
57             .shareIn(mainScope, replay = 1, started = SharingStarted.Lazily)
58 
59     private fun CustomizationProviderClient.Slot.toModel(): SlotModel {
60         return SlotModel(
61             id = id,
62             maxSelectedQuickAffordances = capacity,
63         )
64     }
65 
66     private fun CustomizationProviderClient.Affordance.toModel(): AffordanceModel {
67         return AffordanceModel(
68             id = id,
69             name = name,
70             iconResourceId = iconResourceId,
71             isEnabled = isEnabled,
72             enablementExplanation = enablementExplanation ?: "",
73             enablementActionText = enablementActionText,
74             enablementActionIntent = enablementActionIntent,
75             configureIntent = configureIntent,
76         )
77     }
78 
79     private fun CustomizationProviderClient.Selection.toModel(): SelectionModel {
80         return SelectionModel(
81             slotId = slotId,
82             affordanceId = affordanceId,
83         )
84     }
85 }
86