From 8fd4de0dcce2c9991b9b0593184778293b5e6dc7 Mon Sep 17 00:00:00 2001 From: stefan-niedermann Date: Tue, 31 Dec 2019 10:44:23 +0100 Subject: [PATCH] #451 Toggle checkboxes in view mode --- .../android/fragment/NotePreviewFragment.java | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NotePreviewFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NotePreviewFragment.java index 032ea2b72..c9e3c0a4e 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NotePreviewFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NotePreviewFragment.java @@ -4,6 +4,7 @@ import android.graphics.Typeface; import android.os.Bundle; import android.preference.PreferenceManager; +import android.text.TextUtils; import android.text.method.LinkMovementMethod; import android.util.TypedValue; import android.view.LayoutInflater; @@ -75,28 +76,31 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { markdownProcessor.factory(TextFactory.create()); markdownProcessor.config( MarkDownUtil.getMarkDownConfiguration(noteContent.getContext()) -// .setOnTodoClickCallback((view, line, lineNumber) -> { -// String[] lines = TextUtils.split(note.getContent(), "\\r?\\n"); -// /* -// * Workaround for a bug when checkbox is the last line: -// * When (un)checking a checkbox which is in the last line, every time it gets toggled, the last character of the line gets lost. -// */ -// if ((lines.length - 1) == lineNumber) { -// if(lines[lineNumber].contains("- [ ]")) { -// lines[lineNumber] = lines[lineNumber].replace("- [ ]", "- [x]"); -// } else { -// lines[lineNumber] = lines[lineNumber].replace("- [x]", "- [ ]"); -// } -// -// } else if (lines.length >= lineNumber) { -// lines[lineNumber] = line; -// } -// changedText = TextUtils.join("\n", lines); -// noteContent.setText(markdownProcessor.parse(changedText)); -// saveNote(null); -// return line; -// } -// ) + .setOnTodoClickCallback((view, line, lineNumber) -> { + try { + String[] lines = TextUtils.split(note.getContent(), "\\r?\\n"); + /* + * Workaround for multiple bugs: + * When (un)checking a checkbox which is in the last line, every time it gets toggled, the last character of the line gets lost. + * When (un)checking a checkbox, every markdown gets stripped in the given line argument + */ + if (lines[lineNumber].startsWith("- [ ]") || lines[lineNumber].startsWith("* [ ]")) { + lines[lineNumber] = lines[lineNumber].replace("- [ ]", "- [x]"); + lines[lineNumber] = lines[lineNumber].replace("* [ ]", "* [x]"); + } else { + lines[lineNumber] = lines[lineNumber].replace("- [x]", "- [ ]"); + lines[lineNumber] = lines[lineNumber].replace("* [x]", "* [ ]"); + } + + changedText = TextUtils.join("\n", lines); + noteContent.setText(markdownProcessor.parse(changedText)); + saveNote(null); + } catch (IndexOutOfBoundsException e) { + Toast.makeText(getActivity(), "Checkbox could not be toggled.", Toast.LENGTH_SHORT).show(); + } + return line; + } + ) .build()); setActiveTextView(noteContent); noteContent.setText(markdownProcessor.parse(note.getContent()));