From 26f726e04f8a094c0c2faac190ed58e517d834bb Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 5 Mar 2021 12:02:27 +0100 Subject: [PATCH] Simplified inserting template place holders --- .../eu/faircode/email/FragmentAnswer.java | 70 +++++++++++-------- app/src/main/res/menu/menu_answer.xml | 24 +++++-- app/src/main/res/values/strings.xml | 7 +- 3 files changed, 61 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentAnswer.java b/app/src/main/java/eu/faircode/email/FragmentAnswer.java index db735e0eff..477aa65b0d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAnswer.java +++ b/app/src/main/java/eu/faircode/email/FragmentAnswer.java @@ -19,7 +19,6 @@ package eu.faircode.email; Copyright 2018-2021 by Marcel Bokhorst (M66B) */ -import android.app.Dialog; import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; @@ -41,11 +40,9 @@ import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.EditText; -import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; import androidx.constraintlayout.widget.Group; import androidx.preference.PreferenceManager; @@ -223,17 +220,52 @@ public class FragmentAnswer extends FragmentBase { super.onCreateOptionsMenu(menu, inflater); } + @Override + public void onPrepareOptionsMenu(@NonNull Menu menu) { + menu.findItem(R.id.menu_placeholder_firstname).setVisible(BuildConfig.DEBUG); + menu.findItem(R.id.menu_placeholder_lastname).setVisible(BuildConfig.DEBUG); + super.onPrepareOptionsMenu(menu); + } + @Override public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == R.id.menu_help) { - onMenuHelp(); + int itemId = item.getItemId(); + if (itemId == R.id.menu_placeholder_name) { + onMenuPlaceholder("$name$"); + return true; + } else if (itemId == R.id.menu_placeholder_email) { + onMenuPlaceholder("$email$"); + return true; + } else if (itemId == R.id.menu_placeholder_firstname) { + onMenuPlaceholder("$firstname$"); + return true; + } else if (itemId == R.id.menu_placeholder_lastname) { + onMenuPlaceholder("$lastname$"); return true; } return super.onOptionsItemSelected(item); } - private void onMenuHelp() { - new FragmentInfo().show(getParentFragmentManager(), "answer:info"); + private void onMenuPlaceholder(String name) { + int start = etText.getSelectionStart(); + int end = etText.getSelectionEnd(); + if (start > end) { + int tmp = start; + start = end; + end = tmp; + } + + if (start >= 0 && start < end) + etText.getText().replace(start, end, name); + else { + if (start < 0) { + start = etText.length() - 1; + if (start < 0) + start = 0; + } + + etText.getText().insert(start, name); + } } private void onInsertImage() { @@ -470,28 +502,4 @@ public class FragmentAnswer extends FragmentBase { } else return StyleHelper.apply(action, getViewLifecycleOwner(), view.findViewById(action), etText); } - - public static class FragmentInfo extends FragmentDialogBase { - @NonNull - @Override - public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { - Spanned spanned = HtmlHelper.fromHtml("

" + - getString(R.string.title_answer_template_name) + - "
" + - getString(R.string.title_answer_template_email) + - "

", getContext()); - - View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_ask_again, null); - TextView tvMessage = dview.findViewById(R.id.tvMessage); - CheckBox cbNotAgain = dview.findViewById(R.id.cbNotAgain); - - tvMessage.setText(spanned); - cbNotAgain.setVisibility(View.GONE); - - return new AlertDialog.Builder(getContext()) - .setView(dview) - .setNegativeButton(android.R.string.cancel, null) - .create(); - } - } } diff --git a/app/src/main/res/menu/menu_answer.xml b/app/src/main/res/menu/menu_answer.xml index c8f2097d08..6aed0e6574 100644 --- a/app/src/main/res/menu/menu_answer.xml +++ b/app/src/main/res/menu/menu_answer.xml @@ -1,9 +1,19 @@ - - + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a21812e736..a1c43e7e02 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1194,8 +1194,11 @@ Favorite Hide from menus Template text - $name$ will be replaced by the sender full name - $email$ will be replaced by the sender email address + Placeholder + Sender\'s full name + Sender\'s email address + Sender\'s first name + Sender\'s last name No action Mark read