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

Commit 1e6c421f authored by Will Leshner's avatar Will Leshner
Browse files

Fix an IndexOutOfBoundsException when dragging to add widgets.

Bug: 359992427
Test: Manually by dragging many widgets and verifying there is no crash.
Flag: EXEMPT bugfix
Change-Id: Ieb0e75044877ee7ee4e000ad98c72fc21b64dc69
parent 782ffd0b
Loading
Loading
Loading
Loading
+12 −12
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.selection.selectable
@@ -687,21 +688,20 @@ private fun BoxScope.CommunalHubLazyGrid(
        horizontalArrangement = Arrangement.spacedBy(Dimensions.ItemSpacing),
        verticalArrangement = Arrangement.spacedBy(Dimensions.ItemSpacing),
    ) {
        items(
            count = list.size,
            key = { index -> list[index].key },
            contentType = { index -> list[index].key },
            span = { index -> GridItemSpan(list[index].size.span) },
        ) { index ->
        itemsIndexed(
            items = list,
            key = { _, item -> item.key },
            contentType = { _, item -> item.key },
            span = { _, item -> GridItemSpan(item.size.span) },
        ) { index, item ->
            val size =
                SizeF(
                    Dimensions.CardWidth.value,
                    list[index].size.dp().value,
                    item.size.dp().value,
                )
            val cardModifier = Modifier.requiredSize(width = size.width.dp, height = size.height.dp)
            if (viewModel.isEditMode && dragDropState != null) {
                val selected by
                    remember(index) { derivedStateOf { list[index].key == selectedKey.value } }
                val selected = item.key == selectedKey.value
                DraggableItem(
                    modifier =
                        if (dragDropState.draggingItemIndex == index) {
@@ -713,12 +713,12 @@ private fun BoxScope.CommunalHubLazyGrid(
                        },
                    dragDropState = dragDropState,
                    selected = selected,
                    enabled = list[index].isWidgetContent(),
                    enabled = item.isWidgetContent(),
                    index = index,
                ) { isDragging ->
                    CommunalContent(
                        modifier = cardModifier,
                        model = list[index],
                        model = item,
                        viewModel = viewModel,
                        size = size,
                        selected = selected && !isDragging,
@@ -731,7 +731,7 @@ private fun BoxScope.CommunalHubLazyGrid(
                }
            } else {
                CommunalContent(
                    model = list[index],
                    model = item,
                    viewModel = viewModel,
                    size = size,
                    selected = false,