From ace68da2a70dd4007adbe40b3a6e6846c7c2f624 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 13 Dec 2024 10:09:04 +0100 Subject: [PATCH] add backward compatibility Signed-off-by: alperozturk --- .../assistant/AssistantRepositoryTests.kt | 18 ++--- .../client/assistant/AssistantViewModel.kt | 24 +++--- .../client/assistant/AsssistantScreen.kt | 4 +- .../assistant/extensions/TaskExtensions.kt | 2 +- .../assistant/model/ScreenOverlayState.kt | 4 +- .../repository/AssistantMockRepository.kt | 46 ++++++++---- .../repository/AssistantRepository.kt | 74 +++++++++++++++---- .../repository/AssistantRepositoryType.kt | 8 +- .../client/assistant/task/TaskStatusView.kt | 6 +- .../client/assistant/task/TaskView.kt | 6 +- .../taskDetail/TaskDetailBottomSheet.kt | 6 +- .../assistant/taskTypes/TaskTypesRow.kt | 2 +- .../ui/composeActivity/ComposeActivity.kt | 2 +- 13 files changed, 130 insertions(+), 72 deletions(-) diff --git a/app/src/androidTest/java/com/nextcloud/client/assistant/AssistantRepositoryTests.kt b/app/src/androidTest/java/com/nextcloud/client/assistant/AssistantRepositoryTests.kt index cd86391620..29373ac897 100644 --- a/app/src/androidTest/java/com/nextcloud/client/assistant/AssistantRepositoryTests.kt +++ b/app/src/androidTest/java/com/nextcloud/client/assistant/AssistantRepositoryTests.kt @@ -9,7 +9,7 @@ package com.nextcloud.client.assistant import com.nextcloud.client.assistant.repository.AssistantRepository import com.owncloud.android.AbstractOnServerIT -import com.owncloud.android.lib.resources.assistant.model.TaskTypeData +import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData import com.owncloud.android.lib.resources.status.NextcloudVersion import org.junit.Assert.assertTrue import org.junit.Before @@ -22,7 +22,7 @@ class AssistantRepositoryTests : AbstractOnServerIT() { @Before fun setup() { - sut = AssistantRepository(nextcloudClient) + sut = AssistantRepository(nextcloudClient, capability) } @Test @@ -34,10 +34,7 @@ class AssistantRepositoryTests : AbstractOnServerIT() { } val result = sut?.getTaskTypes() - assertTrue(result?.isSuccess == true) - - val taskTypes = result?.resultData - assertTrue(taskTypes?.isNotEmpty() == true) + assertTrue(result?.isNotEmpty() == true) } @Test @@ -49,10 +46,7 @@ class AssistantRepositoryTests : AbstractOnServerIT() { } val result = sut?.getTaskList("assistant") - assertTrue(result?.isSuccess == true) - - val taskList = result?.resultData?.tasks - assertTrue(taskList?.isEmpty() == true || (taskList?.size ?: 0) > 0) + assertTrue(result?.tasks?.isEmpty() == true || (result?.tasks?.size ?: 0) > 0) } @Test @@ -88,11 +82,11 @@ class AssistantRepositoryTests : AbstractOnServerIT() { sleep(120) val resultOfTaskList = sut?.getTaskList("assistant") - assertTrue(resultOfTaskList?.isSuccess == true) + assertTrue(resultOfTaskList != null) sleep(120) - val taskList = resultOfTaskList?.resultData?.tasks + val taskList = resultOfTaskList?.tasks assert((taskList?.size ?: 0) > 0) 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 5d9c6156d8..e279f36b3c 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/AssistantViewModel.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/AssistantViewModel.kt @@ -13,8 +13,8 @@ import com.nextcloud.client.assistant.model.ScreenOverlayState import com.nextcloud.client.assistant.model.ScreenState import com.nextcloud.client.assistant.repository.AssistantRepositoryType import com.owncloud.android.R -import com.owncloud.android.lib.resources.assistant.model.Task -import com.owncloud.android.lib.resources.assistant.model.TaskTypeData +import com.owncloud.android.lib.resources.assistant.v2.model.Task +import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow @@ -80,16 +80,16 @@ class AssistantViewModel( viewModelScope.launch(Dispatchers.IO) { val taskTypesResult = repository.getTaskTypes() - if (taskTypesResult.isSuccess) { - val result = taskTypesResult.resultData - _taskTypes.update { - result - } - - selectTaskType(result.first()) - } else { + if (taskTypesResult.isNullOrEmpty()) { updateSnackbarMessage(R.string.assistant_screen_task_types_error_state_message) + return@launch } + + _taskTypes.update { + taskTypesResult + } + + selectTaskType(taskTypesResult.first()) } } @@ -101,8 +101,8 @@ class AssistantViewModel( val taskType = _selectedTaskType.value?.id ?: return@launch val result = repository.getTaskList(taskType) - if (result.isSuccess) { - taskList = result.resultData.tasks.filter { it.appId == "assistant" } + if (result != null) { + taskList = result.tasks.filter { it.appId == "assistant" } _filteredTaskList.update { taskList?.sortedByDescending { task -> task.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 e28ffdd726..ab9d7de13f 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt @@ -48,8 +48,8 @@ import com.nextcloud.ui.composeActivity.ComposeActivity import com.nextcloud.ui.composeComponents.alertDialog.SimpleAlertDialog import com.nextcloud.ui.composeComponents.bottomSheet.MoreActionsBottomSheet import com.owncloud.android.R -import com.owncloud.android.lib.resources.assistant.model.Task -import com.owncloud.android.lib.resources.assistant.model.TaskTypeData +import com.owncloud.android.lib.resources.assistant.v2.model.Task +import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData import com.owncloud.android.utils.DisplayUtils import kotlinx.coroutines.delay import kotlinx.coroutines.launch diff --git a/app/src/main/java/com/nextcloud/client/assistant/extensions/TaskExtensions.kt b/app/src/main/java/com/nextcloud/client/assistant/extensions/TaskExtensions.kt index 50544e2490..3902c71f8a 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/extensions/TaskExtensions.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/extensions/TaskExtensions.kt @@ -13,7 +13,7 @@ import android.content.Context import com.nextcloud.utils.date.DateFormatPattern import com.nextcloud.utils.date.DateFormatter import com.owncloud.android.R -import com.owncloud.android.lib.resources.assistant.model.Task +import com.owncloud.android.lib.resources.assistant.v2.model.Task import java.util.concurrent.TimeUnit fun Task.getInputAndOutput(): String { diff --git a/app/src/main/java/com/nextcloud/client/assistant/model/ScreenOverlayState.kt b/app/src/main/java/com/nextcloud/client/assistant/model/ScreenOverlayState.kt index a051885e9b..ab8d328e74 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/model/ScreenOverlayState.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/model/ScreenOverlayState.kt @@ -12,8 +12,8 @@ import com.nextcloud.client.assistant.extensions.getInput import com.nextcloud.client.assistant.extensions.getInputAndOutput import com.nextcloud.utils.extensions.showShareIntent import com.owncloud.android.R -import com.owncloud.android.lib.resources.assistant.model.Task -import com.owncloud.android.lib.resources.assistant.model.TaskTypeData +import com.owncloud.android.lib.resources.assistant.v2.model.Task +import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData import com.owncloud.android.utils.ClipboardUtil sealed class ScreenOverlayState { diff --git a/app/src/main/java/com/nextcloud/client/assistant/repository/AssistantMockRepository.kt b/app/src/main/java/com/nextcloud/client/assistant/repository/AssistantMockRepository.kt index 4821760f15..51e484b79c 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/repository/AssistantMockRepository.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/repository/AssistantMockRepository.kt @@ -9,26 +9,46 @@ package com.nextcloud.client.assistant.repository import com.nextcloud.utils.extensions.getRandomString import com.owncloud.android.lib.common.operations.RemoteOperationResult -import com.owncloud.android.lib.resources.assistant.model.Task -import com.owncloud.android.lib.resources.assistant.model.TaskInput -import com.owncloud.android.lib.resources.assistant.model.TaskList -import com.owncloud.android.lib.resources.assistant.model.TaskOutput -import com.owncloud.android.lib.resources.assistant.model.TaskTypeData +import com.owncloud.android.lib.resources.assistant.v2.model.Task +import com.owncloud.android.lib.resources.assistant.v2.model.TaskInput +import com.owncloud.android.lib.resources.assistant.v2.model.TaskInputShape +import com.owncloud.android.lib.resources.assistant.v2.model.TaskList +import com.owncloud.android.lib.resources.assistant.v2.model.TaskOutput +import com.owncloud.android.lib.resources.assistant.v2.model.TaskOutputShape +import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData @Suppress("MagicNumber") class AssistantMockRepository(private val giveEmptyTasks: Boolean = false) : AssistantRepositoryType { - override fun getTaskTypes(): RemoteOperationResult> { - return RemoteOperationResult>(RemoteOperationResult.ResultCode.OK).apply { - resultData = null - } + override fun getTaskTypes(): List { + return listOf( + TaskTypeData( + "core:text2text", + "Free text to text prompt", + "Runs an arbitrary prompt through a language model that returns a reply", + listOf( + TaskInputShape( + "Prompt", + "Describe a task that you want the assistant to do or ask a question", + "Text" + ) + ), + listOf( + TaskOutputShape( + "Generated reply", + "The generated text from the assistant", + "Text" + ) + ) + ) + ) } override fun createTask(input: String, taskType: TaskTypeData): RemoteOperationResult { return RemoteOperationResult(RemoteOperationResult.ResultCode.OK) } - override fun getTaskList(taskType: String): RemoteOperationResult { - val taskList = if (giveEmptyTasks) { + override fun getTaskList(taskType: String): TaskList { + return if (giveEmptyTasks) { TaskList(listOf()) } else { TaskList( @@ -50,10 +70,6 @@ class AssistantMockRepository(private val giveEmptyTasks: Boolean = false) : Ass ) ) } - - return RemoteOperationResult(RemoteOperationResult.ResultCode.OK).apply { - resultData = taskList - } } override fun deleteTask(id: Long): RemoteOperationResult { diff --git a/app/src/main/java/com/nextcloud/client/assistant/repository/AssistantRepository.kt b/app/src/main/java/com/nextcloud/client/assistant/repository/AssistantRepository.kt index 5da5e56473..19d6f43352 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/repository/AssistantRepository.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/repository/AssistantRepository.kt @@ -9,28 +9,76 @@ package com.nextcloud.client.assistant.repository import com.nextcloud.common.NextcloudClient import com.owncloud.android.lib.common.operations.RemoteOperationResult -import com.owncloud.android.lib.resources.assistant.CreateTaskRemoteOperation -import com.owncloud.android.lib.resources.assistant.DeleteTaskRemoteOperation -import com.owncloud.android.lib.resources.assistant.GetTaskListRemoteOperation -import com.owncloud.android.lib.resources.assistant.GetTaskTypesRemoteOperation -import com.owncloud.android.lib.resources.assistant.model.TaskList -import com.owncloud.android.lib.resources.assistant.model.TaskTypeData +import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode +import com.owncloud.android.lib.resources.assistant.v1.CreateTaskRemoteOperationV1 +import com.owncloud.android.lib.resources.assistant.v1.DeleteTaskRemoteOperationV1 +import com.owncloud.android.lib.resources.assistant.v1.GetTaskListRemoteOperationV1 +import com.owncloud.android.lib.resources.assistant.v1.GetTaskTypesRemoteOperationV1 +import com.owncloud.android.lib.resources.assistant.v1.model.toV2 +import com.owncloud.android.lib.resources.assistant.v2.CreateTaskRemoteOperationV2 +import com.owncloud.android.lib.resources.assistant.v2.DeleteTaskRemoteOperationV2 +import com.owncloud.android.lib.resources.assistant.v2.GetTaskListRemoteOperationV2 +import com.owncloud.android.lib.resources.assistant.v2.GetTaskTypesRemoteOperationV2 +import com.owncloud.android.lib.resources.assistant.v2.model.TaskList +import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData +import com.owncloud.android.lib.resources.status.NextcloudVersion +import com.owncloud.android.lib.resources.status.OCCapability -class AssistantRepository(private val client: NextcloudClient) : AssistantRepositoryType { +class AssistantRepository(private val client: NextcloudClient, capability: OCCapability) : AssistantRepositoryType { - override fun getTaskTypes(): RemoteOperationResult> { - return GetTaskTypesRemoteOperation().execute(client) + private val supportsV2 = capability.version.isNewerOrEqual(NextcloudVersion.nextcloud_30) + + @Suppress("ReturnCount") + override fun getTaskTypes(): List? { + if (supportsV2) { + val result = GetTaskTypesRemoteOperationV2().execute(client) + if (result.isSuccess) { + return result.resultData + } + } else { + val result = GetTaskTypesRemoteOperationV1().execute(client) + if (result.isSuccess) { + return result.resultData.toV2() + } + } + + return null } override fun createTask(input: String, taskType: TaskTypeData): RemoteOperationResult { - return CreateTaskRemoteOperation(input, taskType).execute(client) + return if (supportsV2) { + CreateTaskRemoteOperationV2(input, taskType).execute(client) + } else { + if (taskType.id.isNullOrEmpty()) { + RemoteOperationResult(ResultCode.CANCELLED) + } else { + CreateTaskRemoteOperationV1(input, taskType.id!!).execute(client) + } + } } - override fun getTaskList(taskType: String): RemoteOperationResult { - return GetTaskListRemoteOperation(taskType).execute(client) + @Suppress("ReturnCount") + override fun getTaskList(taskType: String): TaskList? { + if (supportsV2) { + val result = GetTaskListRemoteOperationV2(taskType).execute(client) + if (result.isSuccess) { + return result.resultData + } + } else { + val result = GetTaskListRemoteOperationV1("assistant").execute(client) + if (result.isSuccess) { + return result.resultData.toV2() + } + } + + return null } override fun deleteTask(id: Long): RemoteOperationResult { - return DeleteTaskRemoteOperation(id).execute(client) + return if (supportsV2) { + DeleteTaskRemoteOperationV2(id).execute(client) + } else { + DeleteTaskRemoteOperationV1(id).execute(client) + } } } diff --git a/app/src/main/java/com/nextcloud/client/assistant/repository/AssistantRepositoryType.kt b/app/src/main/java/com/nextcloud/client/assistant/repository/AssistantRepositoryType.kt index 2d031a87c4..c5a17ba9bc 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/repository/AssistantRepositoryType.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/repository/AssistantRepositoryType.kt @@ -8,15 +8,15 @@ package com.nextcloud.client.assistant.repository 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.TaskTypeData +import com.owncloud.android.lib.resources.assistant.v2.model.TaskList +import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData interface AssistantRepositoryType { - fun getTaskTypes(): RemoteOperationResult> + fun getTaskTypes(): List? fun createTask(input: String, taskType: TaskTypeData): RemoteOperationResult - fun getTaskList(taskType: String): RemoteOperationResult + fun getTaskList(taskType: String): TaskList? fun deleteTask(id: Long): RemoteOperationResult } diff --git a/app/src/main/java/com/nextcloud/client/assistant/task/TaskStatusView.kt b/app/src/main/java/com/nextcloud/client/assistant/task/TaskStatusView.kt index 145ee57c53..e5640d314e 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/task/TaskStatusView.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/task/TaskStatusView.kt @@ -28,9 +28,9 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.nextcloud.client.assistant.extensions.getModifiedAtRepresentation import com.nextcloud.client.assistant.extensions.getStatusIcon -import com.owncloud.android.lib.resources.assistant.model.Task -import com.owncloud.android.lib.resources.assistant.model.TaskInput -import com.owncloud.android.lib.resources.assistant.model.TaskOutput +import com.owncloud.android.lib.resources.assistant.v2.model.Task +import com.owncloud.android.lib.resources.assistant.v2.model.TaskInput +import com.owncloud.android.lib.resources.assistant.v2.model.TaskOutput import java.util.concurrent.TimeUnit @Composable diff --git a/app/src/main/java/com/nextcloud/client/assistant/task/TaskView.kt b/app/src/main/java/com/nextcloud/client/assistant/task/TaskView.kt index c01c15a581..1c317bcbf6 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/task/TaskView.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/task/TaskView.kt @@ -41,9 +41,9 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.nextcloud.client.assistant.taskDetail.TaskDetailBottomSheet import com.owncloud.android.R -import com.owncloud.android.lib.resources.assistant.model.Task -import com.owncloud.android.lib.resources.assistant.model.TaskInput -import com.owncloud.android.lib.resources.assistant.model.TaskOutput +import com.owncloud.android.lib.resources.assistant.v2.model.Task +import com.owncloud.android.lib.resources.assistant.v2.model.TaskInput +import com.owncloud.android.lib.resources.assistant.v2.model.TaskOutput @Suppress("LongMethod", "MagicNumber") @Composable diff --git a/app/src/main/java/com/nextcloud/client/assistant/taskDetail/TaskDetailBottomSheet.kt b/app/src/main/java/com/nextcloud/client/assistant/taskDetail/TaskDetailBottomSheet.kt index 07e5bc2d1b..028002865c 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/taskDetail/TaskDetailBottomSheet.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/taskDetail/TaskDetailBottomSheet.kt @@ -47,9 +47,9 @@ import androidx.compose.ui.unit.sp import com.nextcloud.client.assistant.task.TaskStatusView import com.nextcloud.utils.extensions.getRandomString import com.owncloud.android.R -import com.owncloud.android.lib.resources.assistant.model.Task -import com.owncloud.android.lib.resources.assistant.model.TaskInput -import com.owncloud.android.lib.resources.assistant.model.TaskOutput +import com.owncloud.android.lib.resources.assistant.v2.model.Task +import com.owncloud.android.lib.resources.assistant.v2.model.TaskInput +import com.owncloud.android.lib.resources.assistant.v2.model.TaskOutput @Suppress("LongMethod") @OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class) diff --git a/app/src/main/java/com/nextcloud/client/assistant/taskTypes/TaskTypesRow.kt b/app/src/main/java/com/nextcloud/client/assistant/taskTypes/TaskTypesRow.kt index 08f6883fb5..a9596819af 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/taskTypes/TaskTypesRow.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/taskTypes/TaskTypesRow.kt @@ -19,7 +19,7 @@ import androidx.compose.ui.res.colorResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.owncloud.android.R -import com.owncloud.android.lib.resources.assistant.model.TaskTypeData +import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData @SuppressLint("ResourceType") @Composable diff --git a/app/src/main/java/com/nextcloud/ui/composeActivity/ComposeActivity.kt b/app/src/main/java/com/nextcloud/ui/composeActivity/ComposeActivity.kt index d310e6e37c..3bbc01b1bd 100644 --- a/app/src/main/java/com/nextcloud/ui/composeActivity/ComposeActivity.kt +++ b/app/src/main/java/com/nextcloud/ui/composeActivity/ComposeActivity.kt @@ -87,7 +87,7 @@ class ComposeActivity : DrawerActivity() { nextcloudClient?.let { client -> AssistantScreen( viewModel = AssistantViewModel( - repository = AssistantRepository(client) + repository = AssistantRepository(client, capabilities) ), activity = this )