AssistantViewModel.kt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Alper Ozturk
  5. * Copyright (C) 2024 Alper Ozturk
  6. * Copyright (C) 2024 Nextcloud GmbH
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.nextcloud.client.assistant
  22. import androidx.lifecycle.ViewModel
  23. import androidx.lifecycle.viewModelScope
  24. import com.nextcloud.client.assistant.repository.AssistantRepository
  25. import com.nextcloud.common.NextcloudClient
  26. import com.owncloud.android.lib.common.operations.RemoteOperationResult
  27. import com.owncloud.android.lib.resources.assistant.model.TaskList
  28. import com.owncloud.android.lib.resources.assistant.model.TaskType
  29. import com.owncloud.android.lib.resources.assistant.model.TaskTypes
  30. import kotlinx.coroutines.Dispatchers
  31. import kotlinx.coroutines.flow.MutableStateFlow
  32. import kotlinx.coroutines.flow.StateFlow
  33. import kotlinx.coroutines.flow.update
  34. import kotlinx.coroutines.launch
  35. class AssistantViewModel(client: NextcloudClient) : ViewModel() {
  36. private val repository: AssistantRepository = AssistantRepository(client)
  37. private val _selectedTaskType = MutableStateFlow<TaskType?>(null)
  38. val selectedTaskType: StateFlow<TaskType?> = _selectedTaskType
  39. private val _taskTypes = MutableStateFlow<RemoteOperationResult<TaskTypes>?>(null)
  40. val taskTypes: StateFlow<RemoteOperationResult<TaskTypes>?> = _taskTypes
  41. private val _taskList = MutableStateFlow<RemoteOperationResult<TaskList>?>(null)
  42. val taskList: StateFlow<RemoteOperationResult<TaskList>?> = _taskList
  43. private val _loading = MutableStateFlow(true)
  44. val loading: StateFlow<Boolean> = _loading
  45. private val _isTaskCreated = MutableStateFlow(false)
  46. val isTaskCreated: StateFlow<Boolean> = _isTaskCreated
  47. private val _isTaskDeleted = MutableStateFlow(false)
  48. val isTaskDeleted: StateFlow<Boolean> = _isTaskDeleted
  49. init {
  50. getTaskTypes()
  51. getTaskList()
  52. }
  53. fun createTask(
  54. input: String,
  55. type: String,
  56. ) {
  57. viewModelScope.launch(Dispatchers.IO) {
  58. val result = repository.createTask(input, type)
  59. _isTaskCreated.update {
  60. result.isSuccess
  61. }
  62. }
  63. }
  64. fun selectTask(task: TaskType) {
  65. _selectedTaskType.update {
  66. task
  67. }
  68. }
  69. private fun getTaskTypes() {
  70. viewModelScope.launch(Dispatchers.IO) {
  71. val result = repository.getTaskTypes()
  72. _taskTypes.update {
  73. result
  74. }
  75. _selectedTaskType.update {
  76. result.resultData.types.first()
  77. }
  78. }
  79. }
  80. private fun getTaskList(appId: String = "assistant") {
  81. viewModelScope.launch(Dispatchers.IO) {
  82. val result = repository.getTaskList(appId)
  83. _taskList.update {
  84. result
  85. }
  86. _loading.update {
  87. false
  88. }
  89. }
  90. }
  91. fun deleteTask(id: Long) {
  92. viewModelScope.launch(Dispatchers.IO) {
  93. val result = repository.deleteTask(id)
  94. _isTaskDeleted.update {
  95. if (result.isSuccess) {
  96. removeTaskFromList(id)
  97. }
  98. result.isSuccess
  99. }
  100. }
  101. }
  102. private fun removeTaskFromList(id: Long) {
  103. _taskList.update { currentList ->
  104. currentList?.resultData?.tasks?.let { tasks ->
  105. currentList.resultData.tasks = tasks.filter { it.id != id }
  106. }
  107. currentList
  108. }
  109. }
  110. }