Simplify viewModel

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-02-29 10:39:39 +01:00 committed by Alper Öztürk
parent d39d5257ac
commit a52ec924ab
2 changed files with 25 additions and 34 deletions

View file

@ -25,10 +25,8 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.nextcloud.client.assistant.repository.AssistantRepository
import com.nextcloud.common.NextcloudClient
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.resources.assistant.model.TaskList
import com.owncloud.android.lib.resources.assistant.model.Task
import com.owncloud.android.lib.resources.assistant.model.TaskType
import com.owncloud.android.lib.resources.assistant.model.TaskTypes
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@ -42,13 +40,13 @@ class AssistantViewModel(client: NextcloudClient) : ViewModel() {
private val _selectedTaskType = MutableStateFlow<TaskType?>(null)
val selectedTaskType: StateFlow<TaskType?> = _selectedTaskType
private val _taskTypes = MutableStateFlow<RemoteOperationResult<TaskTypes>?>(null)
val taskTypes: StateFlow<RemoteOperationResult<TaskTypes>?> = _taskTypes
private val _taskTypes = MutableStateFlow<List<TaskType>?>(null)
val taskTypes: StateFlow<List<TaskType>?> = _taskTypes
private var _taskList: RemoteOperationResult<TaskList>? = null
private var _taskList: List<Task>? = null
private val _filteredTaskList = MutableStateFlow<RemoteOperationResult<TaskList>?>(null)
val filteredTaskList: StateFlow<RemoteOperationResult<TaskList>?> = _filteredTaskList
private val _filteredTaskList = MutableStateFlow<List<Task>?>(null)
val filteredTaskList: StateFlow<List<Task>?> = _filteredTaskList
private val _loading = MutableStateFlow(true)
val loading: StateFlow<Boolean> = _loading
@ -86,26 +84,21 @@ class AssistantViewModel(client: NextcloudClient) : ViewModel() {
private fun getTaskTypes() {
viewModelScope.launch(Dispatchers.IO) {
val result = repository.getTaskTypes()
val new = ArrayList(result.resultData.types ?: listOf()).apply {
add(TaskType(null, "All", null))
}
result.resultData.types = new
val result = repository.getTaskTypes().resultData.types
_taskTypes.update {
result
}
_selectedTaskType.update {
result.resultData.types?.first()
result?.first()
}
}
}
fun getTaskList(appId: String = "assistant", onCompleted: () -> Unit = {}) {
viewModelScope.launch(Dispatchers.IO) {
_taskList = repository.getTaskList(appId)
_taskList = repository.getTaskList(appId).resultData.tasks
filterTaskList(_selectedTaskType.value?.id)
@ -131,26 +124,21 @@ class AssistantViewModel(client: NextcloudClient) : ViewModel() {
}
}
fun filterTaskList(taskTypeId: String?) {
private fun filterTaskList(taskTypeId: String?) {
if (taskTypeId == null) {
_filteredTaskList.update {
_taskList
}
} else {
val result = _taskList?.resultData?.tasks?.filter { it.type == taskTypeId }
_filteredTaskList.update {
it?.resultData?.tasks = result
it
_taskList?.filter { it.type == taskTypeId }
}
}
}
private fun removeTaskFromList(id: Long) {
_filteredTaskList.update { currentList ->
currentList?.resultData?.tasks?.let { tasks ->
currentList.resultData.tasks = tasks.filter { it.id != id }
}
currentList
currentList?.filter { it.id != id }
}
}
}

View file

@ -96,13 +96,16 @@ fun AssistantScreen(viewModel: AssistantViewModel, floatingActionButton: Floatin
if (loading || pullRefreshState.isRefreshing) {
CenterText(text = stringResource(id = R.string.assistant_screen_loading))
} else {
val tasks = filteredTaskList?.resultData?.tasks ?: return
val types = taskTypes?.resultData?.types ?: return
AssistantContent(tasks, types, selectedTaskType, viewModel, showDeleteTaskAlertDialog = { taskId ->
taskIdToDeleted = taskId
showDeleteTaskAlertDialog = true
})
AssistantContent(
filteredTaskList ?: listOf(),
taskTypes,
selectedTaskType,
viewModel,
showDeleteTaskAlertDialog = { taskId ->
taskIdToDeleted = taskId
showDeleteTaskAlertDialog = true
}
)
}
if (pullRefreshState.isRefreshing) {
@ -137,9 +140,9 @@ fun AssistantScreen(viewModel: AssistantViewModel, floatingActionButton: Floatin
SimpleAlertDialog(
backgroundColor = Color.White,
textColor = Color.Black,
title =stringResource(id = R.string.assistant_screen_delete_task_alert_dialog_title),
title = stringResource(id = R.string.assistant_screen_delete_task_alert_dialog_title),
description = stringResource(id = R.string.assistant_screen_delete_task_alert_dialog_description),
dismiss = {showDeleteTaskAlertDialog = false },
dismiss = { showDeleteTaskAlertDialog = false },
onComplete = { viewModel.deleteTask(id) },
)
}
@ -158,7 +161,7 @@ fun AssistantScreen(viewModel: AssistantViewModel, floatingActionButton: Floatin
@Composable
private fun AssistantContent(
taskList: List<Task>,
taskTypes: List<TaskType>,
taskTypes: List<TaskType>?,
selectedTask: TaskType?,
viewModel: AssistantViewModel,
showDeleteTaskAlertDialog: (Long) -> Unit,