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.AxisPresetConfig 26 import com.android.systemui.plugins.clocks.AxisPresetConfig.Group 27 import com.android.systemui.plugins.clocks.AxisType 28 import com.android.systemui.plugins.clocks.ClockAxisStyle 29 import com.android.systemui.plugins.clocks.ClockFontAxis 30 import com.android.systemui.plugins.clocks.ClockId 31 import kotlinx.coroutines.flow.Flow 32 import kotlinx.coroutines.flow.MutableStateFlow 33 import kotlinx.coroutines.flow.asStateFlow 34 import kotlinx.coroutines.flow.combine 35 36 /** By default [FakeClockPickerRepository] uses [fakeClocks]. */ 37 open class FakeClockPickerRepository(clocks: List<ClockMetadataModel> = fakeClocks) : 38 ClockPickerRepository { 39 override val allClocks: Flow<List<ClockMetadataModel>> = MutableStateFlow(clocks).asStateFlow() 40 41 private val selectedClockId: MutableStateFlow<String> = MutableStateFlow(fakeClocks[0].clockId) 42 @ColorInt private val selectedColorId: MutableStateFlow<String?> = MutableStateFlow(null) 43 private val colorTone: MutableStateFlow<Int> = 44 MutableStateFlow(ClockMetadataModel.DEFAULT_COLOR_TONE_PROGRESS) 45 @ColorInt private val seedColor: MutableStateFlow<Int?> = MutableStateFlow(null) 46 private val clockListFlow: MutableStateFlow<List<ClockMetadataModel>> = 47 MutableStateFlow(fakeClocks) 48 override val selectedClock: Flow<ClockMetadataModel> = 49 combine(clockListFlow, selectedClockId, selectedColorId, colorTone, seedColor) { 50 clockList, 51 selectedClockId, 52 selectedColor, 53 colorTone, 54 seedColor -> 55 val selectedClock = clockList.find { clock -> clock.clockId == selectedClockId } 56 checkNotNull(selectedClock) 57 ClockMetadataModel( 58 clockId = selectedClock.clockId, 59 isSelected = true, 60 description = "description", 61 thumbnail = ColorDrawable(0), 62 isReactiveToTone = selectedClock.isReactiveToTone, 63 axisPresetConfig = selectedClock.axisPresetConfig, 64 selectedColorId = selectedColor, 65 colorToneProgress = colorTone, 66 seedColor = seedColor, 67 ) 68 } 69 70 private val _selectedClockSize = MutableStateFlow(ClockSize.DYNAMIC) 71 override val selectedClockSize: Flow<ClockSize> = _selectedClockSize.asStateFlow() 72 73 override suspend fun setSelectedClock(clockId: String) { 74 selectedClockId.value = clockId 75 } 76 77 override suspend fun setClockColor( 78 selectedColorId: String?, 79 @IntRange(from = 0, to = 100) colorToneProgress: Int, 80 @ColorInt seedColor: Int?, 81 ) { 82 this.selectedColorId.value = selectedColorId 83 this.colorTone.value = colorToneProgress 84 this.seedColor.value = seedColor 85 } 86 87 override suspend fun setClockSize(size: ClockSize) { 88 _selectedClockSize.value = size 89 } 90 91 override suspend fun setClockAxisStyle(axisStyle: ClockAxisStyle) { 92 val clockList = clockListFlow.value 93 val newClockList = clockList.toMutableList() 94 for ((index, clock) in clockList.withIndex()) { 95 val presetConfig = clock.axisPresetConfig 96 val style = presetConfig?.findStyle(axisStyle) 97 if (presetConfig != null && style != null) { 98 newClockList[index] = 99 clock.copy(axisPresetConfig = presetConfig.copy(current = style)) 100 } 101 } 102 103 clockListFlow.value = newClockList.toList() 104 } 105 106 override fun isReactiveToTone(clockId: ClockId): Boolean? = true 107 108 companion object { 109 fun buildFakeClockAxisStyle(i: Int): ClockAxisStyle { 110 return ClockAxisStyle(listOf(buildFakeAxis(i))) 111 } 112 113 private fun buildFakeAxis(i: Int): ClockFontAxis { 114 return ClockFontAxis( 115 key = "key#$i", 116 type = AxisType.Float, 117 maxValue = 0f, 118 minValue = 1000f, 119 currentValue = 50f * (i + 1), 120 name = "FakeAxis", 121 description = "Axis Description", 122 ) 123 } 124 125 private val fakeClockAxisStyle0 = ClockAxisStyle(listOf(buildFakeAxis(0))) 126 val fakeClockAxisStyle1 = ClockAxisStyle(listOf(buildFakeAxis(1))) 127 private val fakeClockAxisStyle2 = ClockAxisStyle(listOf(buildFakeAxis(2))) 128 private val fakeClockAxisStyle3 = ClockAxisStyle(listOf(buildFakeAxis(3))) 129 private val fakeAxisPresetConfig: AxisPresetConfig = 130 AxisPresetConfig( 131 groups = 132 listOf( 133 AxisPresetConfig.Group( 134 presets = listOf(fakeClockAxisStyle0, fakeClockAxisStyle1), 135 icon = ColorDrawable(Color.BLUE), 136 ), 137 AxisPresetConfig.Group( 138 presets = listOf(fakeClockAxisStyle2, fakeClockAxisStyle3), 139 icon = ColorDrawable(Color.YELLOW), 140 ), 141 ), 142 current = 143 AxisPresetConfig.IndexedStyle( 144 groupIndex = 0, 145 presetIndex = 0, 146 style = fakeClockAxisStyle0, 147 ), 148 ) 149 150 const val CLOCK_ID_0 = "clock0" 151 const val CLOCK_ID_1 = "clock1" 152 const val CLOCK_ID_2 = "clock2" 153 const val CLOCK_ID_3 = "clock3" 154 155 val fakeClocks = 156 listOf( 157 ClockMetadataModel( 158 CLOCK_ID_0, 159 true, 160 "description0", 161 ColorDrawable(0), 162 true, 163 fakeAxisPresetConfig, 164 null, 165 50, 166 null, 167 ), 168 ClockMetadataModel( 169 CLOCK_ID_1, 170 false, 171 "description1", 172 ColorDrawable(0), 173 true, 174 null, 175 null, 176 50, 177 null, 178 ), 179 ClockMetadataModel( 180 CLOCK_ID_2, 181 false, 182 "description2", 183 ColorDrawable(0), 184 true, 185 null, 186 null, 187 50, 188 null, 189 ), 190 ClockMetadataModel( 191 CLOCK_ID_3, 192 false, 193 "description3", 194 ColorDrawable(0), 195 false, 196 null, 197 null, 198 50, 199 null, 200 ), 201 ) 202 const val CLOCK_COLOR_ID = "RED" 203 const val CLOCK_COLOR_TONE_PROGRESS = 87 204 const val SEED_COLOR = Color.RED 205 } 206 } 207