Merge changes from topic "blackflash-main" into main

* changes:
  Reuse ClockViewFactory
  Cache affordances and selection picker side
diff --git a/src/com/android/customization/module/ThemePickerInjector.kt b/src/com/android/customization/module/ThemePickerInjector.kt
index 749048a..1975ad1 100644
--- a/src/com/android/customization/module/ThemePickerInjector.kt
+++ b/src/com/android/customization/module/ThemePickerInjector.kt
@@ -15,6 +15,7 @@
  */
 package com.android.customization.module
 
+import android.app.Activity
 import android.app.UiModeManager
 import android.app.WallpaperManager
 import android.content.Context
@@ -105,7 +106,7 @@
     private var notificationsSnapshotRestorer: NotificationsSnapshotRestorer? = null
     private var clockPickerInteractor: ClockPickerInteractor? = null
     private var clockCarouselViewModelFactory: ClockCarouselViewModel.Factory? = null
-    private var clockViewFactories: MutableMap<Int, ClockViewFactory> = HashMap()
+    private var clockViewFactory: ClockViewFactory? = null
     private var clockPickerSnapshotRestorer: ClockPickerSnapshotRestorer? = null
     private var notificationsInteractor: NotificationsInteractor? = null
     private var notificationSectionViewModelFactory: NotificationSectionViewModel.Factory? = null
