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

Commit 412c9bd7 authored by George Lin's avatar George Lin
Browse files

[TP] Smooth scroll to clock color option position

We should smooth scroll to clock color option position to make sure the
selected option is shown on the screen.

Test: manually tested that the selected color option shows on the screen
Bug: 270097085
Change-Id: Ie6cb75cac2c07f72d0ae1937a9bf932be1dd2de7
parent f486be70
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -131,6 +131,18 @@ object ClockSettingsBinder {
                    }
                }

                launch {
                    viewModel.selectedColorOptionPosition.collect { selectedPosition ->
                        if (selectedPosition != -1) {
                            // We use "post" because we need to give the adapter item a pass to
                            // update the view.
                            colorOptionContainerView.post {
                                colorOptionContainerView.smoothScrollToPosition(selectedPosition)
                            }
                        }
                    }
                }

                launch {
                    viewModel.selectedClockSize.collect { size ->
                        when (size) {
+4 −0
Original line number Diff line number Diff line
@@ -181,6 +181,10 @@ private constructor(
                initialValue = emptyList(),
            )

    @OptIn(ExperimentalCoroutinesApi::class)
    val selectedColorOptionPosition: Flow<Int> =
        colorOptions.mapLatest { it.indexOfFirst { colorOption -> colorOption.isSelected } }

    private fun ColorSeedOption.toColorOptionViewModel(
        context: Context,
        selectedColorId: String?,
+4 −0
Original line number Diff line number Diff line
@@ -82,18 +82,22 @@ class ClockSettingsViewModelTest {
    @Test
    fun setSelectedColor() = runTest {
        val observedClockColorOptions = collectLastValue(underTest.colorOptions)
        val observedSelectedColorOptionPosition =
            collectLastValue(underTest.selectedColorOptionPosition)
        val observedSliderProgress = collectLastValue(underTest.sliderProgress)
        val observedSeedColor = collectLastValue(underTest.seedColor)
        // Advance COLOR_OPTIONS_EVENT_UPDATE_DELAY_MILLIS since there is a delay from colorOptions
        advanceTimeBy(ClockSettingsViewModel.COLOR_OPTIONS_EVENT_UPDATE_DELAY_MILLIS)
        assertThat(observedClockColorOptions()!![0].isSelected).isTrue()
        assertThat(observedClockColorOptions()!![0].onClick).isNull()
        assertThat(observedSelectedColorOptionPosition()).isEqualTo(0)

        observedClockColorOptions()!![1].onClick?.invoke()
        // Advance COLOR_OPTIONS_EVENT_UPDATE_DELAY_MILLIS since there is a delay from colorOptions
        advanceTimeBy(ClockSettingsViewModel.COLOR_OPTIONS_EVENT_UPDATE_DELAY_MILLIS)
        assertThat(observedClockColorOptions()!![1].isSelected).isTrue()
        assertThat(observedClockColorOptions()!![1].onClick).isNull()
        assertThat(observedSelectedColorOptionPosition()).isEqualTo(1)
        assertThat(observedSliderProgress())
            .isEqualTo(ClockMetadataModel.DEFAULT_COLOR_TONE_PROGRESS)
        val expectedSelectedColorModel = colorMap.values.first() // RED