1 /*
<lambda>null2  * 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.quickstep.recents.data
18 
19 import android.graphics.drawable.Drawable
20 import com.android.systemui.shared.recents.model.Task
21 import com.android.systemui.shared.recents.model.ThumbnailData
22 import kotlinx.coroutines.flow.Flow
23 import kotlinx.coroutines.flow.MutableStateFlow
24 import kotlinx.coroutines.flow.combine
25 import kotlinx.coroutines.flow.map
26 
27 class FakeTasksRepository : RecentTasksRepository {
28     private var thumbnailDataMap: Map<Int, ThumbnailData> = emptyMap()
29     private var taskIconDataMap: Map<Int, FakeIconData> = emptyMap()
30     private var tasks: MutableStateFlow<List<Task>> = MutableStateFlow(emptyList())
31     private var visibleTasks: MutableStateFlow<Set<Int>> = MutableStateFlow(emptySet())
32 
33     override fun getAllTaskData(forceRefresh: Boolean): Flow<List<Task>> = tasks
34 
35     override fun getTaskDataById(taskId: Int): Flow<Task?> =
36         combine(getAllTaskData(), visibleTasks) { taskList, visibleTasks ->
37                 taskList.filter { visibleTasks.contains(it.key.id) }
38             }
39             .map { taskList ->
40                 val task = taskList.firstOrNull { it.key.id == taskId } ?: return@map null
41                 Task(task).apply {
42                     thumbnail = task.thumbnail
43                     icon = task.icon
44                     titleDescription = task.titleDescription
45                     title = task.title
46                 }
47             }
48 
49     override fun getThumbnailById(taskId: Int): Flow<ThumbnailData?> =
50         getTaskDataById(taskId).map { it?.thumbnail }
51 
52     override fun setVisibleTasks(visibleTaskIdList: Set<Int>) {
53         visibleTasks.value = visibleTaskIdList
54         tasks.value =
55             tasks.value.map {
56                 it.apply {
57                     thumbnail = thumbnailDataMap[it.key.id]
58                     taskIconDataMap[it.key.id].let { data ->
59                         title = data?.title
60                         titleDescription = data?.titleDescription
61                         icon = data?.icon
62                     }
63                 }
64             }
65     }
66 
67     fun seedTasks(tasks: List<Task>) {
68         this.tasks.value = tasks
69     }
70 
71     fun seedThumbnailData(thumbnailDataMap: Map<Int, ThumbnailData>) {
72         this.thumbnailDataMap = thumbnailDataMap
73     }
74 
75     fun seedIconData(id: Int, title: String, contentDescription: String, icon: Drawable) {
76         val iconData = FakeIconData(icon, contentDescription, title)
77         this.taskIconDataMap = mapOf(id to iconData)
78     }
79 
80     private data class FakeIconData(
81         val icon: Drawable,
82         val titleDescription: String,
83         val title: String,
84     )
85 }
86