#608 Checklist auto-continuation

Respect also checked checkboxes and starred checkboxes as indicator
This commit is contained in:
stefan-niedermann 2020-01-18 11:15:27 +01:00 committed by Niedermann IT-Dienstleistungen
parent ec513d19e6
commit 4ad7f6d82b
2 changed files with 71 additions and 37 deletions

View file

@ -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);

View file

@ -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);
}
}