Skip to content

Commit 82c15de

Browse files
committed
add translation task select
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 1bd9a81 commit 82c15de

11 files changed

Lines changed: 81 additions & 32 deletions

File tree

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ dependencies {
347347
implementation(libs.compose.ui)
348348
implementation(libs.compose.ui.graphics)
349349
implementation(libs.compose.material3)
350+
implementation(libs.compose.activity)
350351
implementation(libs.compose.ui.tooling.preview)
351352
implementation(libs.foundation)
352353
debugImplementation(libs.compose.ui.tooling)

app/src/main/java/com/nextcloud/client/assistant/AssistantScreen.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ fun AssistantScreen(
161161
}
162162
})
163163
}
164+
164165
AssistantPage.Content.id -> {
165166
Scaffold(
166167
modifier = Modifier.pullToRefresh(
@@ -230,8 +231,15 @@ fun AssistantScreen(
230231
)
231232
}
232233

233-
AssistantScreenState.Translation -> {
234-
TranslationScreen(selectedTaskType, viewModel, selectedText ?: "")
234+
is AssistantScreenState.Translation -> {
235+
val task = (screenState as AssistantScreenState.Translation).task
236+
val textToTranslate = task?.input?.input ?: selectedText ?: ""
237+
238+
TranslationScreen(
239+
selectedTaskType,
240+
viewModel,
241+
textToTranslate
242+
)
235243
}
236244

237245
else -> EmptyContent(
@@ -380,6 +388,9 @@ private fun TaskContent(
380388
showTaskActions = {
381389
val newState = ScreenOverlayState.TaskActions(task)
382390
viewModel.updateScreenOverlayState(newState)
391+
},
392+
showTranslateScreen = {
393+
viewModel.updateScreenState(AssistantScreenState.Translation(it))
383394
}
384395
)
385396
Spacer(modifier = Modifier.height(8.dp))

app/src/main/java/com/nextcloud/client/assistant/AssistantViewModel.kt

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,13 @@ class AssistantViewModel(
127127
// endregion
128128

129129
private suspend fun pollTaskList() {
130-
val cachedTasks = localRepository.getCachedTasks(accountName)
130+
val taskType = _selectedTaskType.value?.id ?: return
131+
132+
val cachedTasks = localRepository.getCachedTasks(accountName, taskType)
131133
if (cachedTasks.isNotEmpty()) {
132134
_filteredTaskList.value = cachedTasks.sortedByDescending { it.id }
133135
}
134136

135-
val taskType = _selectedTaskType.value?.id ?: return
136137
val result = remoteRepository.getTaskList(taskType)
137138
if (result != null) {
138139
taskList = result
@@ -172,11 +173,9 @@ class AssistantViewModel(
172173
_filteredTaskList
173174
) { selectedTask, chats, tasks ->
174175
val isChat = selectedTask?.isChat() == true
175-
val isTranslation = selectedTask?.isTranslate() == true
176176

177177
when {
178178
selectedTask == null -> AssistantScreenState.Loading
179-
isTranslation -> AssistantScreenState.Translation
180179
isChat && chats.isEmpty() -> AssistantScreenState.emptyChatList()
181180
isChat -> AssistantScreenState.ChatContent
182181
!isChat && (tasks == null || tasks.isEmpty()) -> AssistantScreenState.emptyTaskList()
@@ -282,17 +281,27 @@ class AssistantViewModel(
282281

283282
fun selectTaskType(task: TaskTypeData) {
284283
Log_OC.d(TAG, "Task type changed: ${task.name}, session id: ${_sessionId.value}")
284+
285+
// clear task list immediately when task type change
286+
if (_selectedTaskType.value != task) {
287+
_filteredTaskList.update {
288+
listOf()
289+
}
290+
}
291+
285292
updateTaskType(task)
286293

294+
if (!task.isChat()) {
295+
fetchTaskList()
296+
return
297+
}
298+
299+
// only task chat type needs to be handled differently
287300
val sessionId = _sessionId.value ?: return
288-
if (task.isChat()) {
289-
if (_chatMessages.value.isEmpty()) {
290-
fetchChatMessages(sessionId)
291-
} else {
292-
fetchNewChatMessage(sessionId)
293-
}
301+
if (_chatMessages.value.isEmpty()) {
302+
fetchChatMessages(sessionId)
294303
} else {
295-
fetchTaskList()
304+
fetchNewChatMessage(sessionId)
296305
}
297306
}
298307

@@ -310,12 +319,16 @@ class AssistantViewModel(
310319
}
311320

312321
fun fetchTaskList() = viewModelScope.launch(Dispatchers.IO) {
313-
val cached = localRepository.getCachedTasks(accountName)
322+
val taskType = _selectedTaskType.value ?: return@launch
323+
324+
val cached = localRepository.getCachedTasks(accountName, taskType.name)
314325
if (cached.isNotEmpty()) {
315-
_filteredTaskList.value = cached.sortedByDescending { it.id }
326+
_filteredTaskList.update {
327+
cached.sortedByDescending { it.id }
328+
}
316329
}
317330

318-
_selectedTaskType.value?.id?.let { typeId ->
331+
taskType.id?.let { typeId ->
319332
remoteRepository.getTaskList(typeId)?.let { result ->
320333
taskList = result
321334
_filteredTaskList.value = result.sortedByDescending { it.id }
@@ -334,9 +347,11 @@ class AssistantViewModel(
334347
}
335348

336349
updateSnackbarMessage(message)
350+
351+
val taskType = _selectedTaskType.value ?: return@launch
337352
if (result.isSuccess) {
338353
removeTaskFromList(id)
339-
localRepository.deleteTask(id, accountName)
354+
localRepository.deleteTask(id, accountName, taskType.name)
340355
}
341356
}
342357
// endregion
@@ -365,6 +380,12 @@ class AssistantViewModel(
365380
}
366381
}
367382

383+
fun updateScreenState(state: AssistantScreenState) {
384+
_screenState.update {
385+
state
386+
}
387+
}
388+
368389
private fun removeTaskFromList(id: Long) {
369390
_filteredTaskList.update { currentList ->
370391
currentList?.filter { it.id != id }

app/src/main/java/com/nextcloud/client/assistant/model/AssistantScreenState.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.nextcloud.client.assistant.model
99

1010
import com.owncloud.android.R
11+
import com.owncloud.android.lib.resources.assistant.v2.model.Task
1112

1213
sealed class AssistantScreenState {
1314
data object Loading : AssistantScreenState()
@@ -16,7 +17,7 @@ sealed class AssistantScreenState {
1617

1718
data object ChatContent : AssistantScreenState()
1819

19-
data object Translation : AssistantScreenState()
20+
data class Translation(val task: Task?) : AssistantScreenState()
2021

2122
data class EmptyContent(val iconId: Int?, val titleId: Int?, val descriptionId: Int?) : AssistantScreenState()
2223

app/src/main/java/com/nextcloud/client/assistant/repository/local/AssistantLocalRepository.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import com.owncloud.android.lib.resources.assistant.v2.model.Task
1111

1212
interface AssistantLocalRepository {
1313
suspend fun cacheTasks(tasks: List<Task>, accountName: String)
14-
suspend fun getCachedTasks(accountName: String): List<Task>
14+
suspend fun getCachedTasks(accountName: String, type: String): List<Task>
1515
suspend fun insertTask(task: Task, accountName: String)
16-
suspend fun deleteTask(id: Long, accountName: String)
16+
suspend fun deleteTask(id: Long, accountName: String, type: String)
1717
}

app/src/main/java/com/nextcloud/client/assistant/repository/local/AssistantLocalRepositoryImpl.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ class AssistantLocalRepositoryImpl(private val assistantDao: AssistantDao) : Ass
2020
assistantDao.insertAssistantTasks(entities)
2121
}
2222

23-
override suspend fun getCachedTasks(accountName: String): List<Task> {
24-
val entities = assistantDao.getAssistantTasksByAccount(accountName)
23+
override suspend fun getCachedTasks(accountName: String, type: String): List<Task> {
24+
val entities = assistantDao.getAssistantTasksByAccount(accountName, type)
2525
return entities.map { it.toTask() }
2626
}
2727

2828
override suspend fun insertTask(task: Task, accountName: String) {
2929
assistantDao.insertAssistantTask(task.toEntity(accountName))
3030
}
3131

32-
override suspend fun deleteTask(id: Long, accountName: String) {
33-
val cached = assistantDao.getAssistantTasksByAccount(accountName).firstOrNull { it.id == id } ?: return
32+
override suspend fun deleteTask(id: Long, accountName: String, type: String) {
33+
val cached = assistantDao.getAssistantTasksByAccount(accountName, type).firstOrNull { it.id == id } ?: return
3434
assistantDao.deleteAssistantTask(cached)
3535
}
3636

app/src/main/java/com/nextcloud/client/assistant/repository/local/MockAssistantLocalRepository.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ class MockAssistantLocalRepository : AssistantLocalRepository {
2323
}
2424
}
2525

26-
override suspend fun getCachedTasks(accountName: String): List<Task> = mutex.withLock { tasks.toList() }
26+
override suspend fun getCachedTasks(accountName: String, type: String): List<Task> =
27+
mutex.withLock { tasks.toList() }
2728

2829
override suspend fun insertTask(task: Task, accountName: String) {
2930
mutex.withLock { tasks.add(task) }
3031
}
3132

32-
override suspend fun deleteTask(id: Long, accountName: String) {
33+
override suspend fun deleteTask(id: Long, accountName: String, type: String) {
3334
mutex.withLock { tasks.removeAll { it.id == id } }
3435
}
3536
}

app/src/main/java/com/nextcloud/client/assistant/task/TaskView.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import com.owncloud.android.lib.resources.status.OCCapability
4848

4949
@Suppress("LongMethod", "MagicNumber")
5050
@Composable
51-
fun TaskView(task: Task, capability: OCCapability, showTaskActions: () -> Unit) {
51+
fun TaskView(task: Task, capability: OCCapability, showTaskActions: () -> Unit, showTranslateScreen: (Task) -> Unit) {
5252
var showTaskDetailBottomSheet by remember { mutableStateOf(false) }
5353

5454
Box {
@@ -58,7 +58,11 @@ fun TaskView(task: Task, capability: OCCapability, showTaskActions: () -> Unit)
5858
.clip(RoundedCornerShape(8.dp))
5959
.background(color = colorResource(R.color.task_container))
6060
.clickable {
61-
showTaskDetailBottomSheet = true
61+
if (task.type == "core:text2text:translate") {
62+
showTranslateScreen(task)
63+
} else {
64+
showTaskDetailBottomSheet = true
65+
}
6266
}
6367
.padding(16.dp)
6468
) {
@@ -145,6 +149,8 @@ private fun TaskViewPreview() {
145149
versionMayor = 30
146150
},
147151
showTaskActions = {
152+
},
153+
showTranslateScreen = {
148154
}
149155
)
150156
}

app/src/main/java/com/nextcloud/client/assistant/translate/TranslationScreen.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package com.nextcloud.client.assistant.translate
99

10+
import androidx.activity.compose.BackHandler
1011
import androidx.compose.foundation.clickable
1112
import androidx.compose.foundation.layout.Row
1213
import androidx.compose.foundation.layout.Spacer
@@ -38,15 +39,17 @@ import androidx.compose.ui.res.painterResource
3839
import androidx.compose.ui.res.stringResource
3940
import androidx.compose.ui.unit.dp
4041
import com.nextcloud.client.assistant.AssistantViewModel
42+
import com.nextcloud.client.assistant.model.AssistantScreenState
4143
import com.owncloud.android.R
4244
import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
4345
import com.owncloud.android.lib.resources.assistant.v2.model.TranslationLanguage
4446
import com.owncloud.android.lib.resources.assistant.v2.model.toTranslationLanguages
4547

48+
@Suppress("LongMethod")
4649
@OptIn(ExperimentalMaterial3Api::class)
4750
@Composable
48-
fun TranslationScreen(task: TaskTypeData?, viewModel: AssistantViewModel, textToTranslate: String) {
49-
val languages = remember(task) { task?.toTranslationLanguages() }
51+
fun TranslationScreen(selectedTaskType: TaskTypeData?, viewModel: AssistantViewModel, textToTranslate: String) {
52+
val languages = remember(selectedTaskType) { selectedTaskType?.toTranslationLanguages() }
5053

5154
var sourceState by remember {
5255
mutableStateOf(
@@ -60,6 +63,10 @@ fun TranslationScreen(task: TaskTypeData?, viewModel: AssistantViewModel, textTo
6063
mutableStateOf(TranslationSideState(language = languages?.targetLanguages?.firstOrNull()))
6164
}
6265

66+
BackHandler {
67+
viewModel.updateScreenState(AssistantScreenState.TaskContent)
68+
}
69+
6370
Scaffold(
6471
modifier = Modifier
6572
.fillMaxSize()

app/src/main/java/com/nextcloud/client/database/dao/AssistantDao.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ interface AssistantDao {
3333
@Query(
3434
"""
3535
SELECT * FROM ${ProviderMeta.ProviderTableMeta.ASSISTANT_TABLE_NAME}
36-
WHERE accountName = :accountName
36+
WHERE accountName = :accountName AND type = :taskType
3737
ORDER BY lastUpdated DESC
3838
"""
3939
)
40-
suspend fun getAssistantTasksByAccount(accountName: String): List<AssistantEntity>
40+
suspend fun getAssistantTasksByAccount(accountName: String, taskType: String): List<AssistantEntity>
4141
}

0 commit comments

Comments
 (0)