From cb745e5d69bf5c433c4ead0e692ad733f6135a73 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 27 Jun 2020 14:36:32 +0200 Subject: [PATCH] Refactoring --- .../email/FragmentDialogTranslate.java | 10 ++ .../email/FragmentDialogTranslate.java | 145 ++++++++++-------- 2 files changed, 95 insertions(+), 60 deletions(-) diff --git a/app/src/stub/java/eu/faircode/email/FragmentDialogTranslate.java b/app/src/stub/java/eu/faircode/email/FragmentDialogTranslate.java index 1220748061..c18732ddcb 100644 --- a/app/src/stub/java/eu/faircode/email/FragmentDialogTranslate.java +++ b/app/src/stub/java/eu/faircode/email/FragmentDialogTranslate.java @@ -15,4 +15,14 @@ public class FragmentDialogTranslate extends FragmentDialogBase { .setTitle(R.string.title_translate) .create(); } + + static void Translate(String text, String targetLanguage, ITranslate intf) { + // Stub + } + + interface ITranslate { + void onTranslated(String text); + + void onError(Throwable ex); + } } diff --git a/app/src/xlat/java/eu/faircode/email/FragmentDialogTranslate.java b/app/src/xlat/java/eu/faircode/email/FragmentDialogTranslate.java index 2ec28fed98..3d16e55771 100644 --- a/app/src/xlat/java/eu/faircode/email/FragmentDialogTranslate.java +++ b/app/src/xlat/java/eu/faircode/email/FragmentDialogTranslate.java @@ -44,66 +44,18 @@ public class FragmentDialogTranslate extends FragmentDialogBase { public void onClick(DialogInterface dialog, int which) { String targetLanguage = map.get(items[which]); - LanguageIdentifier languageIdentifier = LanguageIdentification.getClient( - new LanguageIdentificationOptions.Builder() - .setConfidenceThreshold(0.34f) - .build()); - languageIdentifier.identifyLanguage(text) - .addOnSuccessListener( - new OnSuccessListener() { - @Override - public void onSuccess(@Nullable String sourceLanguage) { - Log.i("Translate source=" + sourceLanguage); - if (sourceLanguage.equals("und")) - sourceLanguage = TranslateLanguage.ENGLISH; - - TranslatorOptions options = new TranslatorOptions.Builder() - .setSourceLanguage(sourceLanguage) - .setTargetLanguage(targetLanguage) - .build(); - Translator translator = Translation.getClient(options); - DownloadConditions conditions = new DownloadConditions.Builder() - .requireWifi() - .build(); - translator.downloadModelIfNeeded(conditions) - .addOnSuccessListener( - new OnSuccessListener() { - @Override - public void onSuccess(Void v) { - translator.translate(text) - .addOnSuccessListener( - new OnSuccessListener() { - @Override - public void onSuccess(@NonNull String translatedText) { - getArguments().putString("translated", translatedText); - sendResult(RESULT_OK); - } - }) - .addOnFailureListener( - new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { - Log.unexpectedError(getParentFragmentManager(), ex); - } - }); - } - }) - .addOnFailureListener( - new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { - Log.unexpectedError(getParentFragmentManager(), ex); - } - }); - } - }) - .addOnFailureListener( - new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { - Log.unexpectedError(getParentFragmentManager(), ex); - } - }); + Translate(text, targetLanguage, new ITranslate() { + @Override + public void onTranslated(String text) { + getArguments().putString("translated", text); + sendResult(RESULT_OK); + } + + @Override + public void onError(Throwable ex) { + Log.unexpectedError(getParentFragmentManager(), ex); + } + }); } }) .create(); @@ -112,4 +64,77 @@ public class FragmentDialogTranslate extends FragmentDialogBase { @Override public void onDismiss(@NonNull DialogInterface dialog) { } + + static void Translate(String text, String targetLanguage, ITranslate intf) { + LanguageIdentifier languageIdentifier = LanguageIdentification.getClient( + new LanguageIdentificationOptions.Builder() + .setConfidenceThreshold(0.34f) + .build()); + languageIdentifier.identifyLanguage(text) + .addOnSuccessListener( + new OnSuccessListener() { + @Override + public void onSuccess(@Nullable String sourceLanguage) { + Log.i("Translate source=" + sourceLanguage); + if (sourceLanguage.equals("und")) + sourceLanguage = TranslateLanguage.ENGLISH; + + if (sourceLanguage.equals(targetLanguage)) { + intf.onTranslated(text); + return; + } + + TranslatorOptions options = new TranslatorOptions.Builder() + .setSourceLanguage(sourceLanguage) + .setTargetLanguage(targetLanguage) + .build(); + Translator translator = Translation.getClient(options); + DownloadConditions conditions = new DownloadConditions.Builder() + .requireWifi() + .build(); + translator.downloadModelIfNeeded(conditions) + .addOnSuccessListener( + new OnSuccessListener() { + @Override + public void onSuccess(Void v) { + translator.translate(text) + .addOnSuccessListener( + new OnSuccessListener() { + @Override + public void onSuccess(@NonNull String translatedText) { + intf.onTranslated(translatedText); + } + }) + .addOnFailureListener( + new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception ex) { + intf.onError(ex); + } + }); + } + }) + .addOnFailureListener( + new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception ex) { + intf.onError(ex); + } + }); + } + }) + .addOnFailureListener( + new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception ex) { + intf.onError(ex); + } + }); + } + + interface ITranslate { + void onTranslated(String text); + + void onError(Throwable ex); + } }