Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AssistantRepositoryTests : AbstractOnServerIT() {
}

runBlocking {
val result = sut?.getTaskTypes()
val result = sut?.fetchTaskTypes()
assertTrue(result?.isNotEmpty() == true)
}
}
Expand Down
72 changes: 48 additions & 24 deletions app/src/main/java/com/nextcloud/client/assistant/AssistantScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,17 @@ import com.nextcloud.client.assistant.translate.TranslationViewModel
import com.nextcloud.ui.composeActivity.ComposeActivity
import com.nextcloud.ui.composeActivity.ComposeViewModel
import com.nextcloud.ui.composeComponents.alertDialog.SimpleAlertDialog
import com.nextcloud.ui.composeComponents.alertDialog.TaskSelectionAlertDialog
import com.nextcloud.ui.composeComponents.bottomSheet.MoreActionsBottomSheet
import com.nextcloud.utils.extensions.getChat
import com.owncloud.android.R
import com.owncloud.android.lib.resources.assistant.v2.model.Task
import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
import com.owncloud.android.lib.resources.status.OCCapability
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

private const val CHAT_INPUT_DELAY = 100L
private const val PULL_TO_REFRESH_DELAY = 1500L
Expand Down Expand Up @@ -116,17 +119,24 @@ fun AssistantScreen(
}

