1 /* 2 * Copyright (C) 2023 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.pipeline.data.repository 18 19 import android.util.Log 20 import com.android.systemui.qs.pipeline.data.repository.TileSpecRepository.Companion.POSITION_AT_END 21 import com.android.systemui.qs.pipeline.shared.TileSpec 22 import kotlinx.coroutines.flow.Flow 23 import kotlinx.coroutines.flow.MutableStateFlow 24 import kotlinx.coroutines.flow.asStateFlow 25 26 class FakeTileSpecRepository : TileSpecRepository { 27 28 private val tilesPerUser = mutableMapOf<Int, MutableStateFlow<List<TileSpec>>>() 29 30 override fun tilesSpecs(userId: Int): Flow<List<TileSpec>> { 31 return getFlow(userId).asStateFlow().also { Log.d("Fabian", "Retrieving flow for $userId") } 32 } 33 34 override suspend fun addTile(userId: Int, tile: TileSpec, position: Int) { 35 if (tile == TileSpec.Invalid) return 36 with(getFlow(userId)) { 37 value = 38 value.toMutableList().apply { 39 if (position == POSITION_AT_END) { 40 add(tile) 41 } else { 42 add(position, tile) 43 } 44 } 45 } 46 } 47 48 override suspend fun removeTiles(userId: Int, tiles: Collection<TileSpec>) { 49 with(getFlow(userId)) { 50 value = 51 value.toMutableList().apply { removeAll(tiles.filter { it != TileSpec.Invalid }) } 52 } 53 } 54 55 override suspend fun setTiles(userId: Int, tiles: List<TileSpec>) { 56 getFlow(userId).value = tiles.filter { it != TileSpec.Invalid } 57 } 58 59 private fun getFlow(userId: Int): MutableStateFlow<List<TileSpec>> = 60 tilesPerUser.getOrPut(userId) { MutableStateFlow(emptyList()) } 61 } 62