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 3383df42bb..63852523b0 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/AssistantViewModel.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/AssistantViewModel.kt @@ -36,6 +36,9 @@ import kotlinx.coroutines.launch class AssistantViewModel(private val repository: AssistantRepositoryType) : ViewModel() { + private val _errorMessage = MutableStateFlow(null) + val errorMessage: StateFlow = _errorMessage + private val _selectedTaskType = MutableStateFlow(null) val selectedTaskType: StateFlow = _selectedTaskType @@ -85,30 +88,43 @@ class AssistantViewModel(private val repository: AssistantRepositoryType) : View viewModelScope.launch(Dispatchers.IO) { val allTaskType = MainApp.getAppContext().getString(R.string.assistant_screen_all_task_type) val result = arrayListOf(TaskType(null, allTaskType, null)) - val taskTypes = repository.getTaskTypes().resultData.types - result.addAll(taskTypes) + val taskTypesResult = repository.getTaskTypes() - _taskTypes.update { - result.toList() - } + if (taskTypesResult.isSuccess) { + result.addAll(taskTypesResult.resultData.types) + _taskTypes.update { + result.toList() + } - _selectedTaskType.update { - result.first() + _selectedTaskType.update { + result.first() + } + } else { + _errorMessage.update { + taskTypesResult.message + } } } } fun getTaskList(appId: String = "assistant", onCompleted: () -> Unit = {}) { viewModelScope.launch(Dispatchers.IO) { - _taskList = repository.getTaskList(appId).resultData.tasks + val result = repository.getTaskList(appId) + if (result.isSuccess) { + _taskList = result.resultData.tasks - filterTaskList(_selectedTaskType.value?.id) + filterTaskList(_selectedTaskType.value?.id) - _loading.update { - false + _loading.update { + false + } + + onCompleted() + } else { + _errorMessage.update { + result.message + } } - - onCompleted() } } @@ -126,6 +142,12 @@ class AssistantViewModel(private val repository: AssistantRepositoryType) : View } } + fun resetErrorState() { + _errorMessage.update { + null + } + } + fun resetTaskDeletionState() { _isTaskDeleted.update { null 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 c4388d3a48..37714bfffe 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt @@ -71,6 +71,7 @@ import kotlinx.coroutines.delay @Composable fun AssistantScreen(viewModel: AssistantViewModel, activity: Activity) { val loading by viewModel.loading.collectAsState() + val errorMessage by viewModel.errorMessage.collectAsState() val selectedTaskType by viewModel.selectedTaskType.collectAsState() val filteredTaskList by viewModel.filteredTaskList.collectAsState() val isTaskCreated by viewModel.isTaskCreated.collectAsState() @@ -133,6 +134,7 @@ fun AssistantScreen(viewModel: AssistantViewModel, activity: Activity) { } } + CheckErrorMessage(errorMessage, activity, viewModel) CheckTaskAdd(isTaskCreated, activity, viewModel) CheckTaskDeletion(isTaskDeleted, activity, viewModel) @@ -182,6 +184,18 @@ private fun CheckTaskAdd(isTaskCreated: Boolean?, activity: Activity, viewModel: } } +@Composable +private fun CheckErrorMessage(errorMessage: String?, activity: Activity, viewModel: AssistantViewModel) { + errorMessage?.let { + DisplayUtils.showSnackMessage( + activity, + errorMessage + ) + + viewModel.resetErrorState() + } +} + @Composable private fun CheckTaskDeletion(isTaskDeleted: Boolean?, activity: Activity, viewModel: AssistantViewModel) { isTaskDeleted?.let {