add backward compatibility

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-12-13 10:09:04 +01:00 committed by Alper Öztürk
parent 9f7370f76b
commit ace68da2a7
13 changed files with 130 additions and 72 deletions

View file

@ -9,7 +9,7 @@ package com.nextcloud.client.assistant
import com.nextcloud.client.assistant.repository.AssistantRepository import com.nextcloud.client.assistant.repository.AssistantRepository
import com.owncloud.android.AbstractOnServerIT 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 com.owncloud.android.lib.resources.status.NextcloudVersion
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Before import org.junit.Before
@ -22,7 +22,7 @@ class AssistantRepositoryTests : AbstractOnServerIT() {
@Before @Before
fun setup() { fun setup() {
sut = AssistantRepository(nextcloudClient) sut = AssistantRepository(nextcloudClient, capability)
} }
@Test @Test
@ -34,10 +34,7 @@ class AssistantRepositoryTests : AbstractOnServerIT() {
} }
val result = sut?.getTaskTypes() val result = sut?.getTaskTypes()
assertTrue(result?.isSuccess == true) assertTrue(result?.isNotEmpty() == true)
val taskTypes = result?.resultData
assertTrue(taskTypes?.isNotEmpty() == true)
} }
@Test @Test
@ -49,10 +46,7 @@ class AssistantRepositoryTests : AbstractOnServerIT() {
} }
val result = sut?.getTaskList("assistant") val result = sut?.getTaskList("assistant")
assertTrue(result?.isSuccess == true) assertTrue(result?.tasks?.isEmpty() == true || (result?.tasks?.size ?: 0) > 0)
val taskList = result?.resultData?.tasks
assertTrue(taskList?.isEmpty() == true || (taskList?.size ?: 0) > 0)
} }
@Test @Test
@ -88,11 +82,11 @@ class AssistantRepositoryTests : AbstractOnServerIT() {
sleep(120) sleep(120)
val resultOfTaskList = sut?.getTaskList("assistant") val resultOfTaskList = sut?.getTaskList("assistant")
assertTrue(resultOfTaskList?.isSuccess == true) assertTrue(resultOfTaskList != null)
sleep(120) sleep(120)
val taskList = resultOfTaskList?.resultData?.tasks val taskList = resultOfTaskList?.tasks
assert((taskList?.size ?: 0) > 0) assert((taskList?.size ?: 0) > 0)

View file

@ -13,8 +13,8 @@ import com.nextcloud.client.assistant.model.ScreenOverlayState
import com.nextcloud.client.assistant.model.ScreenState import com.nextcloud.client.assistant.model.ScreenState
import com.nextcloud.client.assistant.repository.AssistantRepositoryType import com.nextcloud.client.assistant.repository.AssistantRepositoryType
import com.owncloud.android.R 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 com.owncloud.android.lib.resources.assistant.model.TaskTypeData import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@ -80,16 +80,16 @@ class AssistantViewModel(
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
val taskTypesResult = repository.getTaskTypes() val taskTypesResult = repository.getTaskTypes()
if (taskTypesResult.isSuccess) { if (taskTypesResult.isNullOrEmpty()) {
val result = taskTypesResult.resultData updateSnackbarMessage(R.string.assistant_screen_task_types_error_state_message)
_taskTypes.update { return@launch
result
} }
selectTaskType(result.first()) _taskTypes.update {
} else { taskTypesResult
updateSnackbarMessage(R.string.assistant_screen_task_types_error_state_message)
} }
selectTaskType(taskTypesResult.first())
} }
} }
@ -101,8 +101,8 @@ class AssistantViewModel(
val taskType = _selectedTaskType.value?.id ?: return@launch val taskType = _selectedTaskType.value?.id ?: return@launch
val result = repository.getTaskList(taskType) val result = repository.getTaskList(taskType)
if (result.isSuccess) { if (result != null) {
taskList = result.resultData.tasks.filter { it.appId == "assistant" } taskList = result.tasks.filter { it.appId == "assistant" }
_filteredTaskList.update { _filteredTaskList.update {
taskList?.sortedByDescending { task -> taskList?.sortedByDescending { task ->
task.id task.id

View file

@ -48,8 +48,8 @@ import com.nextcloud.ui.composeActivity.ComposeActivity
import com.nextcloud.ui.composeComponents.alertDialog.SimpleAlertDialog import com.nextcloud.ui.composeComponents.alertDialog.SimpleAlertDialog
import com.nextcloud.ui.composeComponents.bottomSheet.MoreActionsBottomSheet import com.nextcloud.ui.composeComponents.bottomSheet.MoreActionsBottomSheet
import com.owncloud.android.R 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 com.owncloud.android.lib.resources.assistant.model.TaskTypeData import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
import com.owncloud.android.utils.DisplayUtils import com.owncloud.android.utils.DisplayUtils
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch

View file

@ -13,7 +13,7 @@ import android.content.Context
import com.nextcloud.utils.date.DateFormatPattern import com.nextcloud.utils.date.DateFormatPattern
import com.nextcloud.utils.date.DateFormatter import com.nextcloud.utils.date.DateFormatter
import com.owncloud.android.R 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 import java.util.concurrent.TimeUnit
fun Task.getInputAndOutput(): String { fun Task.getInputAndOutput(): String {

View file

@ -12,8 +12,8 @@ import com.nextcloud.client.assistant.extensions.getInput
import com.nextcloud.client.assistant.extensions.getInputAndOutput import com.nextcloud.client.assistant.extensions.getInputAndOutput
import com.nextcloud.utils.extensions.showShareIntent import com.nextcloud.utils.extensions.showShareIntent
import com.owncloud.android.R 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 com.owncloud.android.lib.resources.assistant.model.TaskTypeData import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
import com.owncloud.android.utils.ClipboardUtil import com.owncloud.android.utils.ClipboardUtil
sealed class ScreenOverlayState { sealed class ScreenOverlayState {

View file

@ -9,26 +9,46 @@ package com.nextcloud.client.assistant.repository
import com.nextcloud.utils.extensions.getRandomString import com.nextcloud.utils.extensions.getRandomString
import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.resources.assistant.model.Task import com.owncloud.android.lib.resources.assistant.v2.model.Task
import com.owncloud.android.lib.resources.assistant.model.TaskInput import com.owncloud.android.lib.resources.assistant.v2.model.TaskInput
import com.owncloud.android.lib.resources.assistant.model.TaskList import com.owncloud.android.lib.resources.assistant.v2.model.TaskInputShape
import com.owncloud.android.lib.resources.assistant.model.TaskOutput import com.owncloud.android.lib.resources.assistant.v2.model.TaskList
import com.owncloud.android.lib.resources.assistant.model.TaskTypeData 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") @Suppress("MagicNumber")
class AssistantMockRepository(private val giveEmptyTasks: Boolean = false) : AssistantRepositoryType { class AssistantMockRepository(private val giveEmptyTasks: Boolean = false) : AssistantRepositoryType {
override fun getTaskTypes(): RemoteOperationResult<List<TaskTypeData>> { override fun getTaskTypes(): List<TaskTypeData> {
return RemoteOperationResult<List<TaskTypeData>>(RemoteOperationResult.ResultCode.OK).apply { return listOf(
resultData = null 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<Void> { override fun createTask(input: String, taskType: TaskTypeData): RemoteOperationResult<Void> {
return RemoteOperationResult<Void>(RemoteOperationResult.ResultCode.OK) return RemoteOperationResult<Void>(RemoteOperationResult.ResultCode.OK)
} }
override fun getTaskList(taskType: String): RemoteOperationResult<TaskList> { override fun getTaskList(taskType: String): TaskList {
val taskList = if (giveEmptyTasks) { return if (giveEmptyTasks) {
TaskList(listOf()) TaskList(listOf())
} else { } else {
TaskList( TaskList(
@ -50,10 +70,6 @@ class AssistantMockRepository(private val giveEmptyTasks: Boolean = false) : Ass
) )
) )
} }
return RemoteOperationResult<TaskList>(RemoteOperationResult.ResultCode.OK).apply {
resultData = taskList
}
} }
override fun deleteTask(id: Long): RemoteOperationResult<Void> { override fun deleteTask(id: Long): RemoteOperationResult<Void> {

View file

@ -9,28 +9,76 @@ package com.nextcloud.client.assistant.repository
import com.nextcloud.common.NextcloudClient import com.nextcloud.common.NextcloudClient
import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.resources.assistant.CreateTaskRemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
import com.owncloud.android.lib.resources.assistant.DeleteTaskRemoteOperation import com.owncloud.android.lib.resources.assistant.v1.CreateTaskRemoteOperationV1
import com.owncloud.android.lib.resources.assistant.GetTaskListRemoteOperation import com.owncloud.android.lib.resources.assistant.v1.DeleteTaskRemoteOperationV1
import com.owncloud.android.lib.resources.assistant.GetTaskTypesRemoteOperation import com.owncloud.android.lib.resources.assistant.v1.GetTaskListRemoteOperationV1
import com.owncloud.android.lib.resources.assistant.model.TaskList import com.owncloud.android.lib.resources.assistant.v1.GetTaskTypesRemoteOperationV1
import com.owncloud.android.lib.resources.assistant.model.TaskTypeData 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<List<TaskTypeData>> { private val supportsV2 = capability.version.isNewerOrEqual(NextcloudVersion.nextcloud_30)
return GetTaskTypesRemoteOperation().execute(client)
@Suppress("ReturnCount")
override fun getTaskTypes(): List<TaskTypeData>? {
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<Void> { override fun createTask(input: String, taskType: TaskTypeData): RemoteOperationResult<Void> {
return CreateTaskRemoteOperation(input, taskType).execute(client) return if (supportsV2) {
CreateTaskRemoteOperationV2(input, taskType).execute(client)
} else {
if (taskType.id.isNullOrEmpty()) {
RemoteOperationResult<Void>(ResultCode.CANCELLED)
} else {
CreateTaskRemoteOperationV1(input, taskType.id!!).execute(client)
}
}
} }
override fun getTaskList(taskType: String): RemoteOperationResult<TaskList> { @Suppress("ReturnCount")
return GetTaskListRemoteOperation(taskType).execute(client) 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<Void> { override fun deleteTask(id: Long): RemoteOperationResult<Void> {
return DeleteTaskRemoteOperation(id).execute(client) return if (supportsV2) {
DeleteTaskRemoteOperationV2(id).execute(client)
} else {
DeleteTaskRemoteOperationV1(id).execute(client)
}
} }
} }

View file

@ -8,15 +8,15 @@
package com.nextcloud.client.assistant.repository package com.nextcloud.client.assistant.repository
import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.resources.assistant.model.TaskList import com.owncloud.android.lib.resources.assistant.v2.model.TaskList
import com.owncloud.android.lib.resources.assistant.model.TaskTypeData import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
interface AssistantRepositoryType { interface AssistantRepositoryType {
fun getTaskTypes(): RemoteOperationResult<List<TaskTypeData>> fun getTaskTypes(): List<TaskTypeData>?
fun createTask(input: String, taskType: TaskTypeData): RemoteOperationResult<Void> fun createTask(input: String, taskType: TaskTypeData): RemoteOperationResult<Void>
fun getTaskList(taskType: String): RemoteOperationResult<TaskList> fun getTaskList(taskType: String): TaskList?
fun deleteTask(id: Long): RemoteOperationResult<Void> fun deleteTask(id: Long): RemoteOperationResult<Void>
} }

View file

@ -28,9 +28,9 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.nextcloud.client.assistant.extensions.getModifiedAtRepresentation import com.nextcloud.client.assistant.extensions.getModifiedAtRepresentation
import com.nextcloud.client.assistant.extensions.getStatusIcon import com.nextcloud.client.assistant.extensions.getStatusIcon
import com.owncloud.android.lib.resources.assistant.model.Task import com.owncloud.android.lib.resources.assistant.v2.model.Task
import com.owncloud.android.lib.resources.assistant.model.TaskInput import com.owncloud.android.lib.resources.assistant.v2.model.TaskInput
import com.owncloud.android.lib.resources.assistant.model.TaskOutput import com.owncloud.android.lib.resources.assistant.v2.model.TaskOutput
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@Composable @Composable

View file

@ -41,9 +41,9 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import com.nextcloud.client.assistant.taskDetail.TaskDetailBottomSheet import com.nextcloud.client.assistant.taskDetail.TaskDetailBottomSheet
import com.owncloud.android.R 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 com.owncloud.android.lib.resources.assistant.model.TaskInput import com.owncloud.android.lib.resources.assistant.v2.model.TaskInput
import com.owncloud.android.lib.resources.assistant.model.TaskOutput import com.owncloud.android.lib.resources.assistant.v2.model.TaskOutput
@Suppress("LongMethod", "MagicNumber") @Suppress("LongMethod", "MagicNumber")
@Composable @Composable

View file

@ -47,9 +47,9 @@ import androidx.compose.ui.unit.sp
import com.nextcloud.client.assistant.task.TaskStatusView import com.nextcloud.client.assistant.task.TaskStatusView
import com.nextcloud.utils.extensions.getRandomString import com.nextcloud.utils.extensions.getRandomString
import com.owncloud.android.R 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 com.owncloud.android.lib.resources.assistant.model.TaskInput import com.owncloud.android.lib.resources.assistant.v2.model.TaskInput
import com.owncloud.android.lib.resources.assistant.model.TaskOutput import com.owncloud.android.lib.resources.assistant.v2.model.TaskOutput
@Suppress("LongMethod") @Suppress("LongMethod")
@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class) @OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class)

View file

@ -19,7 +19,7 @@ import androidx.compose.ui.res.colorResource
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.owncloud.android.R 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") @SuppressLint("ResourceType")
@Composable @Composable

View file

@ -87,7 +87,7 @@ class ComposeActivity : DrawerActivity() {
nextcloudClient?.let { client -> nextcloudClient?.let { client ->
AssistantScreen( AssistantScreen(
viewModel = AssistantViewModel( viewModel = AssistantViewModel(
repository = AssistantRepository(client) repository = AssistantRepository(client, capabilities)
), ),
activity = this activity = this
) )