Skip to content

Commit d0f1604

Browse files
nicofrandNicolas Frandeboeuf
authored and
Nicolas Frandeboeuf
committed
Allow to remove all checked items (fix #1521)
Signed-off-by: nicofrand <[email protected]>
1 parent e128d83 commit d0f1604

File tree

9 files changed

+118
-2
lines changed

9 files changed

+118
-2
lines changed

app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
236236
} else if (itemId == R.id.menu_title) {
237237
showEditTitleDialog();
238238
return true;
239-
} else if (itemId == R.id.menu_move) {
239+
} else if (itemId == R.id.menu_remove_checked_items) {
240+
removeCheckedItems();
241+
return true;
242+
} else if (itemId == R.id.menu_move) {
240243
executor.submit(() -> AccountPickerDialogFragment
241244
.newInstance(new ArrayList<>(repo.getAccounts()), note.getAccountId())
242245
.show(requireActivity().getSupportFragmentManager(), BaseNoteFragment.class.getSimpleName()));
@@ -345,7 +348,7 @@ private void showCategorySelector() {
345348
}
346349

347350
/**
348-
* Opens a dialog in order to chose a category
351+
* Opens a dialog in order to edit the title
349352
*/
350353
public void showEditTitleDialog() {
351354
saveNote(null);
@@ -360,6 +363,28 @@ public void showEditTitleDialog() {
360363
editTitleFragment.show(manager, fragmentId);
361364
}
362365

366+
/**
367+
* Removes all checked items from a note
368+
*
369+
* @return
370+
*/
371+
public String removeCheckedItems() {
372+
Log.d(TAG, "removeCheckedItems()");
373+
if (note != null) {
374+
final var oldContent = note.getContent();
375+
final var newContent = NoteUtil.getContentWithoutCheckedItems(oldContent);
376+
if (!oldContent.equals(newContent)) {
377+
note.setContent(newContent);
378+
}
379+
380+
return newContent;
381+
} else {
382+
Log.e(TAG, "note is null");
383+
}
384+
385+
return "";
386+
}
387+
363388
@Override
364389
public void onCategoryChosen(String category) {
365390
repo.setCategory(localAccount, note.getId(), category);

app/src/main/java/it/niedermann/owncloud/notes/edit/NoteEditFragment.java

+10
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ protected void colorWithText(@NonNull String newText, @Nullable Integer current,
255255
}
256256
}
257257

258+
public String removeCheckedItems() {
259+
String newContent = super.removeCheckedItems();
260+
261+
if (note != null) {
262+
binding.editContent.setMarkdownString(newContent);
263+
}
264+
265+
return newContent;
266+
}
267+
258268
@Override
259269
public void applyBrand(int color) {
260270
super.applyBrand(color);

app/src/main/java/it/niedermann/owncloud/notes/edit/NotePreviewFragment.java

+10
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ protected void colorWithText(@NonNull String newText, @Nullable Integer current,
152152
}
153153
}
154154

155+
public String removeCheckedItems() {
156+
String newContent = super.removeCheckedItems();
157+
158+
if (note != null) {
159+
binding.singleNoteContent.setMarkdownString(newContent);
160+
}
161+
162+
return newContent;
163+
}
164+
155165
@Override
156166
protected String getContent() {
157167
return changedText;

app/src/main/java/it/niedermann/owncloud/notes/edit/NoteReadonlyFragment.java

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void onPrepareOptionsMenu(@NonNull Menu menu) {
2525
menu.findItem(R.id.menu_share).setVisible(false);
2626
menu.findItem(R.id.menu_move).setVisible(false);
2727
menu.findItem(R.id.menu_category).setVisible(false);
28+
menu.findItem(R.id.menu_remove_checked_items).setVisible(false);
2829
menu.findItem(R.id.menu_title).setVisible(false);
2930
if (menu.findItem(MENU_ID_PIN) != null)
3031
menu.findItem(MENU_ID_PIN).setVisible(false);

app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteUtil.java

+26
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import static it.niedermann.android.markdown.MarkdownUtil.removeMarkdown;
1313
import static it.niedermann.android.markdown.MarkdownUtil.replaceCheckboxesWithEmojis;
1414

15+
import java.util.regex.Matcher;
16+
import java.util.regex.Pattern;
17+
1518
/**
1619
* Provides basic functionality for Note operations.
1720
* Created by stefan on 06.10.15.
@@ -123,6 +126,29 @@ public static String getLineWithoutMarkdown(@NonNull String content, int lineNum
123126
return line;
124127
}
125128

129+
/**
130+
* Strips all lines beginning with a filled checkbox from the Markdown.
131+
*
132+
* @param content String
133+
* @return newContent String
134+
*/
135+
@NonNull
136+
public static String getContentWithoutCheckedItems(@NonNull String content) {
137+
final Pattern pattern = Pattern.compile(
138+
"^ *[-+*] \\[[xX]\\].*$",
139+
Pattern.MULTILINE
140+
);
141+
final Matcher matcher = pattern.matcher(content);
142+
143+
// We can't just replace the matcher with an empty string as this would
144+
// let a blank line. To be able to remove the blank line as well, we set
145+
// a random-ish token that we will then match with the EOL as well and
146+
// remove them all together.
147+
return matcher
148+
.replaceAll("RANDOM-ISH_TEXT_TO_REPLACE")
149+
.replaceAll("RANDOM-ISH_TEXT_TO_REPLACE\n?", "");
150+
}
151+
126152
@NonNull
127153
public static String extendCategory(@NonNull String category) {
128154
return category.replace("/", " / ");

app/src/main/res/menu/menu_note_fragment.xml

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
android:orderInCategory="100"
2929
android:title="@string/menu_change_category"
3030
app:showAsAction="ifRoom" />
31+
<item
32+
android:id="@+id/menu_remove_checked_items"
33+
android:icon="@drawable/ic_delete_grey600_24dp"
34+
android:orderInCategory="100"
35+
android:title="@string/menu_remove_checked_items"
36+
app:showAsAction="ifRoom" />
3137
<item
3238
android:id="@+id/menu_share"
3339
android:icon="@drawable/ic_share_white_24dp"

app/src/main/res/values-fr/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
<string name="append_to_note">Ajouter à la note</string>
172172
<string name="change_note_title">Modifier le titre de la note</string>
173173
<string name="menu_edit_title">Modifier le titre</string>
174+
<string name="menu_remove_checked_items">Supprimer éléments cochés</string>
174175
<string name="simple_security">Sécurité</string>
175176
<string name="appearance_and_behavior">Apparence et comportement</string>
176177
<string name="simple_synchronization">Synchronisation</string>

app/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
<string name="append_to_note">Append to note</string>
210210
<string name="change_note_title">Change note title</string>
211211
<string name="menu_edit_title">Edit title</string>
212+
<string name="menu_remove_checked_items">Remove checked items</string>
212213
<string name="simple_security">Security</string>
213214
<string name="appearance_and_behavior">Appearance and behavior</string>
214215
<string name="simple_synchronization">Synchronization</string>

app/src/test/java/it/niedermann/owncloud/notes/shared/util/NoteUtilTest.java

+36
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,40 @@ public void testGenerateNoteExcerpt_sdk_30() {
9999
// content has markdown while titles markdown is already stripped
100100
assertEquals("Title Bar", NoteUtil.generateNoteExcerpt("# Title\n- Title\n- Bar", "Title"));
101101
}
102+
103+
@Test
104+
public void testGetContentWithoutCheckedItems() {
105+
String newLine = System.getProperty("line.separator");
106+
107+
String multilineWithCheckboxes = "- [ ] Line A"
108+
.concat(newLine)
109+
.concat("- [x] Line B")
110+
.concat(newLine)
111+
.concat("- [X] Line C")
112+
.concat(newLine)
113+
.concat(newLine)
114+
.concat("* [x] Line D")
115+
.concat(newLine)
116+
.concat("* [ ] Line E")
117+
.concat(newLine)
118+
.concat("+ [x] Line F")
119+
.concat(newLine)
120+
.concat("+ [ ] Line G")
121+
.concat(newLine)
122+
.concat(" + [ ] G1")
123+
.concat(newLine)
124+
.concat(" + [x] G2");
125+
126+
String expected = "- [ ] Line A"
127+
.concat(newLine)
128+
.concat(newLine)
129+
.concat("* [ ] Line E")
130+
.concat(newLine)
131+
.concat("+ [ ] Line G")
132+
.concat(newLine)
133+
.concat(" + [ ] G1")
134+
.concat(newLine);
135+
136+
assertEquals(expected, NoteUtil.getContentWithoutCheckedItems(multilineWithCheckboxes));
137+
}
102138
}

0 commit comments

Comments
 (0)