Compose translate subject

pull/213/head
M66B 2 years ago
parent e696cadaf0
commit 479aa72faa

@ -2643,6 +2643,13 @@ public class FragmentCompose extends FragmentBase {
if (languages == null)
languages = new ArrayList<>();
boolean subjectHasFocus = etSubject.hasFocus();
boolean canTranslate;
if (subjectHasFocus) {
CharSequence text = etSubject.getText();
canTranslate = (text != null && !TextUtils.isEmpty(text.toString().trim()));
} else {
int s = etBody.getSelectionStart();
Editable edit = etBody.getText();
if (s > 1 && s <= edit.length() &&
@ -2652,7 +2659,8 @@ public class FragmentCompose extends FragmentBase {
etBody.setSelection(s - 1, s - 1);
Pair<Integer, Integer> paragraph = StyleHelper.getParagraph(etBody);
boolean canTranslate = (DeepL.canTranslate(context) && paragraph != null);
canTranslate = (DeepL.canTranslate(context) && paragraph != null);
}
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, getViewLifecycleOwner(), anchor);
@ -2696,6 +2704,34 @@ public class FragmentCompose extends FragmentBase {
}
private void onMenuTranslate(String target) {
if (subjectHasFocus) {
CharSequence text = etSubject.getText();
if (text == null && TextUtils.isEmpty(text.toString().trim()))
return;
Bundle args = new Bundle();
args.putString("target", target);
args.putCharSequence("text", text);
new SimpleTask<DeepL.Translation>() {
@Override
protected DeepL.Translation onExecute(Context context, Bundle args) throws Throwable {
String target = args.getString("target");
CharSequence text = args.getCharSequence("text");
return DeepL.translate(text, true, target, context);
}
@Override
protected void onExecuted(Bundle args, DeepL.Translation translation) {
etSubject.setText(translation.translated_text);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex, !(ex instanceof IOException));
}
}.serial().execute(FragmentCompose.this, args, "compose:translate");
} else {
final Pair<Integer, Integer> paragraph = StyleHelper.getParagraph(etBody);
if (paragraph == null)
return;
@ -2819,6 +2855,7 @@ public class FragmentCompose extends FragmentBase {
}
}.serial().execute(FragmentCompose.this, args, "compose:translate");
}
}
});
popupMenu.showWithIcons(context, anchor);

Loading…
Cancel
Save