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

Commit 9db8599f authored by Darrell Shi's avatar Darrell Shi
Browse files

Sort communal cards by priority

The communal layout library now orders cards based on their priority,
from highest to lowest.

Test: atest CommunalLayoutEngineTest
Bug: 300312458
Fix: 300312458
Change-Id: I98be78d7962092a9c00e0d1153ce6cb4dd959f4d
parent b7c89a1b
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@ class CommunalLayoutEngine {
         *
         * Currently treats the first supported size as the size to be rendered in, ignoring other
         * supported sizes.
         *
         * Cards are ordered by priority, from highest to lowest.
         */
        fun distributeCardsIntoColumns(
            cards: List<CommunalGridLayoutCard>,
@@ -37,7 +39,8 @@ class CommunalLayoutEngine {
            val result = ArrayList<ArrayList<CommunalGridLayoutCardInfo>>()

            var capacityOfLastColumn = 0
            for (card in cards) {
            val sorted = cards.sortedByDescending { it.priority }
            for (card in sorted) {
                val cardSize = card.supportedSizes.first()
                if (capacityOfLastColumn >= cardSize.value) {
                    // Card fits in last column
+48 −3
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ class CommunalLayoutEngineTest {
                ),
            )

        assertDistribution(cards, expected)
        assertDistributionBySize(cards, expected)
    }

    @Test
@@ -71,10 +71,30 @@ class CommunalLayoutEngineTest {
                ),
            )

        assertDistribution(cards, expected)
        assertDistributionBySize(cards, expected)
    }

    private fun assertDistribution(
    @Test
    fun distribution_sortByPriority() {
        val cards =
            listOf(
                generateCard(priority = 2),
                generateCard(priority = 7),
                generateCard(priority = 10),
                generateCard(priority = 1),
                generateCard(priority = 5),
            )
        val expected =
            listOf(
                listOf(10, 7),
                listOf(5, 2),
                listOf(1),
            )

        assertDistributionByPriority(cards, expected)
    }

    private fun assertDistributionBySize(
        cards: List<CommunalGridLayoutCard>,
        expected: List<List<CommunalGridLayoutCard.Size>>,
    ) {
@@ -87,6 +107,19 @@ class CommunalLayoutEngineTest {
        }
    }

    private fun assertDistributionByPriority(
        cards: List<CommunalGridLayoutCard>,
        expected: List<List<Int>>,
    ) {
        val result = CommunalLayoutEngine.distributeCardsIntoColumns(cards)

        for (c in expected.indices) {
            for (r in expected[c].indices) {
                assertThat(result[c][r].card.priority).isEqualTo(expected[c][r])
            }
        }
    }

    private fun generateCard(size: CommunalGridLayoutCard.Size): CommunalGridLayoutCard {
        return object : CommunalGridLayoutCard() {
            override val supportedSizes = listOf(size)
@@ -97,4 +130,16 @@ class CommunalLayoutEngineTest {
            }
        }
    }

    private fun generateCard(priority: Int): CommunalGridLayoutCard {
        return object : CommunalGridLayoutCard() {
            override val supportedSizes = listOf(Size.HALF)
            override val priority = priority

            @Composable
            override fun Content(modifier: Modifier, size: SizeF) {
                Card(modifier = modifier, content = {})
            }
        }
    }
}