From 4920a76e1945a2db4e79300b8a7a15c0f22ac383 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 27 Apr 2019 15:58:28 +0200 Subject: [PATCH] Added insert template --- .../java/eu/faircode/email/DaoAnswer.java | 7 +++ .../java/eu/faircode/email/EntityAnswer.java | 12 +++-- .../eu/faircode/email/FragmentCompose.java | 54 +++++++++++++++++++ app/src/main/res/menu/menu_compose.xml | 5 ++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 75 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoAnswer.java b/app/src/main/java/eu/faircode/email/DaoAnswer.java index 34cb1cd3be..ad7538b4ac 100644 --- a/app/src/main/java/eu/faircode/email/DaoAnswer.java +++ b/app/src/main/java/eu/faircode/email/DaoAnswer.java @@ -19,6 +19,8 @@ package eu.faircode.email; Copyright 2018-2019 by Marcel Bokhorst (M66B) */ +import android.database.Cursor; + import androidx.lifecycle.LiveData; import androidx.room.Dao; import androidx.room.Insert; @@ -29,6 +31,11 @@ import java.util.List; @Dao public interface DaoAnswer { + @Query("SELECT id AS _id, name, text FROM answer" + + " WHERE NOT hide" + + " ORDER BY name COLLATE NOCASE") + Cursor getAnswerList(); + @Query("SELECT * FROM answer" + " WHERE :all OR NOT hide" + " ORDER BY name COLLATE NOCASE") diff --git a/app/src/main/java/eu/faircode/email/EntityAnswer.java b/app/src/main/java/eu/faircode/email/EntityAnswer.java index 2518c1d2e3..d5109f9467 100644 --- a/app/src/main/java/eu/faircode/email/EntityAnswer.java +++ b/app/src/main/java/eu/faircode/email/EntityAnswer.java @@ -77,10 +77,14 @@ public class EntityAnswer implements Serializable { } } - String text = answer.text; - text = text.replace("$name$", name == null ? "" : name); - text = text.replace("$firstname$", first == null ? "" : first); - text = text.replace("$lastname$", last == null ? "" : last); + return replacePlaceholders(answer.text, name, first, last, email); + } + + static String replacePlaceholders( + String text, String fullName, String firstName, String lastName, String email) { + text = text.replace("$name$", fullName == null ? "" : fullName); + text = text.replace("$firstname$", firstName == null ? "" : firstName); + text = text.replace("$lastname$", lastName == null ? "" : lastName); text = text.replace("$email$", email == null ? "" : email); return text; diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 713082bd98..c990b3ad07 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -755,6 +755,7 @@ public class FragmentCompose extends FragmentBase { menu.findItem(R.id.menu_attachment).setVisible(state == State.LOADED && !style); menu.findItem(R.id.menu_clear).setVisible(state == State.LOADED); menu.findItem(R.id.menu_contact_group).setVisible(state == State.LOADED); + menu.findItem(R.id.menu_template).setVisible(state == State.LOADED); menu.findItem(R.id.menu_encrypt).setVisible(state == State.LOADED); menu.findItem(R.id.menu_send_after).setVisible(state == State.LOADED); @@ -763,6 +764,7 @@ public class FragmentCompose extends FragmentBase { menu.findItem(R.id.menu_attachment).setEnabled(!busy); menu.findItem(R.id.menu_clear).setEnabled(!busy); menu.findItem(R.id.menu_contact_group).setEnabled(!busy); + menu.findItem(R.id.menu_template).setEnabled(!busy && Helper.isPro(getContext())); menu.findItem(R.id.menu_encrypt).setEnabled(!busy); menu.findItem(R.id.menu_send_after).setEnabled(!busy); @@ -801,6 +803,9 @@ public class FragmentCompose extends FragmentBase { case R.id.menu_contact_group: onMenuContactGroup(); return true; + case R.id.menu_template: + onMenuTemplate(); + return true; case R.id.menu_encrypt: onMenuEncrypt(); return true; @@ -1092,6 +1097,55 @@ public class FragmentCompose extends FragmentBase { dialog.show(); } + private void onMenuTemplate() { + new SimpleTask() { + @Override + protected Cursor onExecute(Context context, Bundle args) { + DB db = DB.getInstance(getContext()); + return db.answer().getAnswerList(); + } + + @Override + protected void onExecuted(Bundle args, Cursor cursor) { + ListView lv = new ListView(getContext()); + + final SimpleCursorAdapter adapter = new SimpleCursorAdapter( + getContext(), + R.layout.spinner_item1_dropdown, + cursor, + new String[]{"name"}, + new int[]{android.R.id.text1}, + 0); + lv.setAdapter(adapter); + + final AlertDialog dialog = new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) + .setView(lv) + .create(); + + lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + dialog.dismiss(); + + Cursor cursor = (Cursor) adapter.getItem(position); + String text = cursor.getString(cursor.getColumnIndex("text")); + text = EntityAnswer.replacePlaceholders(text, null, null, null, null); + Spanned spanned = HtmlHelper.fromHtml(text); + + etBody.getText().insert(etBody.getSelectionStart(), spanned); + } + }); + + dialog.show(); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex); + } + }.execute(this, new Bundle(), "compose:template"); + } + private void onMenuEncrypt() { encrypt = !encrypt; getActivity().invalidateOptionsMenu(); diff --git a/app/src/main/res/menu/menu_compose.xml b/app/src/main/res/menu/menu_compose.xml index 5c04736650..28c125d2e2 100644 --- a/app/src/main/res/menu/menu_compose.xml +++ b/app/src/main/res/menu/menu_compose.xml @@ -39,6 +39,11 @@ android:title="@string/title_insert_contact_group" app:showAsAction="never" /> + + Add attachment Show CC/BCC Insert contact group + Insert template Sender missing Recipient missing