Skip to content

Commit

Permalink
fix: broken simultaneous translation (closes #448)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Jul 29, 2024
1 parent b1b6f3e commit 5f20c0a
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 105 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/bnyro/translate/ui/ShareActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,6 +80,7 @@ class ShareActivity : BaseActivity() {
},
text = {
TranslationComponent(
modifier = Modifier.fillMaxSize(),
viewModel = translationModel,
showLanguageSelector = true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -164,6 +167,7 @@ fun TranslationPage(
)

if (viewModel.simTranslationEnabled) {
Log.e("sim tra", "sim")
SimTranslationComponent(viewModel)
} else {
HorizontalDivider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

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
Expand Down
199 changes: 99 additions & 100 deletions app/src/main/java/com/bnyro/translate/ui/views/TranslationComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,6 +64,7 @@ import kotlinx.coroutines.launch

@Composable
fun TranslationComponent(
modifier: Modifier,
viewModel: TranslationModel,
showLanguageSelector: Boolean = false
) {
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit 5f20c0a

Please sign in to comment.