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

Commit 2de50a18 authored by Catherine Liang's avatar Catherine Liang Committed by Android (Google) Code Review
Browse files

Merge "Make ColorUpdateViewModel ActivityRetainedScoped (2/2)" into main

parents cd10de6c 2035cf50
Loading
Loading
Loading
Loading
+6 −15
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import com.android.customization.picker.color.ui.viewmodel.ColorOptionIconViewMo
import com.android.themepicker.R
import com.android.wallpaper.picker.common.icon.ui.viewmodel.Icon
import com.android.wallpaper.picker.common.text.ui.viewmodel.Text
import com.android.wallpaper.picker.customization.ui.viewmodel.ColorUpdateViewModel
import com.android.wallpaper.picker.customization.ui.viewmodel.FloatingToolbarTabViewModel
import com.android.wallpaper.picker.option.ui.viewmodel.OptionItemViewModel2
import dagger.assisted.Assisted
@@ -33,8 +34,6 @@ import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.scopes.ViewModelScoped
import kotlin.coroutines.resume
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
@@ -43,14 +42,15 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine

/** Models UI state for a color picker experience. */
class ColorPickerViewModel2
@AssistedInject
constructor(
    @ApplicationContext context: Context,
    private val colorUpdateViewModel: ColorUpdateViewModel,
    private val interactor: ColorPickerInteractor,
    private val logger: ThemesUserEventLogger,
    @Assisted private val viewModelScope: CoroutineScope,
@@ -60,7 +60,6 @@ constructor(
    val previewingColorOption = overridingColorOption.asStateFlow()

    private val selectedColorTypeTabId = MutableStateFlow<ColorType?>(null)
    private var onApplyContinuation: CancellableContinuation<Unit>? = null

    /** View-models for each color tab. */
    val colorTypeTabs: Flow<List<FloatingToolbarTabViewModel>> =
@@ -181,11 +180,9 @@ constructor(
                } else {
                    {
                        interactor.select(it)
                        // Suspend until onApplyComplete is called, e.g. on configuration change
                        suspendCancellableCoroutine { continuation: CancellableContinuation<Unit> ->
                            onApplyContinuation?.cancel()
                            onApplyContinuation = continuation
                            continuation.invokeOnCancellation { onApplyContinuation = null }
                        // Suspend until first color update
                        colorUpdateViewModel.systemColorsUpdatedNoReplay.take(1).collect {
                            return@collect
                        }
                        logger.logThemeColorApplied(
                            previewingColorOption.colorOption.sourceForLogging,
@@ -201,12 +198,6 @@ constructor(
        overridingColorOption.value = null
    }

    /** Resumes the onApply function if apply is in progress, otherwise no-op */
    fun onApplyComplete() {
        onApplyContinuation?.resume(Unit)
        onApplyContinuation = null
    }

    /** The list of all available color options for the selected Color Type. */
    val colorOptions: Flow<List<OptionItemViewModel2<ColorOptionIconViewModel>>> =
        combine(allColorOptions, selectedColorTypeTabId) {
+0 −6
Original line number Diff line number Diff line
@@ -210,12 +210,6 @@ constructor(
                            }
                        }

                        launch {
                            colorUpdateViewModel.systemColorsUpdated.collect {
                                viewModel.colorPickerViewModel2.onApplyComplete()
                            }
                        }

                        launch {
                            combine(
                                    viewModel.colorPickerViewModel2.previewingColorOption,
+8 −2
Original line number Diff line number Diff line
@@ -28,12 +28,14 @@ import com.android.customization.picker.color.domain.interactor.ColorPickerSnaps
import com.android.customization.picker.color.shared.model.ColorType
import com.android.customization.picker.color.ui.viewmodel.ColorOptionIconViewModel
import com.android.systemui.monet.Style
import com.android.wallpaper.picker.customization.ui.viewmodel.ColorUpdateViewModel
import com.android.wallpaper.picker.customization.ui.viewmodel.FloatingToolbarTabViewModel
import com.android.wallpaper.picker.option.ui.viewmodel.OptionItemViewModel2
import com.android.wallpaper.testing.FakeSnapshotStore
import com.android.wallpaper.testing.collectLastValue
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertWithMessage
import dagger.hilt.android.internal.lifecycle.RetainedLifecycleImpl
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
@@ -58,6 +60,7 @@ class ColorPickerViewModel2Test {
    private lateinit var repository: FakeColorPickerRepository
    private lateinit var interactor: ColorPickerInteractor
    private lateinit var store: FakeSnapshotStore
    private lateinit var colorUpdateViewModel: ColorUpdateViewModel

    private lateinit var context: Context
    private lateinit var testScope: TestScope
@@ -80,11 +83,14 @@ class ColorPickerViewModel2Test {
                    },
            )

        colorUpdateViewModel = ColorUpdateViewModel(context, RetainedLifecycleImpl())

        underTest =
            ColorPickerViewModel2(
                context = context,
                interactor = interactor,
                logger = logger,
                colorUpdateViewModel = colorUpdateViewModel,
                viewModelScope = testScope.backgroundScope,
            )

@@ -112,7 +118,7 @@ class ColorPickerViewModel2Test {

            assertThat(job.isActive).isTrue()

            underTest.onApplyComplete()
            colorUpdateViewModel.updateColors()

            assertThat(job.isActive).isFalse()
        }
@@ -245,7 +251,7 @@ class ColorPickerViewModel2Test {
    private suspend fun TestScope.applySelectedColorOption() {
        val onApply = collectLastValue(underTest.onApply)()
        testScope.launch { onApply?.invoke() }
        underTest.onApplyComplete()
        colorUpdateViewModel.updateColors()
    }

    /**