From a52ec924ab1ef099b92be029250b8159de3b8e30 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 29 Feb 2024 10:39:39 +0100 Subject: [PATCH] Simplify viewModel Signed-off-by: alperozturk --- .../client/assistant/AssistantViewModel.kt | 36 +++++++------------ .../client/assistant/AsssistantScreen.kt | 23 ++++++------ 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/assistant/AssistantViewModel.kt b/app/src/main/java/com/nextcloud/client/assistant/AssistantViewModel.kt index b7f6faaae7..2967c17395 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/AssistantViewModel.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/AssistantViewModel.kt @@ -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(null) val selectedTaskType: StateFlow = _selectedTaskType - private val _taskTypes = MutableStateFlow?>(null) - val taskTypes: StateFlow?> = _taskTypes + private val _taskTypes = MutableStateFlow?>(null) + val taskTypes: StateFlow?> = _taskTypes - private var _taskList: RemoteOperationResult? = null + private var _taskList: List? = null - private val _filteredTaskList = MutableStateFlow?>(null) - val filteredTaskList: StateFlow?> = _filteredTaskList + private val _filteredTaskList = MutableStateFlow?>(null) + val filteredTaskList: StateFlow?> = _filteredTaskList private val _loading = MutableStateFlow(true) val loading: StateFlow = _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 } } } } diff --git a/app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt b/app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt index 6136098f1c..5a8171ad87 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt @@ -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, - taskTypes: List, + taskTypes: List?, selectedTask: TaskType?, viewModel: AssistantViewModel, showDeleteTaskAlertDialog: (Long) -> Unit,