AssistantViewModel.kt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.Task
  28. import com.owncloud.android.lib.resources.assistant.model.TaskList
  29. import com.owncloud.android.lib.resources.assistant.model.TaskType
  30. import com.owncloud.android.lib.resources.assistant.model.TaskTypes
  31. import kotlinx.coroutines.Dispatchers
  32. import kotlinx.coroutines.flow.MutableStateFlow
  33. import kotlinx.coroutines.flow.StateFlow
  34. import kotlinx.coroutines.flow.update
  35. import kotlinx.coroutines.launch
  36. class AssistantViewModel(client: NextcloudClient) : ViewModel() {
  37. private val repository: AssistantRepository = AssistantRepository(client)
  38. private val _selectedTask = MutableStateFlow<TaskType?>(null)
  39. val selectedTask: StateFlow<TaskType?> = _selectedTask
  40. private val _taskTypes = MutableStateFlow<RemoteOperationResult<TaskTypes>?>(null)
  41. val taskTypes: StateFlow<RemoteOperationResult<TaskTypes>?> = _taskTypes
  42. private val _taskList = MutableStateFlow<RemoteOperationResult<TaskList>?>(null)
  43. val taskList: StateFlow<RemoteOperationResult<TaskList>?> = _taskList
  44. private val _isTaskCreated = MutableStateFlow(false)
  45. val isTaskCreated: StateFlow<Boolean> = _isTaskCreated
  46. init {
  47. getTaskTypes()
  48. getTaskList()
  49. }
  50. fun createTask(
  51. input: String,
  52. type: String,
  53. ) {
  54. viewModelScope.launch(Dispatchers.IO) {
  55. val result = repository.createTask(input, type)
  56. _isTaskCreated.update {
  57. result.isSuccess
  58. }
  59. }
  60. }
  61. fun selectTask(task: TaskType) {
  62. _selectedTask.update {
  63. task
  64. }
  65. }
  66. private fun getTaskTypes() {
  67. viewModelScope.launch(Dispatchers.IO) {
  68. val result = repository.getTaskTypes()
  69. _taskTypes.update {
  70. result
  71. }
  72. _selectedTask.update {
  73. result.resultData.types.first()
  74. }
  75. }
  76. }
  77. private fun getTaskList(appId: String = "assistant") {
  78. viewModelScope.launch(Dispatchers.IO) {
  79. val result = repository.getTaskList(appId)
  80. _taskList.update {
  81. result
  82. }
  83. }
  84. }
  85. /*
  86. fun deleteTask(id: String) {
  87. viewModelScope.launch(Dispatchers.IO) {
  88. repository?.deleteTask(id)
  89. }
  90. }
  91. */
  92. }