Skip to content

Commit

Permalink
refactor: show error message after failed translation
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Oct 29, 2024
1 parent 8a4b492 commit 964a37d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 18 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/bnyro/translate/ui/ShareActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -65,7 +67,7 @@ fun LanguageSelectionComponent(viewModel: TranslationModel) {
viewModel.targetLanguage = viewModel.sourceLanguage
}
viewModel.sourceLanguage = it
viewModel.translateNow()
viewModel.translateNow(context)
}
}

Expand All @@ -84,7 +86,7 @@ fun LanguageSelectionComponent(viewModel: TranslationModel) {
viewModel.sourceLanguage = viewModel.targetLanguage
}
viewModel.targetLanguage = it
viewModel.translateNow()
viewModel.translateNow(context)
}
}
}
Expand All @@ -108,7 +110,7 @@ fun LanguageSelectionComponent(viewModel: TranslationModel) {
viewModel.targetLanguage = viewModel.sourceLanguage
}
viewModel.sourceLanguage = it
viewModel.translateNow()
viewModel.translateNow(context)
}
}

Expand All @@ -126,7 +128,7 @@ fun LanguageSelectionComponent(viewModel: TranslationModel) {
viewModel.sourceLanguage = viewModel.targetLanguage
}
viewModel.targetLanguage = it
viewModel.translateNow()
viewModel.translateNow(context)
}
}
}
Expand All @@ -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())
}
Expand All @@ -152,7 +156,7 @@ fun SwapLanguagesButton(viewModel: TranslationModel) {
viewModel.translation = Translation("")
}

viewModel.translateNow()
viewModel.translateNow(context)
}
) {
Icon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fun TranslationField(
useElevatedButton = false
) {
setLanguage(it)
translationModel.translateNow()
translationModel.translateNow(context)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,15 +86,15 @@ class TranslationModel : ViewModel() {
}.getOrNull()
}

fun enqueueTranslation() {
fun enqueueTranslation(context: Context) {
if (!Preferences.get(Preferences.translateAutomatically, true)) return

val insertedTextTemp = insertedText
Handler(
Looper.getMainLooper()
).postDelayed(
{
if (insertedTextTemp == insertedText) translateNow()
if (insertedTextTemp == insertedText) translateNow(context)
},
Preferences.get(
Preferences.fetchDelay,
Expand All @@ -102,7 +103,7 @@ class TranslationModel : ViewModel() {
)
}

fun translateNow() {
fun translateNow(context: Context) {
if (insertedText.isEmpty() || targetLanguage == sourceLanguage) {
translation = Translation("")
return
Expand All @@ -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
}
Expand Down Expand Up @@ -232,7 +234,7 @@ class TranslationModel : ViewModel() {
val newSelectedEngine = getCurrentEngine()
if (newSelectedEngine != engine) {
engine = newSelectedEngine
enqueueTranslation()
enqueueTranslation(context)
}

enabledSimEngines = getEnabledEngines()
Expand All @@ -257,7 +259,7 @@ class TranslationModel : ViewModel() {
Thread {
TessHelper.getText(context, uri)?.let {
insertedText = it
translateNow()
translateNow(context)
}
}.start()
}
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/bnyro/translate/ui/views/HistoryRow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -55,14 +56,16 @@ fun HistoryRow(
historyItem: HistoryItem,
onDelete: () -> Unit
) {
val context = LocalContext.current

var showDialog by remember {
mutableStateOf(false)
}

fun loadTranslation() {
showDialog = false
translationModel.insertedText = historyItem.insertedText
translationModel.translateNow()
translationModel.translateNow(context)
navController.navigate(Destination.Translate.route)
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/translate/ui/views/TopBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fun TopBar(
val speechRecognizer = rememberLauncherForActivityResult(SpeechResultContract()) {
if (it != null) {
mainModel.insertedText = it
mainModel.enqueueTranslation()
mainModel.enqueueTranslation(context)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fun TranslationComponent(
) {
viewModel.insertedText = it
hasClip = clipboard.hasText()
viewModel.enqueueTranslation()
viewModel.enqueueTranslation(context)
}

if (viewModel.translating) {
Expand All @@ -130,7 +130,7 @@ fun TranslationComponent(
icon = Icons.Default.ContentPaste
) {
viewModel.insertedText = clipboard.getText()?.toString().orEmpty()
viewModel.enqueueTranslation()
viewModel.enqueueTranslation(context)
}

Spacer(
Expand Down Expand Up @@ -165,7 +165,7 @@ fun TranslationComponent(
text = stringResource(R.string.translate),
icon = Icons.Default.Translate
) {
viewModel.translateNow()
viewModel.translateNow(context)
}
}

Expand Down

0 comments on commit 964a37d

Please sign in to comment.