LaunchedEffect(selectedText) {
selectedText?.let {
if (it.isBlank()) {
selectedText?.let { copiedText ->
if (copiedText.isBlank()) {
return@LaunchedEffect
}

if (pagerState.currentPage == AssistantPage.Conversation.id) {
pagerState.scrollToPage(AssistantPage.Content.id)
}

viewModel.updateInputBarText(it)
snackbarHostState.showSnackbar(activity.getString(R.string.assistant_screen_text_selected))
scope.launch(Dispatchers.IO) {
val types = viewModel.getRemoteRepository().fetchTaskTypes()
if (!types.isNullOrEmpty()) {
withContext(Dispatchers.Main) {
viewModel.updateScreenOverlayState(ScreenOverlayState.TaskTypes(copiedText, types))
snackbarHostState.showSnackbar(activity.getString(R.string.assistant_screen_text_selected))
}
}
}
}
}

Expand Down Expand Up @@ -367,29 +377,43 @@ private fun InputBar(sessionId: Long?, selectedTaskType: TaskTypeData?, viewMode
@Suppress("LongMethod")
@Composable
private fun OverlayState(state: ScreenOverlayState?, activity: Activity, viewModel: AssistantViewModel) {
when (state) {
is ScreenOverlayState.DeleteTask -> {
SimpleAlertDialog(
title = stringResource(id = R.string.assistant_screen_delete_task_alert_dialog_title),
description = stringResource(id = R.string.assistant_screen_delete_task_alert_dialog_description),
dismiss = { viewModel.updateScreenOverlayState(null) },
onComplete = { viewModel.deleteTask(state.id) }
)
}
state?.let {
when (state) {
is ScreenOverlayState.DeleteTask -> {
SimpleAlertDialog(
title = stringResource(id = R.string.assistant_screen_delete_task_alert_dialog_title),
description = stringResource(id = R.string.assistant_screen_delete_task_alert_dialog_description),
onDismiss = { viewModel.updateScreenOverlayState(null) },
onComplete = { viewModel.deleteTask(state.id) }
)
}

is ScreenOverlayState.TaskActions -> {
val actions = state.getActions(activity, onDeleteCompleted = { deleteTask ->
viewModel.updateScreenOverlayState(deleteTask)
})
is ScreenOverlayState.TaskActions -> {
val actions = state.getActions(activity, onDeleteCompleted = { deleteTask ->
viewModel.updateScreenOverlayState(deleteTask)
})

MoreActionsBottomSheet(
title = state.task.getInputTitle(),
actions = actions,
dismiss = { viewModel.updateScreenOverlayState(null) }
)
}
MoreActionsBottomSheet(
title = state.task.getInputTitle(),
actions = actions,
onDismiss = { viewModel.updateScreenOverlayState(null) }
)
}

is ScreenOverlayState.TaskTypes -> {
TaskSelectionAlertDialog(state.taskTypes, onDismiss = {
viewModel.updateScreenOverlayState(null)
}, onConfirm = {
viewModel.selectTaskType(it)
viewModel.updateInputBarText(state.copiedText)

else -> Unit
if (it.isTranslate()) {
viewModel.updateTranslationTaskState(true)
viewModel.updateScreenState(AssistantScreenState.Translation(null))
}
})
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class AssistantViewModel(
}

private fun fetchTaskTypes() = viewModelScope.launch(Dispatchers.IO) {
val result = remoteRepository.getTaskTypes()
val result = remoteRepository.fetchTaskTypes()
if (result.isNullOrEmpty()) {
_screenState.value = AssistantScreenState.emptyTaskTypes()
return@launch
Expand Down Expand Up @@ -379,6 +379,7 @@ class AssistantViewModel(
}

fun onTranslationScreenDismissed() {
updateInputBarText("")
updateTranslationTaskState(false)
selectTask(null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private fun ConversationList(

MoreActionsBottomSheet(
actions = bottomSheetAction,
dismiss = { selectedConversationId = -1L }
onDismiss = { selectedConversationId = -1L }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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.v2.model.Task
import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
import com.owncloud.android.utils.ClipboardUtil

sealed class ScreenOverlayState {
Expand Down Expand Up @@ -52,4 +53,5 @@ sealed class ScreenOverlayState {
})
)
}
data class TaskTypes(val copiedText: String, val taskTypes: List<TaskTypeData>) : ScreenOverlayState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
import com.owncloud.android.lib.resources.assistant.v2.model.TranslationRequest

interface AssistantRemoteRepository {
suspend fun getTaskTypes(): List<TaskTypeData>?
suspend fun fetchTaskTypes(): List<TaskTypeData>?

suspend fun createTask(input: String, taskType: TaskTypeData): RemoteOperationResult<Void>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class AssistantRemoteRepositoryImpl(private val client: NextcloudClient, capabil

private val supportsV2 = capability.version.isNewerOrEqual(NextcloudVersion.nextcloud_30)

override suspend fun getTaskTypes(): List<TaskTypeData>? = withContext(Dispatchers.IO) {
override suspend fun fetchTaskTypes(): List<TaskTypeData>? = withContext(Dispatchers.IO) {
if (supportsV2) {
val result = GetTaskTypesRemoteOperationV2().execute(client)
if (result.isSuccess) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.owncloud.android.lib.resources.assistant.v2.model.TranslationRequest

@Suppress("MagicNumber")
class MockAssistantRemoteRepository(private val giveEmptyTasks: Boolean = false) : AssistantRemoteRepository {
override suspend fun getTaskTypes(): List<TaskTypeData> = listOf(
override suspend fun fetchTaskTypes(): List<TaskTypeData> = listOf(
TaskTypeData(
id = "core:text2text",
name = "Free text to text prompt",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fun SimpleAlertDialog(
heightFraction: Float? = null,
content: @Composable (() -> Unit)? = null,
onComplete: () -> Unit,
dismiss: () -> Unit
onDismiss: () -> Unit
) {
val modifier = if (heightFraction != null) {
Modifier
Expand All @@ -46,7 +46,7 @@ fun SimpleAlertDialog(
iconContentColor = MaterialTheme.colorScheme.onPrimaryContainer,
titleContentColor = MaterialTheme.colorScheme.onPrimaryContainer,
textContentColor = MaterialTheme.colorScheme.onPrimaryContainer,
onDismissRequest = { dismiss() },
onDismissRequest = { onDismiss() },
title = {
Text(text = title)
},
Expand All @@ -66,15 +66,15 @@ fun SimpleAlertDialog(
confirmButton = {
FilledTonalButton(onClick = {
onComplete()
dismiss()
onDismiss()
}) {
Text(
stringResource(id = R.string.common_ok)
)
}
},
dismissButton = {
TextButton(onClick = { dismiss() }) {
TextButton(onClick = { onDismiss() }) {
Text(
stringResource(id = R.string.common_cancel)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.ui.composeComponents.alertDialog

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExposedDropdownMenuAnchorType
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.owncloud.android.R
import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData

@Suppress("LongMethod")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TaskSelectionAlertDialog(taskTypes: List<TaskTypeData>, onDismiss: () -> Unit, onConfirm: (TaskTypeData) -> Unit) {
var expanded by remember { mutableStateOf(false) }
var tempSelectedTask by remember { mutableStateOf(taskTypes.firstOrNull()) }

AlertDialog(
onDismissRequest = onDismiss,
title = {
Text(
text = stringResource(R.string.assistant_screen_select_task_type_title),
style = MaterialTheme.typography.titleLarge
)
},
text = {
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = { expanded = !expanded },
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 16.dp)
) {
OutlinedTextField(
value = tempSelectedTask?.name ?: "",
onValueChange = {},
readOnly = true,
label = { Text(stringResource(R.string.assistant_screen_select_task_type_label)) },
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
colors = ExposedDropdownMenuDefaults.outlinedTextFieldColors(),
modifier = Modifier
.menuAnchor(
type = ExposedDropdownMenuAnchorType.PrimaryNotEditable,
enabled = true
)
.fillMaxWidth()
)

ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
taskTypes.forEach { task ->
DropdownMenuItem(
text = {
Text(
text = task.name,
style = MaterialTheme.typography.bodyLarge
)
},
onClick = {
tempSelectedTask = task
expanded = false
},
contentPadding = ExposedDropdownMenuDefaults.ItemContentPadding
)
}
}
}
},
confirmButton = {
FilledTonalButton(
onClick = {
tempSelectedTask?.let {
onDismiss()
onConfirm(it)
}
},
enabled = tempSelectedTask != null
) {
Text(text = stringResource(android.R.string.ok))
}
},
dismissButton = {
TextButton(onClick = onDismiss) {
Text(text = stringResource(android.R.string.cancel))
}
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ import kotlinx.coroutines.launch
@SuppressLint("ResourceAsColor")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MoreActionsBottomSheet(title: String? = null, actions: List<Triple<Int, Int, () -> Unit>>, dismiss: () -> Unit) {
fun MoreActionsBottomSheet(title: String? = null, actions: List<Triple<Int, Int, () -> Unit>>, onDismiss: () -> Unit) {
val sheetState = rememberModalBottomSheetState()
val scope = rememberCoroutineScope()

ModalBottomSheet(
modifier = Modifier.padding(top = 32.dp),
containerColor = colorScheme.surface,
onDismissRequest = {
dismiss()
onDismiss()
},
sheetState = sheetState
) {
Expand Down Expand Up @@ -73,7 +73,7 @@ fun MoreActionsBottomSheet(title: String? = null, actions: List<Triple<Int, Int,
.launch { sheetState.hide() }
.invokeOnCompletion {
if (!sheetState.isVisible) {
dismiss()
onDismiss()
action.third()
}
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
<string name="assistant_screen_delete_task_alert_dialog_title">Delete task</string>
<string name="assistant_screen_delete_task_alert_dialog_description">Are you sure you want to delete this task?</string>
<string name="assistant_screen_task_more_actions_bottom_sheet_delete_action">Delete Task</string>
<string name="assistant_screen_select_task_type_title">Select task</string>
<string name="assistant_screen_select_task_type_label">Task</string>
<string name="assistant_screen_task_create_success_message">Task created</string>
<string name="assistant_screen_task_create_fail_message">An error occurred while creating the task</string>
<string name="assistant_screen_task_delete_success_message">Task deleted</string>
Expand Down
Loading