Skip to content

Commit 63d83e5

Browse files
committed
add translation logic
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 747223e commit 63d83e5

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import com.owncloud.android.lib.resources.assistant.chat.model.ChatMessage
2121
import com.owncloud.android.lib.resources.assistant.chat.model.ChatMessageRequest
2222
import com.owncloud.android.lib.resources.assistant.v2.model.Task
2323
import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
24+
import com.owncloud.android.lib.resources.assistant.v2.model.TranslationLanguage
25+
import com.owncloud.android.lib.resources.assistant.v2.model.TranslationRequest
26+
import com.owncloud.android.lib.resources.assistant.v2.model.toTranslationModel
2427
import kotlinx.coroutines.Dispatchers
2528
import kotlinx.coroutines.Job
2629
import kotlinx.coroutines.delay
@@ -184,6 +187,35 @@ class AssistantViewModel(
184187
}
185188
}
186189

190+
fun translate(textToTranslate: String, originLanguage: TranslationLanguage, targetLanguage: TranslationLanguage) {
191+
val task = _selectedTaskType.value
192+
if (task == null) {
193+
_snackbarMessageId.update {
194+
R.string.assistant_screen_select_task
195+
}
196+
return
197+
}
198+
199+
val model = task.toTranslationModel()
200+
201+
if (model == null) {
202+
_snackbarMessageId.update {
203+
R.string.translation_screen_error_message
204+
}
205+
return
206+
}
207+
208+
val input = TranslationRequest(
209+
input = textToTranslate,
210+
originLanguage = originLanguage.code,
211+
targetLanguage = targetLanguage.code,
212+
maxTokens = model.maxTokens,
213+
model = model.model
214+
).toJson()
215+
216+
createTask(input, task)
217+
}
218+
187219
// region chat
188220
fun sendChatMessage(content: String, sessionId: Long) = viewModelScope.launch(Dispatchers.IO) {
189221
val request = ChatMessageRequest(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fun TranslationScreen(task: TaskTypeData?, viewModel: AssistantViewModel, textTo
6363
.padding(16.dp)
6464
.padding(top = 32.dp), floatingActionButton = {
6565
FloatingActionButton(onClick = {
66-
66+
viewModel.translate(textToTranslate, originLanguage, targetLanguage)
6767
}, content = {
6868
Icon(
6969
painter = painterResource(R.drawable.ic_translate),

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@
7070
<string name="assistant_screen_task_delete_fail_message">An error occurred while deleting the task</string>
7171
<string name="assistant_task_detail_screen_input_button_title">Input</string>
7272
<string name="assistant_task_detail_screen_output_button_title">Output</string>
73+
<string name="assistant_screen_select_task">Please select task</string>
7374

7475
<!-- Translation Screen -->
7576
<string name="translation_screen_label_from">Source language: </string>
7677
<string name="translation_screen_label_to">Translate to: </string>
7778
<string name="translation_screen_hint_source">Enter text to translate…</string>
7879
<string name="translation_screen_hint_target">Translation will appear here…</string>
80+
<string name="translation_screen_error_message">Translation model not exists.</string>
7981

8082
<!-- Conversation Screen -->
8183
<string name="conversation_screen_title">Conversations</string>

0 commit comments

Comments
 (0)