mirror of
https://github.com/nextcloud/android.git
synced 2024-12-20 07:52:18 +03:00
add backward compatibility
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
parent
9f7370f76b
commit
ace68da2a7
13 changed files with 130 additions and 72 deletions
app/src
androidTest/java/com/nextcloud/client/assistant
main/java/com/nextcloud
client/assistant
ui/composeActivity
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<List<TaskTypeData>> {
|
||||
return RemoteOperationResult<List<TaskTypeData>>(RemoteOperationResult.ResultCode.OK).apply {
|
||||
resultData = null
|
||||
}
|
||||
override fun getTaskTypes(): List<TaskTypeData> {
|
||||
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<Void> {
|
||||
return RemoteOperationResult<Void>(RemoteOperationResult.ResultCode.OK)
|
||||
}
|
||||
|
||||
override fun getTaskList(taskType: String): RemoteOperationResult<TaskList> {
|
||||
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<TaskList>(RemoteOperationResult.ResultCode.OK).apply {
|
||||
resultData = taskList
|
||||
}
|
||||
}
|
||||
|
||||
override fun deleteTask(id: Long): RemoteOperationResult<Void> {
|
||||
|
|
|
@ -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<List<TaskTypeData>> {
|
||||
return GetTaskTypesRemoteOperation().execute(client)
|
||||
private val supportsV2 = capability.version.isNewerOrEqual(NextcloudVersion.nextcloud_30)
|
||||
|
||||
@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> {
|
||||
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> {
|
||||
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<Void> {
|
||||
return DeleteTaskRemoteOperation(id).execute(client)
|
||||
return if (supportsV2) {
|
||||
DeleteTaskRemoteOperationV2(id).execute(client)
|
||||
} else {
|
||||
DeleteTaskRemoteOperationV1(id).execute(client)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<List<TaskTypeData>>
|
||||
fun getTaskTypes(): List<TaskTypeData>?
|
||||
|
||||
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>
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -87,7 +87,7 @@ class ComposeActivity : DrawerActivity() {
|
|||
nextcloudClient?.let { client ->
|
||||
AssistantScreen(
|
||||
viewModel = AssistantViewModel(
|
||||
repository = AssistantRepository(client)
|
||||
repository = AssistantRepository(client, capabilities)
|
||||
),
|
||||
activity = this
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue