Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ddee5638 authored by Catherine Liang's avatar Catherine Liang
Browse files

Clean up and refactor load initial colors (1/3)

Follow up of Ic724ad2b233e3244baec05fbedbb853f2aa39521 to place load
initial colors in a better location.

Bug: 280737870
Test: Manually verified, see bug
Change-Id: I379dfc997d14499854a4d13d5f572b5005432680
parent af5e7005
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -145,6 +145,7 @@ public final class DefaultCustomizationSections implements CustomizationSections
                                        activity,
                                        mColorPickerViewModelFactory)
                                        .get(ColorPickerViewModel.class),
                                wallpaperColorsViewModel,
                                lifecycleOwner),
                        // Wallpaper quick switch section.
                        new WallpaperQuickSwitchSectionController(
+2 −1
Original line number Diff line number Diff line
@@ -442,7 +442,8 @@ open class ThemePickerInjector : WallpaperPicker2Injector(), CustomizationInject
                    repository =
                        ColorPickerRepositoryImpl(
                            wallpaperColorsViewModel,
                            getColorCustomizationManager(appContext)
                            getColorCustomizationManager(appContext),
                            WallpaperManager.getInstance(appContext),
                        ),
                    snapshotRestorer = {
                        getColorPickerSnapshotRestorer(appContext, wallpaperColorsViewModel)
+3 −0
Original line number Diff line number Diff line
@@ -37,4 +37,7 @@ interface ColorPickerRepository {

    /** Returns the current selected color source based on system settings */
    fun getCurrentColorSource(): String?

    /** Retrieves and stores the wallpaper colors for generating wallpaper color options */
    suspend fun loadInitialColors()
}
+16 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
 */
package com.android.customization.picker.color.data.repository

import android.app.WallpaperManager
import android.util.Log
import com.android.customization.model.CustomizationManager
import com.android.customization.model.color.ColorCustomizationManager
@@ -26,17 +27,20 @@ import com.android.customization.picker.color.shared.model.ColorType
import com.android.systemui.monet.Style
import com.android.wallpaper.model.WallpaperColorsModel
import com.android.wallpaper.model.WallpaperColorsViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext

// TODO (b/262924623): refactor to remove dependency on ColorCustomizationManager & ColorOption
// TODO (b/268203200): Create test for ColorPickerRepositoryImpl
class ColorPickerRepositoryImpl(
    wallpaperColorsViewModel: WallpaperColorsViewModel,
    private val wallpaperColorsViewModel: WallpaperColorsViewModel,
    private val colorManager: ColorCustomizationManager,
    private val wallpaperManager: WallpaperManager,
) : ColorPickerRepository {

    private val homeWallpaperColors: StateFlow<WallpaperColorsModel?> =
@@ -151,6 +155,17 @@ class ColorPickerRepositoryImpl(
        return colorManager.currentColorSource
    }

    override suspend fun loadInitialColors() {
        withContext(Dispatchers.IO) {
            val lockColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK)
            val homeColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM)
            withContext(Dispatchers.Main) {
                wallpaperColorsViewModel.setLockWallpaperColors(lockColors)
                wallpaperColorsViewModel.setHomeWallpaperColors(homeColors)
            }
        }
    }

    private fun ColorOptionImpl.toModel(): ColorOptionModel {
        return ColorOptionModel(
            key = "${this.type}::${this.style}::${this.serializedPackages}",
+2 −0
Original line number Diff line number Diff line
@@ -166,6 +166,8 @@ class FakeColorPickerRepository(private val context: Context) : ColorPickerRepos
            else -> null
        }

    override suspend fun loadInitialColors() {}

    private fun ColorOptionModel.testEquals(other: Any?): Boolean {
        if (other == null) {
            return false
Loading