Skip to content

Commit

Permalink
Allow toggling checkboxes from within the preview #157 #287
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-niedermann committed Dec 30, 2019
1 parent eccc4e2 commit eff22ed
Showing 1 changed file with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,6 +36,8 @@ public class NotePreviewFragment extends BaseNoteFragment {

private NoteSQLiteOpenHelper db = null;

private String changedText;

MarkdownProcessor markdownProcessor;

@BindView(R.id.swiperefreshlayout)
Expand Down Expand Up @@ -71,17 +74,42 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
ButterKnife.bind(this, Objects.requireNonNull(getView()));
markdownProcessor = new MarkdownProcessor(getActivity());
markdownProcessor.factory(TextFactory.create());
markdownProcessor.config(MarkDownUtil.getMarkDownConfiguration(noteContent.getContext()).build());
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;
}
)
.build());
setActiveTextView(noteContent);
noteContent.setText(markdownProcessor.parse(note.getContent()));
changedText = note.getContent();
noteContent.setMovementMethod(LinkMovementMethod.getInstance());

db = NoteSQLiteOpenHelper.getInstance(getActivity().getApplicationContext());
// Pull to Refresh
swipeRefreshLayout.setOnRefreshListener(() -> {
if (db.getNoteServerSyncHelper().isSyncPossible()) {
swipeRefreshLayout.setRefreshing(true);
db.getNoteServerSyncHelper().addCallbackPull( new ICallback() {
db.getNoteServerSyncHelper().addCallbackPull(new ICallback() {
@Override
public void onFinish() {
noteContent.setText(markdownProcessor.parse(db.getNote(note.getAccountId(), note.getId()).getContent()));
Expand All @@ -108,6 +136,6 @@ public void onScheduled() {

@Override
protected String getContent() {
return note.getContent();
return changedText;
}
}

0 comments on commit eff22ed

Please sign in to comment.