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
92 lines
3.3 KiB
Kotlin
92 lines
3.3 KiB
Kotlin
/*
|
|
* 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"
|
|
}
|
|
}
|