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

Commit c5a9a59b authored by George Lin's avatar George Lin
Browse files

Avoid clock carousel index-out-of-bound exception

1. Avoid jump to index when index out of bound
2. Set selected clock when setting up the clock carousel
3. This also resolves a problem that there's a delay when selecting an
   index. Since when setting up the clock carousel, we already know
   which index is selected.

Test: Manually tested there's no crash. See bug.
Bug: 289050926
Change-Id: Ib842b321bb3a29ce55789408a3a493a7dbb9367d
parent 719bae5b
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -57,7 +57,7 @@ class ClockPickerRepositoryImpl(
                        registry
                            .getClocks()
                            .filter { "NOT_IN_USE" !in it.clockId }
                            .map { it.toModel() }
                            .map { it.toModel(isSelected = it.clockId == registry.currentClockId) }
                    trySend(allClocks)
                }

@@ -91,6 +91,7 @@ class ClockPickerRepositoryImpl(
                            .getClocks()
                            .find { clockMetadata -> clockMetadata.clockId == currentClockId }
                            ?.toModel(
                                isSelected = true,
                                selectedColorId = metadata?.getSelectedColorId(),
                                colorTone = metadata?.getColorTone()
                                        ?: ClockMetadataModel.DEFAULT_COLOR_TONE_PROGRESS,
@@ -178,6 +179,7 @@ class ClockPickerRepositoryImpl(

    /** By default, [ClockMetadataModel] has no color information unless specified. */
    private fun ClockMetadata.toModel(
        isSelected: Boolean,
        selectedColorId: String? = null,
        @IntRange(from = 0, to = 100) colorTone: Int = 0,
        @ColorInt seedColor: Int? = null,
@@ -185,6 +187,7 @@ class ClockPickerRepositoryImpl(
        return ClockMetadataModel(
            clockId = clockId,
            name = name,
            isSelected = isSelected,
            selectedColorId = selectedColorId,
            colorToneProgress = colorTone,
            seedColor = seedColor,
+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import androidx.annotation.IntRange
data class ClockMetadataModel(
    val clockId: String,
    val name: String,
    val isSelected: Boolean,
    val selectedColorId: String?,
    @IntRange(from = 0, to = 100) val colorToneProgress: Int,
    @ColorInt val seedColor: Int?,
+12 −4
Original line number Diff line number Diff line
@@ -101,7 +101,13 @@ class ClockCarouselView(

        adapter = ClockCarouselAdapter(clockSize, clocks, clockViewFactory, onClockSelected)
        carousel.setAdapter(adapter)
        carousel.refresh()
        val indexOfSelectedClock =
            clocks
                .indexOfFirst { it.isSelected }
                // If not found, default to the first clock as selected:
                .takeIf { it != -1 }
                ?: 0
        carousel.jumpToIndex(indexOfSelectedClock)
        motionLayout.setTransitionListener(
            object : MotionLayout.TransitionListener {

@@ -265,9 +271,11 @@ class ClockCarouselView(
    fun setSelectedClockIndex(
        index: Int,
    ) {
        // jumpToIndex to the same position can cause the views unnecessarily populate again.
        // Only call jumpToIndex when the jump-to index is different from the current carousel.
        if (index != carousel.currentIndex) {
        // 1. setUpClockCarouselView() can possibly not be called before setSelectedClockIndex().
        //    We need to check if index out of bound.
        // 2. jumpToIndex() to the same position can cause the views unnecessarily populate again.
        //    We only call jumpToIndex when the index is different from the current carousel.
        if (index < carousel.count && index != carousel.currentIndex) {
            carousel.jumpToIndex(index)
        }
    }
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ import com.android.customization.module.CustomizationInjector
import com.android.wallpaper.R
import com.android.wallpaper.module.InjectorProvider

class ClockCarouselItemViewModel(val clockId: String) {
class ClockCarouselItemViewModel(val clockId: String, val isSelected: Boolean) {

    /** Description for accessibility purposes when a clock is selected. */
    fun getContentDescription(resources: Resources): String {
+1 −1
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ constructor(
            .mapLatest { allClocks ->
                // Delay to avoid the case that the full list of clocks is not initiated.
                delay(CLOCKS_EVENT_UPDATE_DELAY_MILLIS)
                allClocks.map { ClockCarouselItemViewModel(it.clockId) }
                allClocks.map { ClockCarouselItemViewModel(it.clockId, it.isSelected) }
            }
            .stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

Loading