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 package com.android.customization.picker.clock.data.repository
17 
18 import android.graphics.Color
19 import android.graphics.drawable.ColorDrawable
20 import androidx.annotation.ColorInt
21 import androidx.annotation.IntRange
22 import com.android.customization.picker.clock.data.repository.FakeClockPickerRepository.Companion.fakeClocks
23 import com.android.customization.picker.clock.shared.ClockSize
24 import com.android.customization.picker.clock.shared.model.ClockMetadataModel
25 import com.android.systemui.plugins.clocks.AxisType
26 import com.android.systemui.plugins.clocks.ClockFontAxis
27 import com.android.systemui.plugins.clocks.ClockFontAxisSetting
28 import kotlinx.coroutines.flow.Flow
29 import kotlinx.coroutines.flow.MutableStateFlow
30 import kotlinx.coroutines.flow.asStateFlow
31 import kotlinx.coroutines.flow.combine
32 import kotlinx.coroutines.flow.update
33 
34 /** By default [FakeClockPickerRepository] uses [fakeClocks]. */
35 open class FakeClockPickerRepository(clocks: List<ClockMetadataModel> = fakeClocks) :
36     ClockPickerRepository {
37     override val allClocks: Flow<List<ClockMetadataModel>> = MutableStateFlow(clocks).asStateFlow()
38 
39     private val selectedClockId = MutableStateFlow(fakeClocks[0].clockId)
40     @ColorInt private val selectedColorId = MutableStateFlow<String?>(null)
41     private val colorTone = MutableStateFlow(ClockMetadataModel.DEFAULT_COLOR_TONE_PROGRESS)
42     @ColorInt private val seedColor = MutableStateFlow<Int?>(null)
43     private val fontAxes = MutableStateFlow<List<ClockFontAxis>>(listOf(buildFakeAxis(0)))
44     override val selectedClock: Flow<ClockMetadataModel> =
45         combine(selectedClockId, selectedColorId, colorTone, seedColor, fontAxes) {
46             selectedClockId,
47             selectedColor,
48             colorTone,
49             seedColor,
50             fontAxes ->
51             val selectedClock = fakeClocks.find { clock -> clock.clockId == selectedClockId }
52             checkNotNull(selectedClock)
53             ClockMetadataModel(
54                 clockId = selectedClock.clockId,
55                 isSelected = true,
56                 description = "description",
57                 thumbnail = ColorDrawable(0),
58                 isReactiveToTone = selectedClock.isReactiveToTone,
59                 fontAxes = fontAxes,
60                 selectedColorId = selectedColor,
61                 colorToneProgress = colorTone,
62                 seedColor = seedColor,
63             )
64         }
65 
66     private val _selectedClockSize = MutableStateFlow(ClockSize.DYNAMIC)
67     override val selectedClockSize: Flow<ClockSize> = _selectedClockSize.asStateFlow()
68 
69     override suspend fun setSelectedClock(clockId: String) {
70         selectedClockId.value = clockId
71     }
72 
73     override suspend fun setClockColor(
74         selectedColorId: String?,
75         @IntRange(from = 0, to = 100) colorToneProgress: Int,
76         @ColorInt seedColor: Int?,
77     ) {
78         this.selectedColorId.value = selectedColorId
79         this.colorTone.value = colorToneProgress
80         this.seedColor.value = seedColor
81     }
82 
83     override suspend fun setClockSize(size: ClockSize) {
84         _selectedClockSize.value = size
85     }
86 
87     override suspend fun setClockFontAxes(axisSettings: List<ClockFontAxisSetting>) {
88         fontAxes.update { fontAxes -> ClockFontAxis.merge(fontAxes, axisSettings) }
89     }
90 
91     companion object {
92         fun buildFakeAxis(i: Int): ClockFontAxis {
93             return ClockFontAxis(
94                 key = "key",
95                 type = AxisType.Float,
96                 maxValue = 0f,
97                 minValue = 1000f,
98                 currentValue = 50f * (i + 1),
99                 name = "FakeAxis",
100                 description = "Axis Description",
101             )
102         }
103 
104         const val CLOCK_ID_0 = "clock0"
105         const val CLOCK_ID_1 = "clock1"
106         const val CLOCK_ID_2 = "clock2"
107         const val CLOCK_ID_3 = "clock3"
108         val fakeClocks =
109             listOf(
110                 ClockMetadataModel(
111                     CLOCK_ID_0,
112                     true,
113                     "description0",
114                     ColorDrawable(0),
115                     true,
116                     listOf(buildFakeAxis(0)),
117                     null,
118                     50,
119                     null,
120                 ),
121                 ClockMetadataModel(
122                     CLOCK_ID_1,
123                     false,
124                     "description1",
125                     ColorDrawable(0),
126                     true,
127                     listOf(buildFakeAxis(1)),
128                     null,
129                     50,
130                     null,
131                 ),
132                 ClockMetadataModel(
133                     CLOCK_ID_2,
134                     false,
135                     "description2",
136                     ColorDrawable(0),
137                     true,
138                     listOf(buildFakeAxis(2)),
139                     null,
140                     50,
141                     null,
142                 ),
143                 ClockMetadataModel(
144                     CLOCK_ID_3,
145                     false,
146                     "description3",
147                     ColorDrawable(0),
148                     false,
149                     listOf(buildFakeAxis(3)),
150                     null,
151                     50,
152                     null,
153                 ),
154             )
155         const val CLOCK_COLOR_ID = "RED"
156         const val CLOCK_COLOR_TONE_PROGRESS = 87
157         const val SEED_COLOR = Color.RED
158     }
159 }
160