Skip to content

Commit eff22ed

Browse files
Allow toggling checkboxes from within the preview #157 #287
1 parent eccc4e2 commit eff22ed

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

app/src/main/java/it/niedermann/owncloud/notes/android/fragment/NotePreviewFragment.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.graphics.Typeface;
55
import android.os.Bundle;
66
import android.preference.PreferenceManager;
7+
import android.text.TextUtils;
78
import android.text.method.LinkMovementMethod;
89
import android.util.TypedValue;
910
import android.view.LayoutInflater;
@@ -35,6 +36,8 @@ public class NotePreviewFragment extends BaseNoteFragment {
3536

3637
private NoteSQLiteOpenHelper db = null;
3738

39+
private String changedText;
40+
3841
MarkdownProcessor markdownProcessor;
3942

4043
@BindView(R.id.swiperefreshlayout)
@@ -71,17 +74,42 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
7174
ButterKnife.bind(this, Objects.requireNonNull(getView()));
7275
markdownProcessor = new MarkdownProcessor(getActivity());
7376
markdownProcessor.factory(TextFactory.create());
74-
markdownProcessor.config(MarkDownUtil.getMarkDownConfiguration(noteContent.getContext()).build());
77+
markdownProcessor.config(
78+
MarkDownUtil.getMarkDownConfiguration(noteContent.getContext())
79+
.setOnTodoClickCallback((view, line, lineNumber) -> {
80+
String[] lines = TextUtils.split(note.getContent(), "\\r?\\n");
81+
/*
82+
* Workaround for a bug when checkbox is the last line:
83+
* When (un)checking a checkbox which is in the last line, every time it gets toggled, the last character of the line gets lost.
84+
*/
85+
if ((lines.length - 1) == lineNumber) {
86+
if(lines[lineNumber].contains("- [ ]")) {
87+
lines[lineNumber] = lines[lineNumber].replace("- [ ]", "- [x]");
88+
} else {
89+
lines[lineNumber] = lines[lineNumber].replace("- [x]", "- [ ]");
90+
}
91+
92+
} else if (lines.length >= lineNumber) {
93+
lines[lineNumber] = line;
94+
}
95+
changedText = TextUtils.join("\n", lines);
96+
noteContent.setText(markdownProcessor.parse(changedText));
97+
saveNote(null);
98+
return line;
99+
}
100+
)
101+
.build());
75102
setActiveTextView(noteContent);
76103
noteContent.setText(markdownProcessor.parse(note.getContent()));
104+
changedText = note.getContent();
77105
noteContent.setMovementMethod(LinkMovementMethod.getInstance());
78106

79107
db = NoteSQLiteOpenHelper.getInstance(getActivity().getApplicationContext());
80108
// Pull to Refresh
81109
swipeRefreshLayout.setOnRefreshListener(() -> {
82110
if (db.getNoteServerSyncHelper().isSyncPossible()) {
83111
swipeRefreshLayout.setRefreshing(true);
84-
db.getNoteServerSyncHelper().addCallbackPull( new ICallback() {
112+
db.getNoteServerSyncHelper().addCallbackPull(new ICallback() {
85113
@Override
86114
public void onFinish() {
87115
noteContent.setText(markdownProcessor.parse(db.getNote(note.getAccountId(), note.getId()).getContent()));
@@ -108,6 +136,6 @@ public void onScheduled() {
108136

109137
@Override
110138
protected String getContent() {
111-
return note.getContent();
139+
return changedText;
112140
}
113141
}

0 commit comments

Comments
 (0)