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

Commit 92e34b22 authored by Lucas Silva's avatar Lucas Silva
Browse files

Clear selected key when widget removed

If a widget is removed, clear the selected key if the widget being
removed was selected.

Fixes: 395072939
Test: atest CommunalEditModeViewModelTest
Flag: EXEMPT bugfix
Change-Id: I2d626a441fed098118e24580493250bb7bb65283
parent 4094749b
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -57,7 +57,8 @@ class ContentListState
internal constructor(
    communalContent: List<CommunalContentModel>,
    private val onAddWidget: (componentName: ComponentName, user: UserHandle, rank: Int) -> Unit,
    private val onDeleteWidget: (id: Int, componentName: ComponentName, rank: Int) -> Unit,
    private val onDeleteWidget:
        (id: Int, key: String, componentName: ComponentName, rank: Int) -> Unit,
    private val onReorderWidgets: (widgetIdToRankMap: Map<Int, Int>) -> Unit,
    private val onResizeWidget:
        (
@@ -81,7 +82,7 @@ internal constructor(
        if (list[indexToRemove].isWidgetContent()) {
            val widget = list[indexToRemove] as CommunalContentModel.WidgetContent
            list.apply { removeAt(indexToRemove) }
            onDeleteWidget(widget.appWidgetId, widget.componentName, widget.rank)
            onDeleteWidget(widget.appWidgetId, widget.key, widget.componentName, widget.rank)
        }
    }

+19 −0
Original line number Diff line number Diff line
@@ -201,6 +201,7 @@ class CommunalEditModeViewModelTest : SysuiTestCase() {

            underTest.onDeleteWidget(
                id = 0,
                key = "key_0",
                componentName = ComponentName("test_package", "test_class"),
                rank = 30,
            )
@@ -213,6 +214,24 @@ class CommunalEditModeViewModelTest : SysuiTestCase() {
            assertThat(appWidgetId).isEqualTo(1)
        }

    @Test
    fun deleteWidget_clearsSelectedKey() =
        kosmos.runTest {
            val selectedKey by collectLastValue(underTest.selectedKey)
            underTest.setSelectedKey("test_key")
            assertThat(selectedKey).isEqualTo("test_key")

            // Selected key is deleted.
            underTest.onDeleteWidget(
                id = 0,
                key = "test_key",
                componentName = ComponentName("test_package", "test_class"),
                rank = 30,
            )

            assertThat(selectedKey).isNull()
        }

    @Test
    fun reorderWidget_uiEventLogging_start() =
        kosmos.runTest {
+1 −1
Original line number Diff line number Diff line
@@ -153,7 +153,7 @@ abstract class BaseCommunalViewModel(
    ) {}

    /** Called as the UI requests deleting a widget. */
    open fun onDeleteWidget(id: Int, componentName: ComponentName, rank: Int) {}
    open fun onDeleteWidget(id: Int, key: String, componentName: ComponentName, rank: Int) {}

    /** Called as the UI detects a tap event on the widget. */
    open fun onTapWidget(componentName: ComponentName, rank: Int) {}
+4 −1
Original line number Diff line number Diff line
@@ -141,7 +141,10 @@ constructor(
        metricsLogger.logAddWidget(componentName.flattenToString(), rank)
    }

    override fun onDeleteWidget(id: Int, componentName: ComponentName, rank: Int) {
    override fun onDeleteWidget(id: Int, key: String, componentName: ComponentName, rank: Int) {
        if (selectedKey.value == key) {
            setSelectedKey(null)
        }
        communalInteractor.deleteWidget(id)
        metricsLogger.logRemoveWidget(componentName.flattenToString(), rank)
    }