summaryrefslogtreecommitdiff
path: root/src/com/android/customization/picker/color/data/repository/FakeColorPickerRepository.kt
blob: bb2ef9d3317864a7fb6f660e3f4fa27863521af9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.android.customization.picker.color.data.repository

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import com.android.customization.model.color.ColorOptionImpl
import com.android.customization.model.color.ColorOptionsProvider
import com.android.customization.picker.color.shared.model.ColorOptionModel
import com.android.customization.picker.color.shared.model.ColorType
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

class FakeColorPickerRepository(private val context: Context) : ColorPickerRepository {

    private val _isApplyingSystemColor = MutableStateFlow(false)
    override val isApplyingSystemColor = _isApplyingSystemColor.asStateFlow()

    private lateinit var selectedColorOption: ColorOptionModel

    private val _colorOptions =
        MutableStateFlow(
            mapOf<ColorType, List<ColorOptionModel>>(
                ColorType.WALLPAPER_COLOR to listOf(),
                ColorType.PRESET_COLOR to listOf()
            )
        )
    override val colorOptions: StateFlow<Map<ColorType, List<ColorOptionModel>>> =
        _colorOptions.asStateFlow()

    init {
        setOptions(4, 4, ColorType.WALLPAPER_COLOR, 0)
    }

    fun setOptions(
        numWallpaperOptions: Int,
        numPresetOptions: Int,
        selectedColorOptionType: ColorType,
        selectedColorOptionIndex: Int
    ) {
        _colorOptions.value =
            mapOf(
                ColorType.WALLPAPER_COLOR to
                    buildList {
                        repeat(times = numWallpaperOptions) { index ->
                            val isSelected =
                                selectedColorOptionType == ColorType.WALLPAPER_COLOR &&
                                    selectedColorOptionIndex == index
                            val colorOption =
                                ColorOptionModel(
                                    key = "${ColorType.WALLPAPER_COLOR}::$index",
                                    colorOption = buildWallpaperOption(index),
                                    isSelected = isSelected,
                                )
                            if (isSelected) {
                                selectedColorOption = colorOption
                            }
                            add(colorOption)
                        }
                    },
                ColorType.PRESET_COLOR to
                    buildList {
                        repeat(times = numPresetOptions) { index ->
                            val isSelected =
                                selectedColorOptionType == ColorType.PRESET_COLOR &&
                                    selectedColorOptionIndex == index
                            val colorOption =
                                ColorOptionModel(
                                    key = "${ColorType.PRESET_COLOR}::$index",
                                    colorOption = buildPresetOption(index),
                                    isSelected = isSelected,
                                )
                            if (isSelected) {
                                selectedColorOption = colorOption
                            }
                            add(colorOption)
                        }
                    }
            )
    }

    private fun buildPresetOption(index: Int): ColorOptionImpl {
        val builder = ColorOptionImpl.Builder()
        builder.lightColors =
            intArrayOf(Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT)
        builder.darkColors =
            intArrayOf(Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT)
        builder.index = index
        builder.type = ColorType.PRESET_COLOR
        builder.source = ColorOptionsProvider.COLOR_SOURCE_PRESET
        builder.title = "Preset"
        builder
            .addOverlayPackage("TEST_PACKAGE_TYPE", "preset_color")
            .addOverlayPackage("TEST_PACKAGE_INDEX", "$index")
        return builder.build()
    }

    private fun buildWallpaperOption(index: Int): ColorOptionImpl {
        val builder = ColorOptionImpl.Builder()
        builder.lightColors =
            intArrayOf(Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT)
        builder.darkColors =
            intArrayOf(Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT)
        builder.index = index
        builder.type = ColorType.WALLPAPER_COLOR
        builder.source = ColorOptionsProvider.COLOR_SOURCE_HOME
        builder.title = "Dynamic"
        builder
            .addOverlayPackage("TEST_PACKAGE_TYPE", "wallpaper_color")
            .addOverlayPackage("TEST_PACKAGE_INDEX", "$index")
        return builder.build()
    }

    override suspend fun select(colorOptionModel: ColorOptionModel) {
        val colorOptions = _colorOptions.value
        val wallpaperColorOptions = colorOptions[ColorType.WALLPAPER_COLOR]!!
        val newWallpaperColorOptions = buildList {
            wallpaperColorOptions.forEach { option ->
                add(
                    ColorOptionModel(
                        key = option.key,
                        colorOption = option.colorOption,
                        isSelected = option.testEquals(colorOptionModel),
                    )
                )
            }
        }
        val basicColorOptions = colorOptions[ColorType.PRESET_COLOR]!!
        val newBasicColorOptions = buildList {
            basicColorOptions.forEach { option ->
                add(
                    ColorOptionModel(
                        key = option.key,
                        colorOption = option.colorOption,
                        isSelected = option.testEquals(colorOptionModel),
                    )
                )
            }
        }
        _colorOptions.value =
            mapOf(
                ColorType.WALLPAPER_COLOR to newWallpaperColorOptions,
                ColorType.PRESET_COLOR to newBasicColorOptions
            )
    }

    override fun getCurrentColorOption(): ColorOptionModel = selectedColorOption

    override fun getCurrentColorSource(): String? =
        when ((selectedColorOption.colorOption as ColorOptionImpl).type) {
            ColorType.WALLPAPER_COLOR -> ColorOptionsProvider.COLOR_SOURCE_HOME
            ColorType.PRESET_COLOR -> ColorOptionsProvider.COLOR_SOURCE_PRESET
            else -> null
        }

    private fun ColorOptionModel.testEquals(other: Any?): Boolean {
        if (other == null) {
            return false
        }
        return if (other is ColorOptionModel) {
            TextUtils.equals(this.key, other.key)
        } else {
            false
        }
    }
}