Refactoring

This commit is contained in:
M66B 2020-06-27 14:36:32 +02:00
parent c338aeeae7
commit cb745e5d69
2 changed files with 94 additions and 59 deletions

View File

@ -15,4 +15,14 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
.setTitle(R.string.title_translate) .setTitle(R.string.title_translate)
.create(); .create();
} }
static void Translate(String text, String targetLanguage, ITranslate intf) {
// Stub
}
interface ITranslate {
void onTranslated(String text);
void onError(Throwable ex);
}
} }

View File

@ -44,6 +44,28 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
String targetLanguage = map.get(items[which]); String targetLanguage = map.get(items[which]);
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();
}
@Override
public void onDismiss(@NonNull DialogInterface dialog) {
}
static void Translate(String text, String targetLanguage, ITranslate intf) {
LanguageIdentifier languageIdentifier = LanguageIdentification.getClient( LanguageIdentifier languageIdentifier = LanguageIdentification.getClient(
new LanguageIdentificationOptions.Builder() new LanguageIdentificationOptions.Builder()
.setConfidenceThreshold(0.34f) .setConfidenceThreshold(0.34f)
@ -57,6 +79,11 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
if (sourceLanguage.equals("und")) if (sourceLanguage.equals("und"))
sourceLanguage = TranslateLanguage.ENGLISH; sourceLanguage = TranslateLanguage.ENGLISH;
if (sourceLanguage.equals(targetLanguage)) {
intf.onTranslated(text);
return;
}
TranslatorOptions options = new TranslatorOptions.Builder() TranslatorOptions options = new TranslatorOptions.Builder()
.setSourceLanguage(sourceLanguage) .setSourceLanguage(sourceLanguage)
.setTargetLanguage(targetLanguage) .setTargetLanguage(targetLanguage)
@ -75,15 +102,14 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
new OnSuccessListener<String>() { new OnSuccessListener<String>() {
@Override @Override
public void onSuccess(@NonNull String translatedText) { public void onSuccess(@NonNull String translatedText) {
getArguments().putString("translated", translatedText); intf.onTranslated(translatedText);
sendResult(RESULT_OK);
} }
}) })
.addOnFailureListener( .addOnFailureListener(
new OnFailureListener() { new OnFailureListener() {
@Override @Override
public void onFailure(@NonNull Exception ex) { public void onFailure(@NonNull Exception ex) {
Log.unexpectedError(getParentFragmentManager(), ex); intf.onError(ex);
} }
}); });
} }
@ -92,7 +118,7 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
new OnFailureListener() { new OnFailureListener() {
@Override @Override
public void onFailure(@NonNull Exception ex) { public void onFailure(@NonNull Exception ex) {
Log.unexpectedError(getParentFragmentManager(), ex); intf.onError(ex);
} }
}); });
} }
@ -101,15 +127,14 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
new OnFailureListener() { new OnFailureListener() {
@Override @Override
public void onFailure(@NonNull Exception ex) { public void onFailure(@NonNull Exception ex) {
Log.unexpectedError(getParentFragmentManager(), ex); intf.onError(ex);
} }
}); });
} }
})
.create();
}
@Override interface ITranslate {
public void onDismiss(@NonNull DialogInterface dialog) { void onTranslated(String text);
void onError(Throwable ex);
} }
} }