From 68e7f57d7383be00708c969fbbaa3325b0111218 Mon Sep 17 00:00:00 2001 From: nicofrand Date: Fri, 14 Apr 2023 17:14:41 +0200 Subject: [PATCH] Allow to remove all checked items (fix #1521) Signed-off-by: nicofrand --- .../owncloud/notes/edit/BaseNoteFragment.java | 29 +++++++++++++-- .../owncloud/notes/edit/NoteEditFragment.java | 10 ++++++ .../notes/edit/NotePreviewFragment.java | 10 ++++++ .../notes/edit/NoteReadonlyFragment.java | 1 + .../owncloud/notes/shared/util/NoteUtil.java | 26 ++++++++++++++ app/src/main/res/menu/menu_note_fragment.xml | 6 ++++ app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + .../notes/shared/util/NoteUtilTest.java | 36 +++++++++++++++++++ 9 files changed, 118 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java index 162454336..ae4081ee2 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java @@ -236,7 +236,10 @@ public boolean onOptionsItemSelected(MenuItem item) { } else if (itemId == R.id.menu_title) { showEditTitleDialog(); return true; - } else if (itemId == R.id.menu_move) { + } else if (itemId == R.id.menu_remove_checked_items) { + removeCheckedItems(); + return true; + } else if (itemId == R.id.menu_move) { executor.submit(() -> AccountPickerDialogFragment .newInstance(new ArrayList<>(repo.getAccounts()), note.getAccountId()) .show(requireActivity().getSupportFragmentManager(), BaseNoteFragment.class.getSimpleName())); @@ -345,7 +348,7 @@ private void showCategorySelector() { } /** - * Opens a dialog in order to chose a category + * Opens a dialog in order to edit the title */ public void showEditTitleDialog() { saveNote(null); @@ -360,6 +363,28 @@ public void showEditTitleDialog() { editTitleFragment.show(manager, fragmentId); } + /** + * Removes all checked items from a note + * + * @return + */ + public String removeCheckedItems() { + Log.d(TAG, "removeCheckedItems()"); + if (note != null) { + final var oldContent = note.getContent(); + final var newContent = NoteUtil.getContentWithoutCheckedItems(oldContent); + if (!oldContent.equals(newContent)) { + note.setContent(newContent); + } + + return newContent; + } else { + Log.e(TAG, "note is null"); + } + + return ""; + } + @Override public void onCategoryChosen(String category) { repo.setCategory(localAccount, note.getId(), category); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/edit/NoteEditFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/edit/NoteEditFragment.java index 9ba17f95a..6b02a0353 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/edit/NoteEditFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/edit/NoteEditFragment.java @@ -255,6 +255,16 @@ protected void colorWithText(@NonNull String newText, @Nullable Integer current, } } + public String removeCheckedItems() { + String newContent = super.removeCheckedItems(); + + if (note != null) { + binding.editContent.setMarkdownString(newContent); + } + + return newContent; + } + @Override public void applyBrand(int color) { super.applyBrand(color); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/edit/NotePreviewFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/edit/NotePreviewFragment.java index 37048d6ac..a93cb97f2 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/edit/NotePreviewFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/edit/NotePreviewFragment.java @@ -152,6 +152,16 @@ protected void colorWithText(@NonNull String newText, @Nullable Integer current, } } + public String removeCheckedItems() { + String newContent = super.removeCheckedItems(); + + if (note != null) { + binding.singleNoteContent.setMarkdownString(newContent); + } + + return newContent; + } + @Override protected String getContent() { return changedText; diff --git a/app/src/main/java/it/niedermann/owncloud/notes/edit/NoteReadonlyFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/edit/NoteReadonlyFragment.java index 55771c247..8b098624f 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/edit/NoteReadonlyFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/edit/NoteReadonlyFragment.java @@ -25,6 +25,7 @@ public void onPrepareOptionsMenu(@NonNull Menu menu) { menu.findItem(R.id.menu_share).setVisible(false); menu.findItem(R.id.menu_move).setVisible(false); menu.findItem(R.id.menu_category).setVisible(false); + menu.findItem(R.id.menu_remove_checked_items).setVisible(false); menu.findItem(R.id.menu_title).setVisible(false); if (menu.findItem(MENU_ID_PIN) != null) menu.findItem(MENU_ID_PIN).setVisible(false); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteUtil.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteUtil.java index aae6c472e..425740ac8 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteUtil.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteUtil.java @@ -12,6 +12,9 @@ import static it.niedermann.android.markdown.MarkdownUtil.removeMarkdown; import static it.niedermann.android.markdown.MarkdownUtil.replaceCheckboxesWithEmojis; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * Provides basic functionality for Note operations. * Created by stefan on 06.10.15. @@ -123,6 +126,29 @@ public static String getLineWithoutMarkdown(@NonNull String content, int lineNum return line; } + /** + * Strips all lines beginning with a filled checkbox from the Markdown. + * + * @param content String + * @return newContent String + */ + @NonNull + public static String getContentWithoutCheckedItems(@NonNull String content) { + final Pattern pattern = Pattern.compile( + "^ *[-+*] \\[[xX]\\].*$", + Pattern.MULTILINE + ); + final Matcher matcher = pattern.matcher(content); + + // We can't just replace the matcher with an empty string as this would + // let a blank line. To be able to remove the blank line as well, we set + // a random-ish token that we will then match with the EOL as well and + // remove them all together. + return matcher + .replaceAll("RANDOM-ISH_TEXT_TO_REPLACE") + .replaceAll("RANDOM-ISH_TEXT_TO_REPLACE\n?", ""); + } + @NonNull public static String extendCategory(@NonNull String category) { return category.replace("/", " / "); diff --git a/app/src/main/res/menu/menu_note_fragment.xml b/app/src/main/res/menu/menu_note_fragment.xml index 97cfad95a..46facfc2c 100644 --- a/app/src/main/res/menu/menu_note_fragment.xml +++ b/app/src/main/res/menu/menu_note_fragment.xml @@ -28,6 +28,12 @@ android:orderInCategory="100" android:title="@string/menu_change_category" app:showAsAction="ifRoom" /> + Ajouter à la note Modifier le titre de la note Modifier le titre + Supprimer éléments cochés Sécurité Apparence et comportement Synchronisation diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c45fa5ba5..eb11e77b4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -209,6 +209,7 @@ Append to note Change note title Edit title + Remove checked items Security Appearance and behavior Synchronization diff --git a/app/src/test/java/it/niedermann/owncloud/notes/shared/util/NoteUtilTest.java b/app/src/test/java/it/niedermann/owncloud/notes/shared/util/NoteUtilTest.java index bffa68963..750d0a0d7 100644 --- a/app/src/test/java/it/niedermann/owncloud/notes/shared/util/NoteUtilTest.java +++ b/app/src/test/java/it/niedermann/owncloud/notes/shared/util/NoteUtilTest.java @@ -99,4 +99,40 @@ public void testGenerateNoteExcerpt_sdk_30() { // content has markdown while titles markdown is already stripped assertEquals("Title Bar", NoteUtil.generateNoteExcerpt("# Title\n- Title\n- Bar", "Title")); } + + @Test + public void testGetContentWithoutCheckedItems() { + String newLine = System.getProperty("line.separator"); + + String multilineWithCheckboxes = "- [ ] Line A" + .concat(newLine) + .concat("- [x] Line B") + .concat(newLine) + .concat("- [X] Line C") + .concat(newLine) + .concat(newLine) + .concat("* [x] Line D") + .concat(newLine) + .concat("* [ ] Line E") + .concat(newLine) + .concat("+ [x] Line F") + .concat(newLine) + .concat("+ [ ] Line G") + .concat(newLine) + .concat(" + [ ] G1") + .concat(newLine) + .concat(" + [x] G2"); + + String expected = "- [ ] Line A" + .concat(newLine) + .concat(newLine) + .concat("* [ ] Line E") + .concat(newLine) + .concat("+ [ ] Line G") + .concat(newLine) + .concat(" + [ ] G1") + .concat(newLine); + + assertEquals(expected, NoteUtil.getContentWithoutCheckedItems(multilineWithCheckboxes)); + } } \ No newline at end of file