4
4
import android .graphics .Typeface ;
5
5
import android .os .Bundle ;
6
6
import android .preference .PreferenceManager ;
7
+ import android .text .TextUtils ;
7
8
import android .text .method .LinkMovementMethod ;
8
9
import android .util .TypedValue ;
9
10
import android .view .LayoutInflater ;
@@ -35,6 +36,8 @@ public class NotePreviewFragment extends BaseNoteFragment {
35
36
36
37
private NoteSQLiteOpenHelper db = null ;
37
38
39
+ private String changedText ;
40
+
38
41
MarkdownProcessor markdownProcessor ;
39
42
40
43
@ BindView (R .id .swiperefreshlayout )
@@ -71,17 +74,42 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
71
74
ButterKnife .bind (this , Objects .requireNonNull (getView ()));
72
75
markdownProcessor = new MarkdownProcessor (getActivity ());
73
76
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 ());
75
102
setActiveTextView (noteContent );
76
103
noteContent .setText (markdownProcessor .parse (note .getContent ()));
104
+ changedText = note .getContent ();
77
105
noteContent .setMovementMethod (LinkMovementMethod .getInstance ());
78
106
79
107
db = NoteSQLiteOpenHelper .getInstance (getActivity ().getApplicationContext ());
80
108
// Pull to Refresh
81
109
swipeRefreshLayout .setOnRefreshListener (() -> {
82
110
if (db .getNoteServerSyncHelper ().isSyncPossible ()) {
83
111
swipeRefreshLayout .setRefreshing (true );
84
- db .getNoteServerSyncHelper ().addCallbackPull ( new ICallback () {
112
+ db .getNoteServerSyncHelper ().addCallbackPull (new ICallback () {
85
113
@ Override
86
114
public void onFinish () {
87
115
noteContent .setText (markdownProcessor .parse (db .getNote (note .getAccountId (), note .getId ()).getContent ()));
@@ -108,6 +136,6 @@ public void onScheduled() {
108
136
109
137
@ Override
110
138
protected String getContent () {
111
- return note . getContent () ;
139
+ return changedText ;
112
140
}
113
141
}
0 commit comments