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.model.grid.data.repository
19 
20 import com.android.customization.model.CustomizationManager
21 import com.android.customization.picker.grid.data.repository.GridRepository
22 import com.android.customization.picker.grid.shared.model.GridOptionItemModel
23 import com.android.customization.picker.grid.shared.model.GridOptionItemsModel
24 import kotlinx.coroutines.CoroutineScope
25 import kotlinx.coroutines.channels.BufferOverflow
26 import kotlinx.coroutines.flow.Flow
27 import kotlinx.coroutines.flow.MutableSharedFlow
28 import kotlinx.coroutines.flow.MutableStateFlow
29 import kotlinx.coroutines.flow.SharingStarted
30 import kotlinx.coroutines.flow.asSharedFlow
31 import kotlinx.coroutines.flow.map
32 import kotlinx.coroutines.flow.stateIn
33 
34 class FakeGridRepository(
35     private val scope: CoroutineScope,
36     initialOptionCount: Int,
37     var available: Boolean = true
38 ) : GridRepository {
39     private val _optionChanges =
40         MutableSharedFlow<Unit>(
41             replay = 1,
42             onBufferOverflow = BufferOverflow.DROP_OLDEST,
43         )
44 
45     override suspend fun isAvailable(): Boolean = available
46 
47     override fun getOptionChanges(): Flow<Unit> = _optionChanges.asSharedFlow()
48 
49     private val selectedOptionIndex = MutableStateFlow(0)
50     private var options: GridOptionItemsModel = createOptions(count = initialOptionCount)
51 
52     override suspend fun getOptions(): GridOptionItemsModel {
53         return options
54     }
55 
56     override fun getSelectedOption() = MutableStateFlow(null)
57 
58     override fun applySelectedOption(callback: CustomizationManager.Callback) {}
59 
60     override fun clearSelectedOption() {}
61 
62     override fun isSelectedOptionApplied() = false
63 
64     fun setOptions(
65         count: Int,
66         selectedIndex: Int = 0,
67     ) {
68         options = createOptions(count, selectedIndex)
69         _optionChanges.tryEmit(Unit)
70     }
71 
72     private fun createOptions(
73         count: Int,
74         selectedIndex: Int = 0,
75     ): GridOptionItemsModel {
76         selectedOptionIndex.value = selectedIndex
77         return GridOptionItemsModel.Loaded(
78             options =
79                 buildList {
80                     repeat(times = count) { index ->
81                         add(
82                             GridOptionItemModel(
83                                 name = "option_$index",
84                                 cols = 4,
85                                 rows = index * 2,
86                                 isSelected =
87                                     selectedOptionIndex
88                                         .map { it == index }
89                                         .stateIn(
90                                             scope = scope,
91                                             started = SharingStarted.Eagerly,
92                                             initialValue = false,
93                                         ),
94                                 onSelected = { selectedOptionIndex.value = index },
95                             )
96                         )
97                     }
98                 }
99         )
100     }
101 }
102