From 964a37d3d08e6c830eb4ff87f601394225552424 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 29 Oct 2024 12:44:03 +0100 Subject: [PATCH] refactor: show error message after failed translation --- .../java/com/bnyro/translate/ui/ShareActivity.kt | 4 ++-- .../ui/components/LanguageSelectionComponent.kt | 14 +++++++++----- .../translate/ui/components/TranslationField.kt | 2 +- .../bnyro/translate/ui/models/TranslationModel.kt | 12 +++++++----- .../com/bnyro/translate/ui/views/HistoryRow.kt | 5 ++++- .../java/com/bnyro/translate/ui/views/TopBar.kt | 2 +- .../translate/ui/views/TranslationComponent.kt | 6 +++--- 7 files changed, 27 insertions(+), 18 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 a0a675a75..a8708eb56 100644 --- a/app/src/main/java/com/bnyro/translate/ui/ShareActivity.kt +++ b/app/src/main/java/com/bnyro/translate/ui/ShareActivity.kt @@ -103,7 +103,7 @@ class ShareActivity : BaseActivity() { private fun handleIntentData() { getIntentText()?.let { translationModel.insertedText = it - translationModel.translateNow() + translationModel.translateNow(this) } // open links from Google Translate if (intent.data?.host == "translate.google.com") { @@ -112,7 +112,7 @@ class ShareActivity : BaseActivity() { translationModel.sourceLanguage = Language(source, source) translationModel.targetLanguage = Language(target, target) translationModel.insertedText = intent.data?.getQueryParameter("text").orEmpty() - translationModel.translateNow() + translationModel.translateNow(this) } if (intent.type?.startsWith("image/") != true) return diff --git a/app/src/main/java/com/bnyro/translate/ui/components/LanguageSelectionComponent.kt b/app/src/main/java/com/bnyro/translate/ui/components/LanguageSelectionComponent.kt index ae53988fe..a3dfce32f 100644 --- a/app/src/main/java/com/bnyro/translate/ui/components/LanguageSelectionComponent.kt +++ b/app/src/main/java/com/bnyro/translate/ui/components/LanguageSelectionComponent.kt @@ -34,6 +34,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalConfiguration +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp import com.bnyro.translate.R @@ -43,6 +44,7 @@ import com.bnyro.translate.ui.models.TranslationModel @Composable fun LanguageSelectionComponent(viewModel: TranslationModel) { val orientation = LocalConfiguration.current.orientation + val context = LocalContext.current if (orientation == Configuration.ORIENTATION_PORTRAIT) { Row( @@ -65,7 +67,7 @@ fun LanguageSelectionComponent(viewModel: TranslationModel) { viewModel.targetLanguage = viewModel.sourceLanguage } viewModel.sourceLanguage = it - viewModel.translateNow() + viewModel.translateNow(context) } } @@ -84,7 +86,7 @@ fun LanguageSelectionComponent(viewModel: TranslationModel) { viewModel.sourceLanguage = viewModel.targetLanguage } viewModel.targetLanguage = it - viewModel.translateNow() + viewModel.translateNow(context) } } } @@ -108,7 +110,7 @@ fun LanguageSelectionComponent(viewModel: TranslationModel) { viewModel.targetLanguage = viewModel.sourceLanguage } viewModel.sourceLanguage = it - viewModel.translateNow() + viewModel.translateNow(context) } } @@ -126,7 +128,7 @@ fun LanguageSelectionComponent(viewModel: TranslationModel) { viewModel.sourceLanguage = viewModel.targetLanguage } viewModel.targetLanguage = it - viewModel.translateNow() + viewModel.translateNow(context) } } } @@ -135,6 +137,8 @@ fun LanguageSelectionComponent(viewModel: TranslationModel) { @Composable fun SwapLanguagesButton(viewModel: TranslationModel) { + val context = LocalContext.current + val switchBtnEnabled by remember { mutableStateOf(viewModel.sourceLanguage.code.isNotEmpty()) } @@ -152,7 +156,7 @@ fun SwapLanguagesButton(viewModel: TranslationModel) { viewModel.translation = Translation("") } - viewModel.translateNow() + viewModel.translateNow(context) } ) { Icon( diff --git a/app/src/main/java/com/bnyro/translate/ui/components/TranslationField.kt b/app/src/main/java/com/bnyro/translate/ui/components/TranslationField.kt index 3f2ceea6f..f79a5efce 100644 --- a/app/src/main/java/com/bnyro/translate/ui/components/TranslationField.kt +++ b/app/src/main/java/com/bnyro/translate/ui/components/TranslationField.kt @@ -89,7 +89,7 @@ fun TranslationField( useElevatedButton = false ) { setLanguage(it) - translationModel.translateNow() + translationModel.translateNow(context) } } diff --git a/app/src/main/java/com/bnyro/translate/ui/models/TranslationModel.kt b/app/src/main/java/com/bnyro/translate/ui/models/TranslationModel.kt index 017b3b4b6..5c023ba27 100644 --- a/app/src/main/java/com/bnyro/translate/ui/models/TranslationModel.kt +++ b/app/src/main/java/com/bnyro/translate/ui/models/TranslationModel.kt @@ -35,6 +35,7 @@ import com.bnyro.translate.const.TranslationEngines import com.bnyro.translate.db.obj.HistoryItem import com.bnyro.translate.db.obj.HistoryItemType import com.bnyro.translate.db.obj.Language +import com.bnyro.translate.ext.toastFromMainThread import com.bnyro.translate.obj.Translation import com.bnyro.translate.util.JsonHelper import com.bnyro.translate.util.Preferences @@ -85,7 +86,7 @@ class TranslationModel : ViewModel() { }.getOrNull() } - fun enqueueTranslation() { + fun enqueueTranslation(context: Context) { if (!Preferences.get(Preferences.translateAutomatically, true)) return val insertedTextTemp = insertedText @@ -93,7 +94,7 @@ class TranslationModel : ViewModel() { Looper.getMainLooper() ).postDelayed( { - if (insertedTextTemp == insertedText) translateNow() + if (insertedTextTemp == insertedText) translateNow(context) }, Preferences.get( Preferences.fetchDelay, @@ -102,7 +103,7 @@ class TranslationModel : ViewModel() { ) } - fun translateNow() { + fun translateNow(context: Context) { if (insertedText.isEmpty() || targetLanguage == sourceLanguage) { translation = Translation("") return @@ -124,6 +125,7 @@ class TranslationModel : ViewModel() { ) } catch (e: Exception) { Log.e("error", e.message.toString()) + context.toastFromMainThread(e.message.orEmpty()) translating = false return@launch } @@ -232,7 +234,7 @@ class TranslationModel : ViewModel() { val newSelectedEngine = getCurrentEngine() if (newSelectedEngine != engine) { engine = newSelectedEngine - enqueueTranslation() + enqueueTranslation(context) } enabledSimEngines = getEnabledEngines() @@ -257,7 +259,7 @@ class TranslationModel : ViewModel() { Thread { TessHelper.getText(context, uri)?.let { insertedText = it - translateNow() + translateNow(context) } }.start() } diff --git a/app/src/main/java/com/bnyro/translate/ui/views/HistoryRow.kt b/app/src/main/java/com/bnyro/translate/ui/views/HistoryRow.kt index c20409969..b90b24f33 100644 --- a/app/src/main/java/com/bnyro/translate/ui/views/HistoryRow.kt +++ b/app/src/main/java/com/bnyro/translate/ui/views/HistoryRow.kt @@ -36,6 +36,7 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp @@ -55,6 +56,8 @@ fun HistoryRow( historyItem: HistoryItem, onDelete: () -> Unit ) { + val context = LocalContext.current + var showDialog by remember { mutableStateOf(false) } @@ -62,7 +65,7 @@ fun HistoryRow( fun loadTranslation() { showDialog = false translationModel.insertedText = historyItem.insertedText - translationModel.translateNow() + translationModel.translateNow(context) navController.navigate(Destination.Translate.route) } diff --git a/app/src/main/java/com/bnyro/translate/ui/views/TopBar.kt b/app/src/main/java/com/bnyro/translate/ui/views/TopBar.kt index 98d0c6ca9..e44b3b993 100644 --- a/app/src/main/java/com/bnyro/translate/ui/views/TopBar.kt +++ b/app/src/main/java/com/bnyro/translate/ui/views/TopBar.kt @@ -66,7 +66,7 @@ fun TopBar( val speechRecognizer = rememberLauncherForActivityResult(SpeechResultContract()) { if (it != null) { mainModel.insertedText = it - mainModel.enqueueTranslation() + mainModel.enqueueTranslation(context) } } 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 a643bf4d4..637400b8f 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 @@ -104,7 +104,7 @@ fun TranslationComponent( ) { viewModel.insertedText = it hasClip = clipboard.hasText() - viewModel.enqueueTranslation() + viewModel.enqueueTranslation(context) } if (viewModel.translating) { @@ -130,7 +130,7 @@ fun TranslationComponent( icon = Icons.Default.ContentPaste ) { viewModel.insertedText = clipboard.getText()?.toString().orEmpty() - viewModel.enqueueTranslation() + viewModel.enqueueTranslation(context) } Spacer( @@ -165,7 +165,7 @@ fun TranslationComponent( text = stringResource(R.string.translate), icon = Icons.Default.Translate ) { - viewModel.translateNow() + viewModel.translateNow(context) } }