From 5f20c0ab27ae5bdcc7971bb6443e1a1905a5370e Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 29 Jul 2024 17:01:23 +0200 Subject: [PATCH] fix: broken simultaneous translation (closes #448) --- .../com/bnyro/translate/ui/ShareActivity.kt | 2 + .../translate/ui/screens/TranslationPage.kt | 10 +- .../ui/views/SimTranslationComponent.kt | 3 +- .../ui/views/TranslationComponent.kt | 199 +++++++++--------- 4 files changed, 109 insertions(+), 105 deletions(-) diff --git a/app/src/main/java/com/bnyro/translate/ui/ShareActivity.kt b/app/src/main/java/com/bnyro/translate/ui/ShareActivity.kt index 92de4a1ef..be271aeaf 100644 --- a/app/src/main/java/com/bnyro/translate/ui/ShareActivity.kt +++ b/app/src/main/java/com/bnyro/translate/ui/ShareActivity.kt @@ -23,6 +23,7 @@ import android.net.Uri import android.os.Build import android.os.Bundle import android.os.Parcelable +import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding import androidx.compose.material3.AlertDialog @@ -79,6 +80,7 @@ class ShareActivity : BaseActivity() { }, text = { TranslationComponent( + modifier = Modifier.fillMaxSize(), viewModel = translationModel, showLanguageSelector = true ) diff --git a/app/src/main/java/com/bnyro/translate/ui/screens/TranslationPage.kt b/app/src/main/java/com/bnyro/translate/ui/screens/TranslationPage.kt index 060d78831..cd677ff04 100644 --- a/app/src/main/java/com/bnyro/translate/ui/screens/TranslationPage.kt +++ b/app/src/main/java/com/bnyro/translate/ui/screens/TranslationPage.kt @@ -18,6 +18,7 @@ package com.bnyro.translate.ui.screens import android.annotation.SuppressLint +import android.util.Log import android.view.ViewTreeObserver import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column @@ -60,7 +61,7 @@ import com.bnyro.translate.ui.views.AdditionalInfoComponent import com.bnyro.translate.ui.views.TopBar import com.bnyro.translate.ui.views.TranslationComponent import com.bnyro.translate.util.Preferences -import com.bnyro.translate.util.SimTranslationComponent +import com.bnyro.translate.ui.views.SimTranslationComponent @SuppressLint("UnrememberedMutableState") @Composable @@ -149,8 +150,10 @@ fun TranslationPage( modifier = Modifier .weight(1.0f) ) { - Column { - TranslationComponent(viewModel) + Column( + modifier = Modifier.padding(16.dp) + ) { + TranslationComponent(Modifier.weight(1f), viewModel) if (Preferences.get(Preferences.showAdditionalInfo, true) && !isKeyboardOpen @@ -164,6 +167,7 @@ fun TranslationPage( ) if (viewModel.simTranslationEnabled) { + Log.e("sim tra", "sim") SimTranslationComponent(viewModel) } else { HorizontalDivider( diff --git a/app/src/main/java/com/bnyro/translate/ui/views/SimTranslationComponent.kt b/app/src/main/java/com/bnyro/translate/ui/views/SimTranslationComponent.kt index ed3c6f93e..129b34c4d 100644 --- a/app/src/main/java/com/bnyro/translate/ui/views/SimTranslationComponent.kt +++ b/app/src/main/java/com/bnyro/translate/ui/views/SimTranslationComponent.kt @@ -15,13 +15,12 @@ * along with this program. If not, see . */ -package com.bnyro.translate.util +package com.bnyro.translate.ui.views import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyRow import androidx.compose.foundation.lazy.items import androidx.compose.material3.ElevatedFilterChip -import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue diff --git a/app/src/main/java/com/bnyro/translate/ui/views/TranslationComponent.kt b/app/src/main/java/com/bnyro/translate/ui/views/TranslationComponent.kt index 0e22620da..a643bf4d4 100644 --- a/app/src/main/java/com/bnyro/translate/ui/views/TranslationComponent.kt +++ b/app/src/main/java/com/bnyro/translate/ui/views/TranslationComponent.kt @@ -25,6 +25,7 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width @@ -63,6 +64,7 @@ import kotlinx.coroutines.launch @Composable fun TranslationComponent( + modifier: Modifier, viewModel: TranslationModel, showLanguageSelector: Boolean = false ) { @@ -78,125 +80,122 @@ fun TranslationComponent( hasClip = clipboard.hasText() && !clipboard.getText()?.toString().isNullOrBlank() } - Column( - modifier = Modifier - .padding(15.dp) - .fillMaxSize() + Box( + modifier = modifier + .fillMaxWidth() ) { - Box( - modifier = Modifier.weight(1.0f) + Column( + modifier = Modifier + .verticalScroll(scrollState) + .fillMaxSize() ) { - Column( - modifier = Modifier - .verticalScroll(scrollState) - .fillMaxSize() - ) { - TranslationField( - translationModel = viewModel, - isSourceField = true, - text = viewModel.insertedText, - viewModel.sourceLanguage, - showLanguageSelector = showLanguageSelector, - setLanguage = { - if (it == viewModel.targetLanguage) { - viewModel.targetLanguage = viewModel.sourceLanguage - } - viewModel.sourceLanguage = it + TranslationField( + translationModel = viewModel, + isSourceField = true, + text = viewModel.insertedText, + viewModel.sourceLanguage, + showLanguageSelector = showLanguageSelector, + setLanguage = { + if (it == viewModel.targetLanguage) { + viewModel.targetLanguage = viewModel.sourceLanguage } - ) { - viewModel.insertedText = it - hasClip = clipboard.hasText() - viewModel.enqueueTranslation() - } - - val modifier = Modifier - .align(Alignment.CenterHorizontally) - .padding(10.dp) - - if (viewModel.translating) { - LinearProgressIndicator( - modifier = modifier - ) - } else { - HorizontalDivider( - color = MaterialTheme.colorScheme.onSurface, - modifier = modifier - .size(70.dp, 1.dp) - ) + viewModel.sourceLanguage = it } + ) { + viewModel.insertedText = it + hasClip = clipboard.hasText() + viewModel.enqueueTranslation() + } - if (hasClip && viewModel.insertedText.isBlank()) { - Row { - ButtonWithIcon( - text = stringResource(R.string.paste), - icon = Icons.Default.ContentPaste - ) { - viewModel.insertedText = clipboard.getText()?.toString().orEmpty() - viewModel.enqueueTranslation() - } + if (viewModel.translating) { + LinearProgressIndicator( + modifier = Modifier + .align(Alignment.CenterHorizontally) + .padding(10.dp) + ) + } else { + HorizontalDivider( + color = MaterialTheme.colorScheme.onSurface, + modifier = Modifier + .align(Alignment.CenterHorizontally) + .padding(10.dp) + .size(70.dp, 1.dp) + ) + } - Spacer( - modifier = Modifier - .width(0.dp) - ) + if (hasClip && viewModel.insertedText.isBlank()) { + Row { + ButtonWithIcon( + text = stringResource(R.string.paste), + icon = Icons.Default.ContentPaste + ) { + viewModel.insertedText = clipboard.getText()?.toString().orEmpty() + viewModel.enqueueTranslation() + } - ButtonWithIcon( - text = stringResource(R.string.forget), - icon = Icons.Default.Clear - ) { - hasClip = false + Spacer( + modifier = Modifier + .width(0.dp) + ) - val manager = ContextCompat.getSystemService(context, ClipboardManager::class.java) ?: return@ButtonWithIcon + ButtonWithIcon( + text = stringResource(R.string.forget), + icon = Icons.Default.Clear + ) { + hasClip = false - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - manager.clearPrimaryClip() - } else { - manager.setPrimaryClip(ClipData(null)) - } + val manager = + ContextCompat.getSystemService(context, ClipboardManager::class.java) + ?: return@ButtonWithIcon - viewModel.clearTranslation() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + manager.clearPrimaryClip() + } else { + manager.setPrimaryClip(ClipData(null)) } + + viewModel.clearTranslation() } - } else if ( - viewModel.insertedText.isNotBlank() && - !Preferences.get(Preferences.translateAutomatically, true) + } + } else if ( + viewModel.insertedText.isNotBlank() && + !Preferences.get(Preferences.translateAutomatically, true) + ) { + ButtonWithIcon( + text = stringResource(R.string.translate), + icon = Icons.Default.Translate ) { - ButtonWithIcon( - text = stringResource(R.string.translate), - icon = Icons.Default.Translate - ) { - viewModel.translateNow() - } + viewModel.translateNow() } + } - TranslationField( - translationModel = viewModel, - isSourceField = false, - text = viewModel.translation.translatedText, - language = viewModel.targetLanguage, - showLanguageSelector = showLanguageSelector, - setLanguage = { - if (it == viewModel.sourceLanguage) { - viewModel.sourceLanguage = viewModel.targetLanguage - } - viewModel.targetLanguage = it + TranslationField( + translationModel = viewModel, + isSourceField = false, + text = viewModel.translation.translatedText, + language = viewModel.targetLanguage, + showLanguageSelector = showLanguageSelector, + setLanguage = { + if (it == viewModel.sourceLanguage) { + viewModel.sourceLanguage = viewModel.targetLanguage } - ) - } + viewModel.targetLanguage = it + } + ) + } - if (scrollState.value > 100) { - FloatingActionButton( - modifier = Modifier - .align(Alignment.BottomEnd) - .padding(16.dp), - onClick = { - coroutineScope.launch { - scrollState.animateScrollTo(0) - } + if (scrollState.value > 100) { + FloatingActionButton( + modifier = Modifier + .align(Alignment.BottomEnd) + .padding(16.dp), + onClick = { + coroutineScope.launch { + scrollState.animateScrollTo(0) } - ) { - Icon(Icons.Default.ArrowUpward, null) } + ) { + Icon(Icons.Default.ArrowUpward, null) } } }