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