Refactoring

pull/182/head
M66B 5 years ago
parent c338aeeae7
commit cb745e5d69

@ -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);
}
}

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

Loading…
Cancel
Save