• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.communal.util
18 
19 import com.android.systemui.communal.domain.model.CommunalContentModel
20 import com.android.systemui.communal.shared.model.CommunalContentSize
21 
22 object ResizeUtils {
23     /**
24      * Resizes ongoing items such that we don't mix regular content with ongoing content.
25      *
26      * NOTE: This is *NOT* a pure function, as it modifies items in the input list.
27      *
28      * Assumptions:
29      * 1. Ongoing content is always at the start of the list.
30      * 2. The maximum size of ongoing content is 2 rows.
31      */
resizeOngoingItemsnull32     fun resizeOngoingItems(
33         list: List<CommunalContentModel>,
34         numRows: Int,
35     ): List<CommunalContentModel> {
36         val finalizedList = mutableListOf<CommunalContentModel>()
37         val numOngoing = list.count { it is CommunalContentModel.Ongoing }
38         // Calculate the number of extra rows we have if each ongoing item were to take up a single
39         // row. This is the number of rows we have to distribute across items.
40         var extraRows =
41             if (numOngoing % numRows == 0) {
42                 0
43             } else {
44                 numRows - (numOngoing % numRows)
45             }
46         var remainingRows = numRows
47 
48         for (item in list) {
49             if (item is CommunalContentModel.Ongoing) {
50                 if (remainingRows == 0) {
51                     // Start a new column.
52                     remainingRows = numRows
53                 }
54                 val newSize = if (extraRows > 0 && remainingRows > 1) 2 else 1
55                 item.size = CommunalContentSize.Responsive(newSize)
56                 finalizedList.add(item)
57                 extraRows -= (newSize - 1)
58                 remainingRows -= newSize
59             } else {
60                 if (numOngoing > 0 && remainingRows > 0) {
61                     finalizedList.add(
62                         CommunalContentModel.Spacer(CommunalContentSize.Responsive(remainingRows))
63                     )
64                 }
65                 remainingRows = -1
66                 finalizedList.add(item)
67             }
68         }
69         return finalizedList
70     }
71 }
72