From b1dacae3d1a6c6a5d7c2ddf361514118291a44a2 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 7 Sep 2018 19:30:56 +0000 Subject: [PATCH] Added bold/italic to compose Refs #49 --- .../eu/faircode/email/FragmentCompose.java | 40 ++++++++++++++++--- .../res/drawable/baseline_format_bold_24.xml | 10 +++++ .../drawable/baseline_format_italic_24.xml | 10 +++++ app/src/main/res/layout/fragment_compose.xml | 23 ++++++++++- 4 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/drawable/baseline_format_bold_24.xml create mode 100644 app/src/main/res/drawable/baseline_format_italic_24.xml diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index b2f5a82b7b..c24b9d58cb 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -25,6 +25,7 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.database.Cursor; +import android.graphics.Typeface; import android.net.Uri; import android.os.Bundle; import android.os.Handler; @@ -32,8 +33,10 @@ import android.preference.PreferenceManager; import android.provider.ContactsContract; import android.provider.OpenableColumns; import android.text.Html; +import android.text.SpannableString; import android.text.Spanned; import android.text.TextUtils; +import android.text.style.StyleSpan; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -46,6 +49,7 @@ import android.webkit.MimeTypeMap; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.FilterQueryProvider; +import android.widget.ImageButton; import android.widget.ImageView; import android.widget.MultiAutoCompleteTextView; import android.widget.ProgressBar; @@ -100,6 +104,8 @@ public class FragmentCompose extends FragmentEx { private EditText etSubject; private RecyclerView rvAttachment; private EditText etBody; + private ImageButton ibBold; + private ImageButton ibItalic; private BottomNavigationView bottom_navigation; private ProgressBar pbWait; private Group grpHeader; @@ -133,6 +139,8 @@ public class FragmentCompose extends FragmentEx { etSubject = view.findViewById(R.id.etSubject); rvAttachment = view.findViewById(R.id.rvAttachment); etBody = view.findViewById(R.id.etBody); + ibBold = view.findViewById(R.id.ibBold); + ibItalic = view.findViewById(R.id.ibItalic); bottom_navigation = view.findViewById(R.id.bottom_navigation); pbWait = view.findViewById(R.id.pbWait); grpHeader = view.findViewById(R.id.grpHeader); @@ -225,6 +233,28 @@ public class FragmentCompose extends FragmentEx { } }); + View.OnClickListener styleListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + int start = etBody.getSelectionStart(); + int end = etBody.getSelectionEnd(); + if (start > end) { + int tmp = start; + start = end; + end = tmp; + } + if (start != end) { + SpannableString s = new SpannableString(etBody.getText()); + s.setSpan(new StyleSpan(v.getId() == ibBold.getId() ? Typeface.BOLD : Typeface.ITALIC), + start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + etBody.setText(s); + } + } + }; + + ibBold.setOnClickListener(styleListener); + ibItalic.setOnClickListener(styleListener); + bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { @@ -521,7 +551,7 @@ public class FragmentCompose extends FragmentEx { args.putString("cc", etCc.getText().toString()); args.putString("bcc", etBcc.getText().toString()); args.putString("subject", etSubject.getText().toString()); - args.putString("body", etBody.getText().toString()); + args.putString("body", Html.toHtml(etBody.getText())); Log.i(Helper.TAG, "Run load id=" + working); actionLoader.load(this, args); @@ -981,8 +1011,6 @@ public class FragmentCompose extends FragmentEx { draft.subject = subject; draft.received = new Date().getTime(); - String pbody = "
" + body.replaceAll("\\r?\\n", "
") + "
"; - // Execute action if (action == R.id.action_delete) { draft.msgid = null; @@ -993,13 +1021,13 @@ public class FragmentCompose extends FragmentEx { } else if (action == R.id.action_save) { db.message().updateMessage(draft); - draft.write(context, pbody); + draft.write(context, body); EntityOperation.queue(db, draft, EntityOperation.ADD); } else if (action == R.id.action_send) { db.message().updateMessage(draft); - draft.write(context, pbody); + draft.write(context, body); // Check data if (draft.identity == null) @@ -1030,7 +1058,7 @@ public class FragmentCompose extends FragmentEx { draft.msgid = msgid; draft.ui_hide = false; draft.id = db.message().insertMessage(draft); - draft.write(getContext(), pbody); + draft.write(getContext(), body); // Restore attachments for (EntityAttachment attachment : attachments) { diff --git a/app/src/main/res/drawable/baseline_format_bold_24.xml b/app/src/main/res/drawable/baseline_format_bold_24.xml new file mode 100644 index 0000000000..2cf148302d --- /dev/null +++ b/app/src/main/res/drawable/baseline_format_bold_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/baseline_format_italic_24.xml b/app/src/main/res/drawable/baseline_format_italic_24.xml new file mode 100644 index 0000000000..bd21e642a7 --- /dev/null +++ b/app/src/main/res/drawable/baseline_format_italic_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/fragment_compose.xml b/app/src/main/res/layout/fragment_compose.xml index e1f01abcf5..41d5d36032 100644 --- a/app/src/main/res/layout/fragment_compose.xml +++ b/app/src/main/res/layout/fragment_compose.xml @@ -154,7 +154,7 @@ android:layout_marginStart="6dp" android:fillViewport="true" android:orientation="vertical" - app:layout_constraintBottom_toTopOf="@+id/bottom_navigation" + app:layout_constraintBottom_toTopOf="@+id/ibBold" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/vSeparator"> @@ -170,6 +170,25 @@ android:textAppearance="@style/TextAppearance.AppCompat.Small" /> + + + + + app:constraint_referenced_ids="scroll,ibBold,ibItalic,bottom_navigation" /> \ No newline at end of file