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

Commit 69244376 authored by Shamali P's avatar Shamali P
Browse files

Update the reorder function to also support adding item during reorder.

- Dao class today accepts a priority when adding widget, so on the same
lines updated the updateOrder function to accept mapping of widget ids
to priorities.
- By accepting priorities, it also makes the input ordering more
explicit (instead of ordered list)
- Updated tests to also test for "order" using `.inOrder()`

Test: atest CommunalWidgetDaoTest
Bug: 315209740
Flag: ACONFIG com.android.systemui.communal_hub DEVELOPMENT

Change-Id: I594afb33a61c49f6bba3307c21f16a3506d07c3c
parent bb4db1a7
Loading
Loading
Loading
Loading
+32 −6
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.systemui.communal.ui.compose

import android.content.ComponentName
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
@@ -32,6 +33,7 @@ fun rememberContentListState(
    return remember(communalContent) {
        ContentListState(
            communalContent,
            viewModel::onAddWidget,
            viewModel::onDeleteWidget,
            viewModel::onReorderWidgets,
        )
@@ -46,8 +48,9 @@ fun rememberContentListState(
class ContentListState
internal constructor(
    communalContent: List<CommunalContentModel>,
    private val onAddWidget: (componentName: ComponentName, priority: Int) -> Unit,
    private val onDeleteWidget: (id: Int) -> Unit,
    private val onReorderWidgets: (ids: List<Int>) -> Unit,
    private val onReorderWidgets: (widgetIdToPriorityMap: Map<Int, Int>) -> Unit,
) {
    var list by mutableStateOf(communalContent)
        private set
@@ -66,10 +69,33 @@ internal constructor(
        }
    }

    /** Persist the new order with all the movements happened during dragging. */
    fun onSaveList() {
        val widgetIds: List<Int> =
            list.filterIsInstance<CommunalContentModel.Widget>().map { it.appWidgetId }
        onReorderWidgets(widgetIds)
    /**
     * Persists the new order with all the movements happened during drag operations & the new
     * widget drop (if applicable).
     *
     * @param newItemComponentName name of the new widget that was dropped into the list; null if no
     * new widget was added.
     * @param newItemIndex index at which the a new widget was dropped into the list; null if no new
     * widget was dropped.
     */
    fun onSaveList(newItemComponentName: ComponentName? = null, newItemIndex: Int? = null) {
        // filters placeholder, but, maintains the indices of the widgets as if the placeholder was
        // in the list. When persisted in DB, this leaves space for the new item (to be added) at
        // the correct priority.
        val widgetIdToPriorityMap: Map<Int, Int> =
            list
                .mapIndexedNotNull { index, item ->
                    if (item is CommunalContentModel.Widget) {
                        item.appWidgetId to list.size - index
                    } else {
                        null
                    }
                }
                .toMap()
        // reorder and then add the new widget
        onReorderWidgets(widgetIdToPriorityMap)
        if (newItemComponentName != null && newItemIndex != null) {
            onAddWidget(newItemComponentName, /*priority=*/ list.size - newItemIndex)
        }
    }
}
+3 −3
Original line number Diff line number Diff line
@@ -208,11 +208,11 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
            val repository = initCommunalWidgetRepository()
            runCurrent()

            val ids = listOf(104, 103, 101)
            repository.updateWidgetOrder(ids)
            val widgetIdToPriorityMap = mapOf(104 to 1, 103 to 2, 101 to 3)
            repository.updateWidgetOrder(widgetIdToPriorityMap)
            runCurrent()

            verify(communalWidgetDao).updateWidgetOrder(ids)
            verify(communalWidgetDao).updateWidgetOrder(widgetIdToPriorityMap)
        }

    @Test
+5 −5
Original line number Diff line number Diff line
@@ -117,10 +117,10 @@ interface CommunalWidgetDao {
    fun updateItemRank(itemUid: Long, order: Int)

    @Transaction
    fun updateWidgetOrder(ids: List<Int>) {
        ids.forEachIndexed { index, it ->
            val widget = getWidgetByIdNow(it)
            updateItemRank(widget.itemId, ids.size - index)
    fun updateWidgetOrder(widgetIdToPriorityMap: Map<Int, Int>) {
        widgetIdToPriorityMap.forEach { (id, priority) ->
            val widget = getWidgetByIdNow(id)
            updateItemRank(widget.itemId, priority)
        }
    }

@@ -129,7 +129,7 @@ interface CommunalWidgetDao {
        return insertWidget(
            widgetId = widgetId,
            componentName = provider.flattenToString(),
            insertItemRank(priority),
            itemId = insertItemRank(priority),
        )
    }

+9 −5
Original line number Diff line number Diff line
@@ -62,8 +62,12 @@ interface CommunalWidgetRepository {
    /** Delete a widget by id from app widget service and the database. */
    fun deleteWidget(widgetId: Int) {}

    /** Update the order of widgets in the database. */
    fun updateWidgetOrder(ids: List<Int>) {}
    /**
     * Update the order of widgets in the database.
     *
     * @param widgetIdToPriorityMap mapping of the widget ids to the priority of the widget.
     */
    fun updateWidgetOrder(widgetIdToPriorityMap: Map<Int, Int>) {}
}

@OptIn(ExperimentalCoroutinesApi::class)
@@ -168,11 +172,11 @@ constructor(
        }
    }

    override fun updateWidgetOrder(ids: List<Int>) {
    override fun updateWidgetOrder(widgetIdToPriorityMap: Map<Int, Int>) {
        applicationScope.launch(bgDispatcher) {
            communalWidgetDao.updateWidgetOrder(ids)
            communalWidgetDao.updateWidgetOrder(widgetIdToPriorityMap)
            logger.i({ "Updated the order of widget list with ids: $str1." }) {
                str1 = ids.toString()
                str1 = widgetIdToPriorityMap.toString()
            }
        }
    }
+7 −2
Original line number Diff line number Diff line
@@ -102,8 +102,13 @@ constructor(
    /** Delete a widget by id. */
    fun deleteWidget(id: Int) = widgetRepository.deleteWidget(id)

    /** Reorder widgets. The order in the list will be their display order in the hub. */
    fun updateWidgetOrder(ids: List<Int>) = widgetRepository.updateWidgetOrder(ids)
    /**
     * Reorder the widgets.
     *
     * @param widgetIdToPriorityMap mapping of the widget ids to their new priorities.
     */
    fun updateWidgetOrder(widgetIdToPriorityMap: Map<Int, Int>) =
        widgetRepository.updateWidgetOrder(widgetIdToPriorityMap)

    /** A list of widget content to be displayed in the communal hub. */
    val widgetContent: Flow<List<CommunalContentModel.Widget>> =
Loading