@@ -247,7 +248,7 @@
         val client = getKeyguardQuickAffordancePickerProviderClient(context)
         val appContext = context.applicationContext
         return KeyguardQuickAffordancePickerInteractor(
-            KeyguardQuickAffordancePickerRepository(client),
+            KeyguardQuickAffordancePickerRepository(client, getApplicationCoroutineScope()),
             client
         ) {
             getKeyguardQuickAffordanceSnapshotRestorer(appContext)
@@ -353,8 +354,7 @@
     }
 
     override fun getClockViewFactory(activity: ComponentActivity): ClockViewFactory {
-        val activityHashCode = activity.hashCode()
-        return clockViewFactories[activityHashCode]
+        return clockViewFactory
             ?: ClockViewFactoryImpl(
                     activity.applicationContext,
                     ScreenSizeCalculator.getInstance()
@@ -363,13 +363,13 @@
                     getClockRegistry(activity.applicationContext),
                 )
                 .also {
-                    clockViewFactories[activityHashCode] = it
+                    clockViewFactory = it
                     activity.lifecycle.addObserver(
                         object : DefaultLifecycleObserver {
                             override fun onDestroy(owner: LifecycleOwner) {
                                 super.onDestroy(owner)
-                                clockViewFactories[activityHashCode]?.onDestroy()
-                                clockViewFactories.remove(activityHashCode)
+                                if ((owner as Activity).isChangingConfigurations()) return
+                                clockViewFactory?.onDestroy()
                             }
                         }
                     )
diff --git a/src/com/android/customization/picker/quickaffordance/data/repository/KeyguardQuickAffordancePickerRepository.kt b/src/com/android/customization/picker/quickaffordance/data/repository/KeyguardQuickAffordancePickerRepository.kt
index 10473a2..6bfe348 100644
--- a/src/com/android/customization/picker/quickaffordance/data/repository/KeyguardQuickAffordancePickerRepository.kt
+++ b/src/com/android/customization/picker/quickaffordance/data/repository/KeyguardQuickAffordancePickerRepository.kt
@@ -21,8 +21,11 @@
 import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSelectionModel as SelectionModel
 import com.android.customization.picker.quickaffordance.shared.model.KeyguardQuickAffordancePickerSlotModel as SlotModel
 import com.android.systemui.shared.customization.data.content.CustomizationProviderClient as Client
+import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.SharingStarted
 import kotlinx.coroutines.flow.map
+import kotlinx.coroutines.flow.shareIn
 
 /**
  * Abstracts access to application state related to functionality for selecting, picking, or setting
@@ -30,6 +33,7 @@
  */
 class KeyguardQuickAffordancePickerRepository(
     private val client: Client,
+    private val scope: CoroutineScope
 ) {
     /** List of slots available on the device. */
     val slots: Flow<List<SlotModel>> =
@@ -37,15 +41,17 @@
 
     /** List of all available quick affordances. */
     val affordances: Flow<List<AffordanceModel>> =
-        client.observeAffordances().map { affordances ->
-            affordances.map { affordance -> affordance.toModel() }
-        }
+        client
+            .observeAffordances()
+            .map { affordances -> affordances.map { affordance -> affordance.toModel() } }
+            .shareIn(scope, replay = 1, started = SharingStarted.Lazily)
 
     /** List of slot-affordance pairs, modeling what the user has currently chosen for each slot. */
     val selections: Flow<List<SelectionModel>> =
-        client.observeSelections().map { selections ->
-            selections.map { selection -> selection.toModel() }
-        }
+        client
+            .observeSelections()
+            .map { selections -> selections.map { selection -> selection.toModel() } }
+            .shareIn(scope, replay = 1, started = SharingStarted.Lazily)
 
     private fun Client.Slot.toModel(): SlotModel {
         return SlotModel(
diff --git a/tests/robotests/src/com/android/customization/model/picker/quickaffordance/data/repository/KeyguardQuickAffordancePickerRepositoryTest.kt b/tests/robotests/src/com/android/customization/model/picker/quickaffordance/data/repository/KeyguardQuickAffordancePickerRepositoryTest.kt
index 8a5d582..8687b30 100644
--- a/tests/robotests/src/com/android/customization/model/picker/quickaffordance/data/repository/KeyguardQuickAffordancePickerRepositoryTest.kt
+++ b/tests/robotests/src/com/android/customization/model/picker/quickaffordance/data/repository/KeyguardQuickAffordancePickerRepositoryTest.kt
@@ -20,6 +20,7 @@
 import androidx.test.filters.SmallTest
 import com.android.customization.picker.quickaffordance.data.repository.KeyguardQuickAffordancePickerRepository
 import com.android.systemui.shared.customization.data.content.FakeCustomizationProviderClient
+import com.google.common.truth.Truth.assertThat
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.test.TestScope
@@ -28,6 +29,7 @@
 import kotlinx.coroutines.test.setMain
 import org.junit.After
 import org.junit.Before
+import org.junit.Test
 import org.junit.runner.RunWith
 import org.robolectric.RobolectricTestRunner
 
@@ -51,9 +53,16 @@
         underTest =
             KeyguardQuickAffordancePickerRepository(
                 client = client,
+                scope = testScope.backgroundScope,
             )
     }
 
+    // We need at least one test to prevent Studio errors
+    @Test
+    fun creationSucceeds() {
+        assertThat(underTest).isNotNull()
+    }
+
     @After
     fun tearDown() {
         Dispatchers.resetMain()
diff --git a/tests/robotests/src/com/android/customization/model/picker/quickaffordance/domain/interactor/KeyguardQuickAffordancePickerInteractorTest.kt b/tests/robotests/src/com/android/customization/model/picker/quickaffordance/domain/interactor/KeyguardQuickAffordancePickerInteractorTest.kt
index 11098ec..bf53f61 100644
--- a/tests/robotests/src/com/android/customization/model/picker/quickaffordance/domain/interactor/KeyguardQuickAffordancePickerInteractorTest.kt
+++ b/tests/robotests/src/com/android/customization/model/picker/quickaffordance/domain/interactor/KeyguardQuickAffordancePickerInteractorTest.kt
@@ -62,6 +62,7 @@
                 repository =
                     KeyguardQuickAffordancePickerRepository(
                         client = client,
+                        scope = testScope.backgroundScope,
                     ),
                 client = client,
                 snapshotRestorer = {
diff --git a/tests/robotests/src/com/android/customization/model/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModelTest.kt b/tests/robotests/src/com/android/customization/model/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModelTest.kt
index a9da1c3..3221024 100644
--- a/tests/robotests/src/com/android/customization/model/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModelTest.kt
+++ b/tests/robotests/src/com/android/customization/model/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModelTest.kt
@@ -89,6 +89,7 @@
                 repository =
                     KeyguardQuickAffordancePickerRepository(
                         client = client,
+                        scope = testScope.backgroundScope,
                     ),
                 client = client,
                 snapshotRestorer = {