mirror of
https://github.com/LawnchairLauncher/lawnchair.git
synced 2026-03-02 17:06:49 +00:00
Add TaskViewModel to fetch and map Tasks data
This commit introduces a new `TaskViewModel` which retrieves task data using `GetTaskUseCase` and maps them to `TaskTileUiState`. It also adds new files `TaskTileUiState` and `GetTaskUseCase` with their corresponding models. - `TaskTileUiState` is used to represent the UI state of a single overview tile. - Introduced `TaskData` sealed interface to encapsulate different states of a task. - `GetTaskUseCase` is used to fetch a single task given an id from the repository. - `TaskViewModel` fetches and maps tasks to `TaskUiState`. - Added unit tests for `TaskViewModel` to ensure the correct data is being emitted. - Added unit tests for `GetTaskUseCaseTest` for testing task retrieval logic. - `TaskModel` is the new data model representing the data fetched by the repository. Fix: 390578940 Fix: 390578937 Bug: 388486032 Flag: com.android.launcher3.enable_refactor_task_thumbnail Test: GetTaskUseCaseTest Test: TaskViewModelTest Change-Id: Ibf728eccc31270c0d0d8668a503e26d6d0e88f59
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2025 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.quickstep.recents.domain.model
|
||||
|
||||
import android.graphics.drawable.Drawable
|
||||
import com.android.systemui.shared.recents.model.ThumbnailData
|
||||
|
||||
/**
|
||||
* Data class representing a task in the application.
|
||||
*
|
||||
* This class holds the essential information about a task, including its unique identifier, display
|
||||
* title, associated icon, optional thumbnail data, and background color.
|
||||
*
|
||||
* @property id The unique identifier for this task. Must be an integer.
|
||||
* @property title The display title of the task.
|
||||
* @property titleDescription A content description of the task.
|
||||
* @property icon An optional drawable resource representing an icon for the task. Can be null if no
|
||||
* icon is required.
|
||||
* @property thumbnail An optional [ThumbnailData] object containing thumbnail information. Can be
|
||||
* null if no thumbnail is needed.
|
||||
* @property backgroundColor The background color of the task, represented as an integer color
|
||||
* value.
|
||||
* @property isLocked Indicates whether the [Task] is locked.
|
||||
*/
|
||||
data class TaskModel(
|
||||
val id: TaskId,
|
||||
val title: String,
|
||||
val titleDescription: String?,
|
||||
val icon: Drawable?,
|
||||
val thumbnail: ThumbnailData?,
|
||||
val backgroundColor: Int,
|
||||
val isLocked: Boolean,
|
||||
)
|
||||
|
||||
typealias TaskId = Int
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (C) 2025 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.quickstep.recents.domain.usecase
|
||||
|
||||
import com.android.quickstep.recents.data.RecentTasksRepository
|
||||
import com.android.quickstep.recents.domain.model.TaskModel
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
class GetTaskUseCase(private val repository: RecentTasksRepository) {
|
||||
operator fun invoke(taskId: Int): Flow<TaskModel?> =
|
||||
repository.getTaskDataById(taskId).map { task ->
|
||||
if (task != null) {
|
||||
TaskModel(
|
||||
id = task.key.id,
|
||||
title = task.title,
|
||||
titleDescription = task.titleDescription,
|
||||
icon = task.icon,
|
||||
thumbnail = task.thumbnail,
|
||||
backgroundColor = task.colorBackground,
|
||||
isLocked = task.isLocked,
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2025 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.quickstep.recents.ui.viewmodel
|
||||
|
||||
import android.graphics.drawable.Drawable
|
||||
import com.android.systemui.shared.recents.model.ThumbnailData
|
||||
|
||||
/**
|
||||
* This class represents the UI state to be consumed by TaskView, GroupTaskView and DesktopTaskView.
|
||||
* Data class representing the state of a list of tasks.
|
||||
*
|
||||
* This class encapsulates a list of [TaskTileUiState] objects, along with a flag indicating whether
|
||||
* the data is being used for a live tile display.
|
||||
*
|
||||
* @property tasks The list of [TaskTileUiState] objects representing the individual tasks.
|
||||
* @property isLiveTile Indicates whether this data is intended for a live tile. If `true`, the
|
||||
* running app will be displayed instead of the thumbnail.
|
||||
*/
|
||||
data class TaskTileUiState(val tasks: List<TaskData>, val isLiveTile: Boolean)
|
||||
|
||||
sealed interface TaskData {
|
||||
/** When no data was found for the TaskId provided */
|
||||
data class NoData(val taskId: Int) : TaskData
|
||||
|
||||
/**
|
||||
* This class provides UI information related to a Task (App) to be displayed within a TaskView.
|
||||
*
|
||||
* @property taskId Identifier of the task
|
||||
* @property title App title
|
||||
* @property icon App icon
|
||||
* @property thumbnailData Information related to the last snapshot retrieved from the app
|
||||
* @property backgroundColor The background color of the task.
|
||||
* @property isLocked Indicates whether the task is locked or not.
|
||||
*/
|
||||
data class Data(
|
||||
val taskId: Int,
|
||||
val title: String,
|
||||
val icon: Drawable?,
|
||||
val thumbnailData: ThumbnailData?,
|
||||
val backgroundColor: Int,
|
||||
val isLocked: Boolean,
|
||||
) : TaskData
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (C) 2025 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.quickstep.recents.ui.viewmodel
|
||||
|
||||
import android.util.Log
|
||||
import com.android.launcher3.util.coroutines.DispatcherProvider
|
||||
import com.android.quickstep.recents.domain.model.TaskId
|
||||
import com.android.quickstep.recents.domain.model.TaskModel
|
||||
import com.android.quickstep.recents.domain.usecase.GetTaskUseCase
|
||||
import com.android.quickstep.recents.viewmodel.RecentsViewData
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
/**
|
||||
* ViewModel used for [com.android.quickstep.views.TaskView],
|
||||
* [com.android.quickstep.views.DesktopTaskView] and [com.android.quickstep.views.GroupedTaskView].
|
||||
*/
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class TaskViewModel(
|
||||
recentsViewData: RecentsViewData,
|
||||
private val getTaskUseCase: GetTaskUseCase,
|
||||
dispatcherProvider: DispatcherProvider,
|
||||
) {
|
||||
private var taskIds = MutableStateFlow(emptySet<Int>())
|
||||
|
||||
private val isLiveTile =
|
||||
combine(
|
||||
taskIds,
|
||||
recentsViewData.runningTaskIds,
|
||||
recentsViewData.runningTaskShowScreenshot,
|
||||
) { taskIds, runningTaskIds, runningTaskShowScreenshot ->
|
||||
runningTaskIds == taskIds && !runningTaskShowScreenshot
|
||||
}
|
||||
.distinctUntilChanged()
|
||||
|
||||
val state: Flow<TaskTileUiState> =
|
||||
taskIds
|
||||
.flatMapLatest { ids ->
|
||||
// Combine Tasks requests
|
||||
combine(
|
||||
ids.map { id -> getTaskUseCase(id).map { taskModel -> id to taskModel } },
|
||||
::mapToUiState,
|
||||
)
|
||||
}
|
||||
.combine(isLiveTile) { tasks, isLiveTile -> TaskTileUiState(tasks, isLiveTile) }
|
||||
.flowOn(dispatcherProvider.background)
|
||||
|
||||
fun bind(vararg taskId: TaskId) {
|
||||
Log.d(TAG, "bind: $taskId")
|
||||
taskIds.value = taskId.toSet()
|
||||
}
|
||||
|
||||
private fun mapToUiState(result: Array<Pair<TaskId, TaskModel?>>): List<TaskData> =
|
||||
result.map { mapToUiState(it.first, it.second) }
|
||||
|
||||
private fun mapToUiState(taskId: TaskId, result: TaskModel?): TaskData =
|
||||
result?.let {
|
||||
TaskData.Data(
|
||||
taskId = taskId,
|
||||
title = result.title,
|
||||
icon = result.icon,
|
||||
thumbnailData = result.thumbnail,
|
||||
backgroundColor = result.backgroundColor,
|
||||
isLocked = result.isLocked,
|
||||
)
|
||||
} ?: TaskData.NoData(taskId)
|
||||
|
||||
private companion object {
|
||||
const val TAG = "TaskViewModel"
|
||||
}
|
||||
}
|
||||
@@ -613,6 +613,7 @@ constructor(
|
||||
borderEnabled = false
|
||||
hoverBorderVisible = false
|
||||
taskViewId = UNBOUND_TASK_VIEW_ID
|
||||
// TODO(b/390583187): Clean the components UI State when TaskView is recycled.
|
||||
taskContainers.forEach { it.destroy() }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user