|
| 1 | +/* |
| 2 | + * Nextcloud - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com> |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +package com.nextcloud.client.assistant.translate |
| 9 | + |
| 10 | +import androidx.compose.foundation.clickable |
| 11 | +import androidx.compose.foundation.layout.Row |
| 12 | +import androidx.compose.foundation.layout.Spacer |
| 13 | +import androidx.compose.foundation.layout.fillMaxSize |
| 14 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 15 | +import androidx.compose.foundation.layout.heightIn |
| 16 | +import androidx.compose.foundation.layout.padding |
| 17 | +import androidx.compose.foundation.layout.width |
| 18 | +import androidx.compose.foundation.lazy.LazyColumn |
| 19 | +import androidx.compose.material3.DropdownMenu |
| 20 | +import androidx.compose.material3.DropdownMenuItem |
| 21 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 22 | +import androidx.compose.material3.FloatingActionButton |
| 23 | +import androidx.compose.material3.HorizontalDivider |
| 24 | +import androidx.compose.material3.Icon |
| 25 | +import androidx.compose.material3.MaterialTheme |
| 26 | +import androidx.compose.material3.Scaffold |
| 27 | +import androidx.compose.material3.Text |
| 28 | +import androidx.compose.material3.TextField |
| 29 | +import androidx.compose.material3.TextFieldDefaults |
| 30 | +import androidx.compose.runtime.Composable |
| 31 | +import androidx.compose.runtime.getValue |
| 32 | +import androidx.compose.runtime.mutableStateOf |
| 33 | +import androidx.compose.runtime.remember |
| 34 | +import androidx.compose.runtime.setValue |
| 35 | +import androidx.compose.ui.Modifier |
| 36 | +import androidx.compose.ui.graphics.Color |
| 37 | +import androidx.compose.ui.res.painterResource |
| 38 | +import androidx.compose.ui.res.stringResource |
| 39 | +import androidx.compose.ui.unit.dp |
| 40 | +import com.owncloud.android.R |
| 41 | + |
| 42 | +@OptIn(ExperimentalMaterial3Api::class) |
| 43 | +@Composable |
| 44 | +fun TranslationScreen(textToTranslate: String) { |
| 45 | + var originText by remember { mutableStateOf(textToTranslate) } |
| 46 | + var originLanguage by remember { mutableStateOf("English") } |
| 47 | + var showOriginDropdownMenu by remember { mutableStateOf(false) } |
| 48 | + |
| 49 | + var targetText by remember { mutableStateOf("") } |
| 50 | + var targetLanguage by remember { mutableStateOf("Spanish") } |
| 51 | + var showTargetDropdownMenu by remember { mutableStateOf(false) } |
| 52 | + |
| 53 | + val languages = listOf("English", "Spanish", "French", "German", "Turkish", "Japanese") |
| 54 | + |
| 55 | + Scaffold( |
| 56 | + modifier = Modifier |
| 57 | + .fillMaxSize() |
| 58 | + .padding(16.dp) |
| 59 | + .padding(top = 32.dp), floatingActionButton = { |
| 60 | + FloatingActionButton(onClick = { |
| 61 | + |
| 62 | + }, content = { |
| 63 | + Icon( |
| 64 | + painter = painterResource(R.drawable.ic_translate), |
| 65 | + contentDescription = "translate button" |
| 66 | + ) |
| 67 | + }) |
| 68 | + }) { |
| 69 | + LazyColumn( |
| 70 | + modifier = Modifier.padding(it) |
| 71 | + ) { |
| 72 | + item { |
| 73 | + LanguageSelector( |
| 74 | + title = originLanguage, |
| 75 | + languages = languages, |
| 76 | + titleId = R.string.translation_screen_label_from, |
| 77 | + expanded = showOriginDropdownMenu, |
| 78 | + expand = { |
| 79 | + showOriginDropdownMenu = it |
| 80 | + }, onLanguageSelect = { |
| 81 | + originLanguage = it |
| 82 | + } |
| 83 | + ) |
| 84 | + |
| 85 | + TranslationTextField(titleId = R.string.translation_screen_hint_source, originText, onValueChange = { |
| 86 | + originText = it |
| 87 | + }) |
| 88 | + } |
| 89 | + |
| 90 | + item { |
| 91 | + HorizontalDivider( |
| 92 | + modifier = Modifier.padding(vertical = 16.dp), |
| 93 | + thickness = 1.dp, |
| 94 | + color = MaterialTheme.colorScheme.outlineVariant |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + item { |
| 99 | + LanguageSelector( |
| 100 | + title = targetLanguage, |
| 101 | + languages = languages, |
| 102 | + titleId = R.string.translation_screen_label_to, |
| 103 | + expanded = showTargetDropdownMenu, |
| 104 | + expand = { |
| 105 | + showTargetDropdownMenu = it |
| 106 | + }, onLanguageSelect = { |
| 107 | + targetLanguage = it |
| 108 | + } |
| 109 | + ) |
| 110 | + |
| 111 | + TranslationTextField(titleId = R.string.translation_screen_hint_target, targetText, onValueChange = { |
| 112 | + targetText = it |
| 113 | + }) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +@Composable |
| 120 | +private fun TranslationTextField(titleId: Int, value: String, onValueChange: (String) -> Unit) { |
| 121 | + TextField( |
| 122 | + value = value, |
| 123 | + onValueChange = { |
| 124 | + onValueChange(it) |
| 125 | + }, |
| 126 | + modifier = Modifier |
| 127 | + .fillMaxWidth() |
| 128 | + .heightIn(min = 120.dp, max = 240.dp), |
| 129 | + placeholder = { |
| 130 | + Text( |
| 131 | + text = stringResource(titleId), |
| 132 | + style = MaterialTheme.typography.headlineSmall |
| 133 | + ) |
| 134 | + }, |
| 135 | + textStyle = MaterialTheme.typography.headlineSmall, |
| 136 | + colors = TextFieldDefaults.colors( |
| 137 | + focusedContainerColor = Color.Transparent, |
| 138 | + unfocusedContainerColor = Color.Transparent, |
| 139 | + disabledContainerColor = Color.Transparent, |
| 140 | + focusedIndicatorColor = Color.Transparent, |
| 141 | + unfocusedIndicatorColor = Color.Transparent |
| 142 | + ) |
| 143 | + ) |
| 144 | +} |
| 145 | + |
| 146 | +@Composable |
| 147 | +private fun LanguageSelector( |
| 148 | + title: String, |
| 149 | + languages: List<String>, |
| 150 | + titleId: Int, |
| 151 | + expanded: Boolean, |
| 152 | + expand: (Boolean) -> Unit, |
| 153 | + onLanguageSelect: (String) -> Unit |
| 154 | +) { |
| 155 | + Row( |
| 156 | + modifier = Modifier |
| 157 | + .padding(16.dp) |
| 158 | + .clickable(onClick = { |
| 159 | + expand(!expanded) |
| 160 | + }) |
| 161 | + ) { |
| 162 | + Text( |
| 163 | + text = stringResource(titleId, title), |
| 164 | + style = MaterialTheme.typography.labelLarge, |
| 165 | + color = MaterialTheme.colorScheme.primary, |
| 166 | + ) |
| 167 | + |
| 168 | + Spacer(modifier = Modifier.width(8.dp)) |
| 169 | + |
| 170 | + Text( |
| 171 | + text = title, |
| 172 | + style = MaterialTheme.typography.labelLarge, |
| 173 | + ) |
| 174 | + |
| 175 | + DropdownMenu( |
| 176 | + expanded = expanded, |
| 177 | + onDismissRequest = { expand(false) } |
| 178 | + ) { |
| 179 | + languages.forEach { language -> |
| 180 | + DropdownMenuItem( |
| 181 | + text = { Text(language) }, |
| 182 | + onClick = { |
| 183 | + expand(false) |
| 184 | + onLanguageSelect(language) |
| 185 | + } |
| 186 | + ) |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments