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

Revert submission 23676200-wpp-initial-colors-cleanup

Reason for revert: Color option no longer update when switching wallpapers when testing with newest build

Reverted changes: /q/submissionid:23676200-wpp-initial-colors-cleanup

Change-Id: Ie11de2a5cb0f5de53610a37fc79556879251c8f2
diff --git a/src/com/android/customization/module/DefaultCustomizationSections.java b/src/com/android/customization/module/DefaultCustomizationSections.java
index 00f9433..b408a89 100644
--- a/src/com/android/customization/module/DefaultCustomizationSections.java
+++ b/src/com/android/customization/module/DefaultCustomizationSections.java
@@ -145,7 +145,6 @@
                                         activity,
                                         mColorPickerViewModelFactory)
                                         .get(ColorPickerViewModel.class),
-                                wallpaperColorsViewModel,
                                 lifecycleOwner),
                         // Wallpaper quick switch section.
                         new WallpaperQuickSwitchSectionController(
diff --git a/src/com/android/customization/module/ThemePickerInjector.kt b/src/com/android/customization/module/ThemePickerInjector.kt
index d9ad4fe..6c747d7 100644
--- a/src/com/android/customization/module/ThemePickerInjector.kt
+++ b/src/com/android/customization/module/ThemePickerInjector.kt
@@ -442,8 +442,7 @@
                     repository =
                         ColorPickerRepositoryImpl(
                             wallpaperColorsViewModel,
-                            getColorCustomizationManager(appContext),
-                            WallpaperManager.getInstance(appContext),
+                            getColorCustomizationManager(appContext)
                         ),
                     snapshotRestorer = {
                         getColorPickerSnapshotRestorer(appContext, wallpaperColorsViewModel)
diff --git a/src/com/android/customization/picker/color/data/repository/ColorPickerRepository.kt b/src/com/android/customization/picker/color/data/repository/ColorPickerRepository.kt
index ba77f25..7cf9fd0 100644
--- a/src/com/android/customization/picker/color/data/repository/ColorPickerRepository.kt
+++ b/src/com/android/customization/picker/color/data/repository/ColorPickerRepository.kt
@@ -37,7 +37,4 @@
 
     /** 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()
 }
diff --git a/src/com/android/customization/picker/color/data/repository/ColorPickerRepositoryImpl.kt b/src/com/android/customization/picker/color/data/repository/ColorPickerRepositoryImpl.kt
index f1c695d..41ef3a5 100644
--- a/src/com/android/customization/picker/color/data/repository/ColorPickerRepositoryImpl.kt
+++ b/src/com/android/customization/picker/color/data/repository/ColorPickerRepositoryImpl.kt
@@ -16,7 +16,6 @@
  */
 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
@@ -27,20 +26,17 @@
 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(
-    private val wallpaperColorsViewModel: WallpaperColorsViewModel,
+    wallpaperColorsViewModel: WallpaperColorsViewModel,
     private val colorManager: ColorCustomizationManager,
-    private val wallpaperManager: WallpaperManager,
 ) : ColorPickerRepository {
 
     private val homeWallpaperColors: StateFlow<WallpaperColorsModel?> =
@@ -155,17 +151,6 @@
         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}",
diff --git a/src/com/android/customization/picker/color/data/repository/FakeColorPickerRepository.kt b/src/com/android/customization/picker/color/data/repository/FakeColorPickerRepository.kt
index e4b8795..714129d 100644
--- a/src/com/android/customization/picker/color/data/repository/FakeColorPickerRepository.kt
+++ b/src/com/android/customization/picker/color/data/repository/FakeColorPickerRepository.kt
@@ -166,8 +166,6 @@
             else -> null
         }
 
-    override suspend fun loadInitialColors() {}
-
     private fun ColorOptionModel.testEquals(other: Any?): Boolean {
         if (other == null) {
             return false
diff --git a/src/com/android/customization/picker/color/domain/interactor/ColorPickerInteractor.kt b/src/com/android/customization/picker/color/domain/interactor/ColorPickerInteractor.kt
index 36d2a0d..8c7a4b7 100644
--- a/src/com/android/customization/picker/color/domain/interactor/ColorPickerInteractor.kt
+++ b/src/com/android/customization/picker/color/domain/interactor/ColorPickerInteractor.kt
@@ -46,6 +46,4 @@
     }
 
     fun getCurrentColorOption(): ColorOptionModel = repository.getCurrentColorOption()
-
-    suspend fun loadInitialColors() = repository.loadInitialColors()
 }
diff --git a/src/com/android/customization/picker/color/ui/fragment/ColorPickerFragment.kt b/src/com/android/customization/picker/color/ui/fragment/ColorPickerFragment.kt
index 3390a0d..78bfa43 100644
--- a/src/com/android/customization/picker/color/ui/fragment/ColorPickerFragment.kt
+++ b/src/com/android/customization/picker/color/ui/fragment/ColorPickerFragment.kt
@@ -28,7 +28,6 @@
 import com.android.customization.model.mode.DarkModeSectionController
 import com.android.customization.module.ThemePickerInjector
 import com.android.customization.picker.color.ui.binder.ColorPickerBinder
-import com.android.customization.picker.color.ui.viewmodel.ColorPickerViewModel
 import com.android.wallpaper.R
 import com.android.wallpaper.model.WallpaperColorsModel
 import com.android.wallpaper.model.WallpaperColorsViewModel
@@ -74,29 +73,20 @@
         val wallpaperInfoFactory = injector.getCurrentWallpaperInfoFactory(requireContext())
         val displayUtils: DisplayUtils = injector.getDisplayUtils(requireContext())
         val wcViewModel = injector.getWallpaperColorsViewModel()
-
-        val colorPickerViewModel =
-            ViewModelProvider(
-                    requireActivity(),
-                    injector.getColorPickerViewModelFactory(
-                        context = requireContext(),
-                        wallpaperColorsViewModel = wcViewModel,
-                    ),
-                )
-                .get() as ColorPickerViewModel
-
-        // load wallpaper colors if it has not been populated
-        if (
-            wcViewModel.lockWallpaperColors.value is WallpaperColorsModel.Loading ||
-                wcViewModel.homeWallpaperColors.value is WallpaperColorsModel.Loading
-        ) {
-            lifecycleScope.launch { colorPickerViewModel.loadInitialColors() }
-        }
+        val wallpaperManager = WallpaperManager.getInstance(requireContext())
 
         binding =
             ColorPickerBinder.bind(
                 view = view,
-                viewModel = colorPickerViewModel,
+                viewModel =
+                    ViewModelProvider(
+                            requireActivity(),
+                            injector.getColorPickerViewModelFactory(
+                                context = requireContext(),
+                                wallpaperColorsViewModel = wcViewModel,
+                            ),
+                        )
+                        .get(),
                 lifecycleOwner = this,
             )
 
@@ -120,6 +110,18 @@
                         suspendCancellableCoroutine { continuation ->
                             wallpaperInfoFactory.createCurrentWallpaperInfos(
                                 { homeWallpaper, lockWallpaper, _ ->
+                                    lifecycleScope.launch {
+                                        if (
+                                            wcViewModel.lockWallpaperColors.value
+                                                is WallpaperColorsModel.Loading
+                                        ) {
+                                            loadInitialColors(
+                                                wallpaperManager,
+                                                wcViewModel,
+                                                CustomizationSections.Screen.LOCK_SCREEN
+                                            )
+                                        }
+                                    }
                                     continuation.resume(lockWallpaper ?: homeWallpaper, null)
                                 },
                                 forceReload,
@@ -155,6 +157,18 @@
                         suspendCancellableCoroutine { continuation ->
                             wallpaperInfoFactory.createCurrentWallpaperInfos(
                                 { homeWallpaper, lockWallpaper, _ ->
+                                    lifecycleScope.launch {
+                                        if (
+                                            wcViewModel.homeWallpaperColors.value
+                                                is WallpaperColorsModel.Loading
+                                        ) {
+                                            loadInitialColors(
+                                                wallpaperManager,
+                                                wcViewModel,
+                                                CustomizationSections.Screen.HOME_SCREEN
+                                            )
+                                        }
+                                    }
                                     continuation.resume(homeWallpaper ?: lockWallpaper, null)
                                 },
                                 forceReload,
diff --git a/src/com/android/customization/picker/color/ui/section/ColorSectionController2.kt b/src/com/android/customization/picker/color/ui/section/ColorSectionController2.kt
index d0ddb2b..f1c982b 100644
--- a/src/com/android/customization/picker/color/ui/section/ColorSectionController2.kt
+++ b/src/com/android/customization/picker/color/ui/section/ColorSectionController2.kt
@@ -20,7 +20,6 @@
 import android.content.Context
 import android.view.LayoutInflater
 import androidx.lifecycle.LifecycleOwner
-import androidx.lifecycle.lifecycleScope
 import com.android.customization.picker.color.ui.binder.ColorSectionViewBinder
 import com.android.customization.picker.color.ui.fragment.ColorPickerFragment
 import com.android.customization.picker.color.ui.view.ColorSectionView2
@@ -28,14 +27,10 @@
 import com.android.wallpaper.R
 import com.android.wallpaper.model.CustomizationSectionController
 import com.android.wallpaper.model.CustomizationSectionController.CustomizationSectionNavigationController as NavigationController
-import com.android.wallpaper.model.WallpaperColorsModel
-import com.android.wallpaper.model.WallpaperColorsViewModel
-import kotlinx.coroutines.launch
 
 class ColorSectionController2(
     private val navigationController: NavigationController,
     private val viewModel: ColorPickerViewModel,
-    private val wcViewModel: WallpaperColorsViewModel,
     private val lifecycleOwner: LifecycleOwner
 ) : CustomizationSectionController<ColorSectionView2> {
 
@@ -58,15 +53,6 @@
                     R.layout.color_section_view2,
                     null,
                 ) as ColorSectionView2
-
-        // load wallpaper colors if it has not been populated
-        if (
-            wcViewModel.lockWallpaperColors.value is WallpaperColorsModel.Loading ||
-                wcViewModel.homeWallpaperColors.value is WallpaperColorsModel.Loading
-        ) {
-            lifecycleOwner.lifecycleScope.launch { viewModel.loadInitialColors() }
-        }
-
         ColorSectionViewBinder.bind(
             view = view,
             viewModel = viewModel,
diff --git a/src/com/android/customization/picker/color/ui/viewmodel/ColorPickerViewModel.kt b/src/com/android/customization/picker/color/ui/viewmodel/ColorPickerViewModel.kt
index 057eff5..67c6838 100644
--- a/src/com/android/customization/picker/color/ui/viewmodel/ColorPickerViewModel.kt
+++ b/src/com/android/customization/picker/color/ui/viewmodel/ColorPickerViewModel.kt
@@ -202,8 +202,6 @@
                 replay = 1,
             )
 
-    suspend fun loadInitialColors() = interactor.loadInitialColors()
-
     class Factory(
         private val context: Context,
         private val interactor: ColorPickerInteractor,
diff --git a/src/com/android/customization/picker/preview/ui/section/PreviewWithThemeSectionController.kt b/src/com/android/customization/picker/preview/ui/section/PreviewWithThemeSectionController.kt
index b630122..56c6c30 100644
--- a/src/com/android/customization/picker/preview/ui/section/PreviewWithThemeSectionController.kt
+++ b/src/com/android/customization/picker/preview/ui/section/PreviewWithThemeSectionController.kt
@@ -94,6 +94,10 @@
                                 } else {
                                     homeWallpaper ?: lockWallpaper
                                 }
+                            loadInitialColors(
+                                context = context,
+                                screen = screen,
+                            )
                             continuation.resume(wallpaper, null)
                         },
                         forceReload,