From 4ad7f6d82b8d5b2cffe35e86240f3c3877440b81 Mon Sep 17 00:00:00 2001 From: stefan-niedermann Date: Sat, 18 Jan 2020 11:15:27 +0100 Subject: [PATCH] #608 Checklist auto-continuation Respect also checked checkboxes and starred checkboxes as indicator --- .../android/fragment/NoteEditFragment.java | 50 +++++----------- .../owncloud/notes/util/NotesTextWatcher.java | 58 +++++++++++++++++++ 2 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/it/niedermann/owncloud/notes/util/NotesTextWatcher.java diff --git a/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NoteEditFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NoteEditFragment.java index 8a6abe23..ceb11fce 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NoteEditFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NoteEditFragment.java @@ -32,6 +32,7 @@ import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.model.CloudNote; import it.niedermann.owncloud.notes.util.ICallback; import it.niedermann.owncloud.notes.util.MarkDownUtil; +import it.niedermann.owncloud.notes.util.NotesTextWatcher; import it.niedermann.owncloud.notes.util.StyleCallback; public class NoteEditFragment extends BaseNoteFragment { @@ -55,43 +56,7 @@ public class NoteEditFragment extends BaseNoteFragment { } } }; - private final TextWatcher textWatcher = new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(final CharSequence s, int start, int before, int count) { - if (count == 1 && s.charAt(start) == '\n') { // 'Enter' was pressed - // Find start of line - int startOfLine = start; - while (startOfLine > 0 && s.charAt(startOfLine - 1) != '\n') { - startOfLine--; - } - String line = s.subSequence(startOfLine, start).toString(); - - if (line.equals("- [ ] ")) { - String newStr = new StringBuilder(s).replace(startOfLine, startOfLine + 7, "\n").toString(); - editContent.setText(newStr); - editContent.setSelection(startOfLine + 1); - } else if(line.startsWith("- [ ] ") ) { - // Line contains checkbox - String newStr = new StringBuilder(s).insert(start + count, "- [ ] ").toString(); - editContent.setText(newStr); - editContent.setSelection(start + 7); - } - } - } - - @Override - public void afterTextChanged(final Editable s) { - unsavedEdit = true; - if (!saveActive) { - handler.removeCallbacks(runAutoSave); - handler.postDelayed(runAutoSave, DELAY); - } - } - }; + private TextWatcher textWatcher; public static NoteEditFragment newInstance(long accountId, long noteId) { NoteEditFragment f = new NoteEditFragment(); @@ -135,6 +100,17 @@ public class NoteEditFragment extends BaseNoteFragment { ButterKnife.bind(this, Objects.requireNonNull(getView())); + textWatcher = new NotesTextWatcher(editContent) { + @Override + public void afterTextChanged(final Editable s) { + unsavedEdit = true; + if (!saveActive) { + handler.removeCallbacks(runAutoSave); + handler.postDelayed(runAutoSave, DELAY); + } + } + }; + if (note != null) { setActiveTextView(editContent); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/util/NotesTextWatcher.java b/app/src/main/java/it/niedermann/owncloud/notes/util/NotesTextWatcher.java new file mode 100644 index 00000000..1d924169 --- /dev/null +++ b/app/src/main/java/it/niedermann/owncloud/notes/util/NotesTextWatcher.java @@ -0,0 +1,58 @@ +package it.niedermann.owncloud.notes.util; + +import android.text.TextWatcher; +import android.widget.EditText; + +/** + * Implements auto-continuation for checked-lists + */ +public abstract class NotesTextWatcher implements TextWatcher { + private static final String uncheckedMinusCheckbox = "- [ ] "; + private static final String uncheckedStarCheckbox = "* [ ] "; + private static final String checkedMinusCheckbox = "- [x] "; + private static final String checkedStarCheckbox = "* [x] "; + private static final int lengthCheckbox = 6; + + private EditText editText; + + protected NotesTextWatcher(EditText editText) { + this.editText = editText; + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + // Nothing to do here... + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + if (count == 1 && s.charAt(start) == '\n') { // 'Enter' was pressed + // Find start of line + int startOfLine = start; + while (startOfLine > 0 && s.charAt(startOfLine - 1) != '\n') { + startOfLine--; + } + String line = s.subSequence(startOfLine, start).toString(); + + if (line.equals(uncheckedMinusCheckbox) || line.equals(uncheckedStarCheckbox)) { + editText.setSelection(startOfLine + 1); + setNewText(new StringBuilder(s).replace(startOfLine, startOfLine + lengthCheckbox + 1, "\n"), startOfLine + 1); + } else if(lineStartsWithCheckbox(line, false)) { + setNewText(new StringBuilder(s).insert(start + count, uncheckedMinusCheckbox), start + lengthCheckbox + 1); + } else if(lineStartsWithCheckbox(line, true)) { + setNewText(new StringBuilder(s).insert(start + count, uncheckedStarCheckbox), start + lengthCheckbox + 1); + } + } + } + + private static boolean lineStartsWithCheckbox(String line, boolean starAsLeadingCharacter) { + return starAsLeadingCharacter + ? line.startsWith(uncheckedStarCheckbox) || line.startsWith(checkedStarCheckbox) + : line.startsWith(uncheckedMinusCheckbox) || line.startsWith(checkedMinusCheckbox); + } + + private void setNewText(StringBuilder newText, int selection) { + editText.setText(newText); + editText.setSelection(selection); + } +}