Adjust Color Provider to handle consecutive calls

Color Provider needs to be able to handle consecutive calls because
there are 2 section controllers collecting from the color flow on the
main page. To do so, wallpaper color loading is made synchronous, and
the colors are stored once loaded, so that they can be re-used if there
are no wallpaper color changes.

Flag: NONE
Bug: 326293722
Test: manually verified that wallpaper color options display in both
home and lock color sections and the color picker fragment
Test: regression tested color picker features including pausing and
resuming app and changing light/dark theme, no regressions observed

Change-Id: I2bd7b958b8959dfbd77e5d62da3b0fec75bd4169
diff --git a/src/com/android/customization/model/color/ColorProvider.kt b/src/com/android/customization/model/color/ColorProvider.kt
index 79925d8..eb8d8df 100644
--- a/src/com/android/customization/model/color/ColorProvider.kt
+++ b/src/com/android/customization/model/color/ColorProvider.kt
@@ -79,7 +79,8 @@
         }
 
     private var colorsAvailable = true
-    private var colorBundles: List<ColorOption>? = null
+    private var presetColorBundles: List<ColorOption>? = null
+    private var wallpaperColorBundles: List<ColorOption>? = null
     private var homeWallpaperColors: WallpaperColors? = null
     private var lockWallpaperColors: WallpaperColors? = null
 
@@ -96,31 +97,27 @@
         val wallpaperColorsChanged =
             this.homeWallpaperColors != homeWallpaperColors ||
                 this.lockWallpaperColors != lockWallpaperColors
-        if (wallpaperColorsChanged) {
+        if (wallpaperColorsChanged || reload) {
+            loadSeedColors(
+                homeWallpaperColors,
+                lockWallpaperColors,
+            )
             this.homeWallpaperColors = homeWallpaperColors
             this.lockWallpaperColors = lockWallpaperColors
         }
-        if (colorBundles == null || reload || wallpaperColorsChanged) {
+        if (presetColorBundles == null || reload) {
             scope.launch {
                 try {
-                    if (colorBundles == null || reload) {
-                        loadPreset()
-                    }
-                    if (wallpaperColorsChanged || reload) {
-                        loadSeedColors(
-                            homeWallpaperColors,
-                            lockWallpaperColors,
-                        )
-                    }
+                    loadPreset()
                 } catch (e: Throwable) {
                     colorsAvailable = false
                     callback?.onError(e)
                     return@launch
                 }
-                callback?.onOptionsLoaded(colorBundles)
+                callback?.onOptionsLoaded(buildFinalList())
             }
         } else {
-            callback?.onOptionsLoaded(colorBundles)
+            callback?.onOptionsLoaded(buildFinalList())
         }
     }
 
@@ -173,19 +170,7 @@
                 bundles,
             )
         }
-
-        // Insert monochrome in the second position if it is enabled and included in preset
-        // colors
-        if (InjectorProvider.getInjector().getFlags().isMonochromaticThemeEnabled(mContext)) {
-            monochromeBundleName?.let {
-                bundles.add(1, buildPreset(it, -1, Style.MONOCHROMATIC, ColorType.WALLPAPER_COLOR))
-            }
-        }
-        bundles.addAll(
-            colorBundles?.filterNot { (it as ColorOptionImpl).type == ColorType.WALLPAPER_COLOR }
-                ?: emptyList()
-        )
-        colorBundles = bundles
+        wallpaperColorBundles = bundles
     }
 
     private fun buildColorSeeds(
@@ -381,7 +366,7 @@
                 monochromeBundleName = null
             }
 
-            colorBundles = bundles
+            presetColorBundles = bundles
         }
 
     private fun buildPreset(
@@ -425,4 +410,22 @@
         builder.darkColors = darkColors
         return builder.build()
     }
+
+    private fun buildFinalList(): List<ColorOption> {
+        val presetColors = presetColorBundles ?: emptyList()
+        val wallpaperColors = wallpaperColorBundles?.toMutableList() ?: mutableListOf()
+        // Insert monochrome in the second position if it is enabled and included in preset
+        // colors
+        if (InjectorProvider.getInjector().getFlags().isMonochromaticThemeEnabled(mContext)) {
+            monochromeBundleName?.let {
+                if (wallpaperColors.isNotEmpty()) {
+                    wallpaperColors.add(
+                        1,
+                        buildPreset(it, -1, Style.MONOCHROMATIC, ColorType.WALLPAPER_COLOR)
+                    )
+                }
+            }
+        }
+        return wallpaperColors + presetColors
+    }
 }