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

Commit 57e034a0 authored by Darrell Shi's avatar Darrell Shi
Browse files

Reverse ranks order and add widgets at the end

- reverse ranks from high-to-low to low-to-high
- make rank an optional parameter
- move "add widget and shift others" logic to database level to be
  performed in a single transaction

Test: CommunalWidgetDaoTest
Test: verified widgets order remain consistent after db migration
Test: verified add widgets by tap and by drag & drop work correctly
Test: verified widgets added by tap remain in consistent order
Fix: 348041107
Flag: com.android.systemui.communal_hub
Change-Id: I7af9b535013ce0a696a0630bd991838a881e2495
parent 4f78f423
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line
@@ -93,24 +93,24 @@ internal constructor(
        newItemUser: UserHandle? = 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 rank.
        // New widget added to the grid. Other widgets are shifted as needed at the database level.
        if (newItemComponentName != null && newItemUser != null && newItemIndex != null) {
            onAddWidget(newItemComponentName, newItemUser, /* rank= */ newItemIndex)
            return
        }

        // No new widget, only reorder existing widgets.
        val widgetIdToRankMap: Map<Int, Int> =
            list
                .mapIndexedNotNull { index, item ->
                    if (item is CommunalContentModel.WidgetContent) {
                        item.appWidgetId to list.size - index
                        item.appWidgetId to index
                    } else {
                        null
                    }
                }
                .toMap()
        // reorder and then add the new widget
        onReorderWidgets(widgetIdToRankMap)
        if (newItemComponentName != null && newItemUser != null && newItemIndex != null) {
            onAddWidget(newItemComponentName, newItemUser, /* rank= */ list.size - newItemIndex)
        }
    }

    /** Returns true if the item at given index is editable. */
+3 −3
Original line number Diff line number Diff line
@@ -115,21 +115,21 @@ class DefaultWidgetPopulationTest : SysuiTestCase() {
                .addWidget(
                    widgetId = 0,
                    componentName = defaultWidgets[0],
                    rank = 3,
                    rank = 0,
                    userSerialNumber = 0,
                )
            verify(communalWidgetDao)
                .addWidget(
                    widgetId = 1,
                    componentName = defaultWidgets[1],
                    rank = 2,
                    rank = 1,
                    userSerialNumber = 0,
                )
            verify(communalWidgetDao)
                .addWidget(
                    widgetId = 2,
                    componentName = defaultWidgets[2],
                    rank = 1,
                    rank = 2,
                    userSerialNumber = 0,
                )
        }
+26 −5
Original line number Diff line number Diff line
@@ -97,7 +97,7 @@ constructor(
                            .addWidget(
                                widgetId = id,
                                componentName = name,
                                rank = defaultWidgets.size - index,
                                rank = index,
                                userSerialNumber = userSerialNumber,
                            )
                    }
@@ -132,10 +132,17 @@ interface CommunalWidgetDao {
    @Query(
        "SELECT * FROM communal_widget_table JOIN communal_item_rank_table " +
            "ON communal_item_rank_table.uid = communal_widget_table.item_id " +
            "ORDER BY communal_item_rank_table.rank DESC"
            "ORDER BY communal_item_rank_table.rank ASC"
    )
    fun getWidgets(): Flow<Map<CommunalItemRank, CommunalWidgetItem>>

    @Query(
        "SELECT * FROM communal_widget_table JOIN communal_item_rank_table " +
            "ON communal_item_rank_table.uid = communal_widget_table.item_id " +
            "ORDER BY communal_item_rank_table.rank ASC"
    )
    fun getWidgetsNow(): Map<CommunalItemRank, CommunalWidgetItem>

    @Query("SELECT * FROM communal_widget_table WHERE widget_id = :id")
    fun getWidgetByIdNow(id: Int): CommunalWidgetItem?

@@ -180,7 +187,7 @@ interface CommunalWidgetDao {
    fun addWidget(
        widgetId: Int,
        provider: ComponentName,
        rank: Int,
        rank: Int? = null,
        userSerialNumber: Int,
    ): Long {
        return addWidget(
@@ -195,13 +202,27 @@ interface CommunalWidgetDao {
    fun addWidget(
        widgetId: Int,
        componentName: String,
        rank: Int,
        rank: Int? = null,
        userSerialNumber: Int,
    ): Long {
        val widgets = getWidgetsNow()

        // If rank is not specified, rank it last by finding the current maximum rank and increment
        // by 1. If the new widget is the first widget, set the rank to 0.
        val newRank = rank ?: widgets.keys.maxOfOrNull { it.rank + 1 } ?: 0

        // Shift widgets after [rank], unless widget is added at the end.
        if (rank != null) {
            widgets.forEach { (rankEntry, widgetEntry) ->
                if (rankEntry.rank < newRank) return@forEach
                updateItemRank(widgetEntry.itemId, rankEntry.rank + 1)
            }
        }

        return insertWidget(
            widgetId = widgetId,
            componentName = componentName,
            itemId = insertItemRank(rank),
            itemId = insertItemRank(newRank),
            userSerialNumber = userSerialNumber,
        )
    }
+10 −5
Original line number Diff line number Diff line
@@ -57,12 +57,17 @@ interface CommunalWidgetRepository {
    /** A flow of information about active communal widgets stored in database. */
    val communalWidgets: Flow<List<CommunalWidgetContentModel>>

    /** Add a widget at the specified position in the app widget service and the database. */
    /**
     * Add a widget in the app widget service and the database.
     *
     * @param rank The rank of the widget determines its position in the grid. 0 is first place, 1
     *   is second, etc. If rank is not specified, widget is added at the end.
     */
    fun addWidget(
        provider: ComponentName,
        user: UserHandle,
        rank: Int,
        configurator: WidgetConfigurator? = null
        rank: Int?,
        configurator: WidgetConfigurator? = null,
    ) {}

    /**
@@ -151,8 +156,8 @@ constructor(
    override fun addWidget(
        provider: ComponentName,
        user: UserHandle,
        rank: Int,
        configurator: WidgetConfigurator?
        rank: Int?,
        configurator: WidgetConfigurator?,
    ) {
        bgScope.launch {
            val id = communalWidgetHost.allocateIdAndBindWidget(provider, user)
+5 −2
Original line number Diff line number Diff line
@@ -367,11 +367,14 @@ constructor(
    /** Dismiss the CTA tile from the hub in view mode. */
    suspend fun dismissCtaTile() = communalPrefsInteractor.setCtaDismissed()

    /** Add a widget at the specified position. */
    /**
     * Add a widget at the specified rank. If rank is not provided, the widget will be added at the
     * end.
     */
    fun addWidget(
        componentName: ComponentName,
        user: UserHandle,
        rank: Int,
        rank: Int? = null,
        configurator: WidgetConfigurator?,
    ) = widgetRepository.addWidget(componentName, user, rank, configurator)

Loading