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

Commit 2e202b00 authored by Coco Duan's avatar Coco Duan
Browse files

Fix crash when dragging widget to remove in hub edit mode

It happens when the last item is taller than the dragged item, as we
add a Spacer at the end of the list when drag starts and save its
initial index. This index becomes invalid (out of bound) after the dragged widget is removed.
The fix is to check if the last item is a Spacer item on drag ends and remove it if so.

Fixes: 415898439
Test: on device
Flag: EXEMPT bugfix
Change-Id: I188cf0454df955830b5b830fcc77e5aa7fef87d2
parent 260412b0
Loading
Loading
Loading
Loading
+12 −12
Original line number Diff line number Diff line
@@ -209,7 +209,6 @@ private class GridDragDropStateV1(
    private val scrollChannel = Channel<Float>()

    private val spacer = CommunalContentModel.Spacer(CommunalContentSize.Responsive(1))
    private var spacerIndex: Int? = null

    private var previousTargetItemKey: Any? = null

@@ -248,7 +247,6 @@ private class GridDragDropStateV1(
                        lastWidget.size.span > draggingItemLayoutInfo!!.span
                ) {
                    contentListState.list.add(spacer)
                    spacerIndex = contentListState.list.size - 1
                }
                return true
            }
@@ -272,10 +270,12 @@ private class GridDragDropStateV1(
        previousTargetItemKey = null
        draggingItemDraggedDelta = Offset.Zero
        draggingItemInitialOffset = Offset.Zero
        // Remove spacer, if any, when a drag gesture finishes.
        spacerIndex?.let {
            contentListState.list.removeAt(it)
            spacerIndex = null
        // Remove spacer, if one is added at the end, when a drag gesture finishes.
        if (
            contentListState.list.isNotEmpty() &&
                contentListState.list.last() is CommunalContentModel.Spacer
        ) {
            contentListState.list.removeLast()
        }
    }

@@ -395,7 +395,6 @@ private class GridDragDropStateV2(
    private var draggingItemWhileScrolling: LazyGridItemInfo? by mutableStateOf(null)

    private val spacer = CommunalContentModel.Spacer(CommunalContentSize.Responsive(1))
    private var spacerIndex: Int? = null

    private var previousTargetItemKey: Any? = null

@@ -468,7 +467,6 @@ private class GridDragDropStateV2(
                        lastWidget.size.span > draggingItemLayoutInfo!!.span
                ) {
                    contentListState.list.add(spacer)
                    spacerIndex = contentListState.list.size - 1
                }
                return true
            }
@@ -494,10 +492,12 @@ private class GridDragDropStateV2(
        draggingItemInitialOffset = Offset.Zero
        currentDragPositionOnScreen = Offset.Zero
        draggingItemWhileScrolling = null
        // Remove spacer, if any, when a drag gesture finishes.
        spacerIndex?.let {
            contentListState.list.removeAt(it)
            spacerIndex = null
        // Remove spacer, if one is added at the end, when a drag gesture finishes.
        if (
            contentListState.list.isNotEmpty() &&
                contentListState.list.last() is CommunalContentModel.Spacer
        ) {
            contentListState.list.removeLast()
        }
    }