1 /*
2  * Copyright (C) 2022 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.statusbar.notification.collection.listbuilder
18 
19 import com.android.systemui.statusbar.notification.collection.ListEntry
20 
21 object ShadeListBuilderHelper {
getSectionSubListsnull22     fun getSectionSubLists(entries: List<ListEntry>): Iterable<List<ListEntry>> =
23         getContiguousSubLists(entries, minLength = 1) { it.sectionIndex }
24 
getContiguousSubListsnull25     inline fun <T : Any, K : Any> getContiguousSubLists(
26         itemList: List<T>,
27         minLength: Int = 1,
28         key: (T) -> K,
29     ): Iterable<List<T>> {
30         val subLists = mutableListOf<List<T>>()
31         val numEntries = itemList.size
32         var currentSectionStartIndex = 0
33         var currentSectionKey: K? = null
34         for (i in 0 until numEntries) {
35             val sectionKey = key(itemList[i])
36             if (currentSectionKey == null) {
37                 currentSectionKey = sectionKey
38             } else if (currentSectionKey != sectionKey) {
39                 val length = i - currentSectionStartIndex
40                 if (length >= minLength) {
41                     subLists.add(itemList.subList(currentSectionStartIndex, i))
42                 }
43                 currentSectionStartIndex = i
44                 currentSectionKey = sectionKey
45             }
46         }
47         val length = numEntries - currentSectionStartIndex
48         if (length >= minLength) {
49             subLists.add(itemList.subList(currentSectionStartIndex, numEntries))
50         }
51         return subLists
52     }
53 }
54