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 androidx.annotation.ColorInt
20 import androidx.annotation.IntRange
21 import com.android.customization.picker.clock.data.repository.FakeClockPickerRepository.Companion.fakeClocks
22 import com.android.customization.picker.clock.shared.ClockSize
23 import com.android.customization.picker.clock.shared.model.ClockMetadataModel
24 import kotlinx.coroutines.flow.Flow
25 import kotlinx.coroutines.flow.MutableStateFlow
26 import kotlinx.coroutines.flow.asStateFlow
27 import kotlinx.coroutines.flow.combine
28 
29 /** By default [FakeClockPickerRepository] uses [fakeClocks]. */
30 open class FakeClockPickerRepository(clocks: List<ClockMetadataModel> = fakeClocks) :
31     ClockPickerRepository {
32     override val allClocks: Flow<List<ClockMetadataModel>> = MutableStateFlow(clocks).asStateFlow()
33 
34     private val selectedClockId = MutableStateFlow(fakeClocks[0].clockId)
35     @ColorInt private val selectedColorId = MutableStateFlow<String?>(null)
36     private val colorTone = MutableStateFlow(ClockMetadataModel.DEFAULT_COLOR_TONE_PROGRESS)
37     @ColorInt private val seedColor = MutableStateFlow<Int?>(null)
38     override val selectedClock: Flow<ClockMetadataModel> =
39         combine(
40             selectedClockId,
41             selectedColorId,
42             colorTone,
43             seedColor,
44         ) { selectedClockId, selectedColor, colorTone, seedColor ->
45             val selectedClock = fakeClocks.find { clock -> clock.clockId == selectedClockId }
46             checkNotNull(selectedClock)
47             ClockMetadataModel(
48                 clockId = selectedClock.clockId,
49                 isSelected = true,
50                 selectedColorId = selectedColor,
51                 colorToneProgress = colorTone,
52                 seedColor = seedColor,
53             )
54         }
55 
56     private val _selectedClockSize = MutableStateFlow(ClockSize.SMALL)
57     override val selectedClockSize: Flow<ClockSize> = _selectedClockSize.asStateFlow()
58 
59     override suspend fun setSelectedClock(clockId: String) {
60         selectedClockId.value = clockId
61     }
62 
63     override suspend fun setClockColor(
64         selectedColorId: String?,
65         @IntRange(from = 0, to = 100) colorToneProgress: Int,
66         @ColorInt seedColor: Int?,
67     ) {
68         this.selectedColorId.value = selectedColorId
69         this.colorTone.value = colorToneProgress
70         this.seedColor.value = seedColor
71     }
72 
73     override suspend fun setClockSize(size: ClockSize) {
74         _selectedClockSize.value = size
75     }
76 
77     companion object {
78         const val CLOCK_ID_0 = "clock0"
79         const val CLOCK_ID_1 = "clock1"
80         const val CLOCK_ID_2 = "clock2"
81         const val CLOCK_ID_3 = "clock3"
82         val fakeClocks =
83             listOf(
84                 ClockMetadataModel(CLOCK_ID_0, true, null, 50, null),
85                 ClockMetadataModel(CLOCK_ID_1, false, null, 50, null),
86                 ClockMetadataModel(CLOCK_ID_2, false, null, 50, null),
87                 ClockMetadataModel(CLOCK_ID_3, false, null, 50, null),
88             )
89         const val CLOCK_COLOR_ID = "RED"
90         const val CLOCK_COLOR_TONE_PROGRESS = 87
91         const val SEED_COLOR = Color.RED
92     }
93 }
94