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.qs.panels.shared.model
18 
19 /** Represents a tile of type [T] associated with a width */
20 data class SizedTile<T>(val tile: T, val width: Int)
21 
22 /** Represents a row of [SizedTile] with a maximum width of [columns] */
23 class TileRow<T>(private val columns: Int) {
24     private var availableColumns = columns
25     private val _tiles: MutableList<SizedTile<T>> = mutableListOf()
26     val tiles: List<SizedTile<T>>
27         get() = _tiles.toList()
28 
maybeAddTilenull29     fun maybeAddTile(tile: SizedTile<T>): Boolean {
30         if (availableColumns - tile.width >= 0) {
31             _tiles.add(tile)
32             availableColumns -= tile.width
33             return true
34         }
35         return false
36     }
37 
findLastIconTilenull38     fun findLastIconTile(): SizedTile<T>? {
39         return _tiles.findLast { it.width == 1 }
40     }
41 
removeTilenull42     fun removeTile(tile: SizedTile<T>) {
43         _tiles.remove(tile)
44         availableColumns += tile.width
45     }
46 
clearnull47     fun clear() {
48         _tiles.clear()
49         availableColumns = columns
50     }
51 
isFullnull52     fun isFull(): Boolean = availableColumns == 0
53 }
54