Loading packages/SystemUI/communal/layout/src/com/android/systemui/communal/layout/CommunalLayoutEngine.kt +4 −1 Original line number Diff line number Diff line Loading @@ -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>, Loading @@ -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 Loading packages/SystemUI/communal/layout/tests/src/com/android/systemui/communal/layout/CommunalLayoutEngineTest.kt +48 −3 Original line number Diff line number Diff line Loading @@ -41,7 +41,7 @@ class CommunalLayoutEngineTest { ), ) assertDistribution(cards, expected) assertDistributionBySize(cards, expected) } @Test Loading Loading @@ -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>>, ) { Loading @@ -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) Loading @@ -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 = {}) } } } } Loading
packages/SystemUI/communal/layout/src/com/android/systemui/communal/layout/CommunalLayoutEngine.kt +4 −1 Original line number Diff line number Diff line Loading @@ -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>, Loading @@ -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 Loading
packages/SystemUI/communal/layout/tests/src/com/android/systemui/communal/layout/CommunalLayoutEngineTest.kt +48 −3 Original line number Diff line number Diff line Loading @@ -41,7 +41,7 @@ class CommunalLayoutEngineTest { ), ) assertDistribution(cards, expected) assertDistributionBySize(cards, expected) } @Test Loading Loading @@ -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>>, ) { Loading @@ -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) Loading @@ -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 = {}) } } } }