1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
package it.niedermann.owncloud.notes.edit;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.text.Layout;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ScrollView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.FragmentNoteEditBinding;
import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.shared.model.ISyncCallback;
import it.niedermann.owncloud.notes.shared.util.DisplayUtils;
import static androidx.core.view.ViewCompat.isAttachedToWindow;
import static it.niedermann.owncloud.notes.shared.util.NoteUtil.getFontSizeFromPreferences;
public class NoteEditFragment extends SearchableBaseNoteFragment {
private static final String TAG = NoteEditFragment.class.getSimpleName();
private static final String LOG_TAG_AUTOSAVE = "AutoSave";
private static final long DELAY = 2000; // Wait for this time after typing before saving
private static final long DELAY_AFTER_SYNC = 5000; // Wait for this time after saving before checking for next save
private FragmentNoteEditBinding binding;
private Handler handler;
private boolean saveActive;
private boolean unsavedEdit;
private final Runnable runAutoSave = new Runnable() {
@Override
public void run() {
if (unsavedEdit) {
Log.d(LOG_TAG_AUTOSAVE, "runAutoSave: start AutoSave");
autoSave();
} else {
Log.d(LOG_TAG_AUTOSAVE, "runAutoSave: nothing changed");
}
}
};
private TextWatcher textWatcher;
private boolean keyboardShown = false;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler(Looper.getMainLooper());
}
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_edit).setVisible(false);
menu.findItem(R.id.menu_preview).setVisible(true);
}
@Override
public ScrollView getScrollView() {
return binding.scrollView;
}
@Override
protected void scrollToY(int y) {
if (binding != null) {
binding.scrollView.post(() -> binding.scrollView.setScrollY(y));
}
}
@Override
protected Layout getLayout() {
binding.editContent.onPreDraw();
return binding.editContent.getLayout();
}
@Override
protected FloatingActionButton getSearchNextButton() {
return binding.searchNext;
}
@Override
protected FloatingActionButton getSearchPrevButton() {
return binding.searchPrev;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
binding = FragmentNoteEditBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Nothing to do here...
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Nothing to do here...
}
@Override
public void afterTextChanged(final Editable s) {
unsavedEdit = true;
if (!saveActive) {
handler.removeCallbacks(runAutoSave);
handler.postDelayed(runAutoSave, DELAY);
}
}
};
}
@Override
public void onResume() {
super.onResume();
binding.editContent.addTextChangedListener(textWatcher);
if (keyboardShown) {
openSoftKeyboard();
}
}
@Override
protected void onNoteLoaded(Note note) {
super.onNoteLoaded(note);
if (TextUtils.isEmpty(note.getContent())) {
openSoftKeyboard();
}
binding.editContent.setMarkdownString(note.getContent());
binding.editContent.setEnabled(true);
final var sp = PreferenceManager.getDefaultSharedPreferences(requireContext().getApplicationContext());
binding.editContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, getFontSizeFromPreferences(requireContext(), sp));
if (sp.getBoolean(getString(R.string.pref_key_font), false)) {
binding.editContent.setTypeface(Typeface.MONOSPACE);
}
}
private void openSoftKeyboard() {
binding.editContent.postDelayed(() -> {
binding.editContent.requestFocus();
final var imm = (InputMethodManager) requireContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(binding.editContent, InputMethodManager.SHOW_IMPLICIT);
} else {
Log.e(TAG, InputMethodManager.class.getSimpleName() + " is null.");
}
//Without a small delay the keyboard does not show reliably
}, 100);
}
@Override
public void onPause() {
super.onPause();
binding.editContent.removeTextChangedListener(textWatcher);
cancelTimers();
final ViewGroup parentView = requireActivity().findViewById(android.R.id.content);
if (parentView != null && parentView.getChildCount() > 0) {
keyboardShown = DisplayUtils.isSoftKeyboardVisible(parentView.getChildAt(0));
} else {
keyboardShown = false;
}
}
private void cancelTimers() {
handler.removeCallbacks(runAutoSave);
}
/**
* Gets the current content of the EditText field in the UI.
*
* @return String of the current content.
*/
@Override
protected String getContent() {
final var editable = binding.editContent.getText();
return editable == null ? "" : editable.toString();
}
@Override
protected void saveNote(@Nullable ISyncCallback callback) {
super.saveNote(callback);
unsavedEdit = false;
}
/**
* Saves the current changes and show the status in the ActionBar
*/
private void autoSave() {
Log.d(LOG_TAG_AUTOSAVE, "STARTAUTOSAVE");
saveActive = true;
saveNote(new ISyncCallback() {
@Override
public void onFinish() {
onSaved();
}
@Override
public void onScheduled() {
onSaved();
}
private void onSaved() {
// AFTER SYNCHRONIZATION
Log.d(LOG_TAG_AUTOSAVE, "FINISHED AUTOSAVE");
saveActive = false;
// AFTER "DELAY_AFTER_SYNC" SECONDS: allow next auto-save or start it directly
handler.postDelayed(runAutoSave, DELAY_AFTER_SYNC);
}
});
}
@Override
protected void colorWithText(@NonNull String newText, @Nullable Integer current, int mainColor, int textColor) {
if (binding != null && isAttachedToWindow(binding.editContent)) {
binding.editContent.clearFocus();
binding.editContent.setSearchText(newText, current);
}
}
@Override
public void applyBrand(int mainColor, int textColor) {
super.applyBrand(mainColor, textColor);
binding.editContent.setSearchColor(mainColor);
binding.editContent.setHighlightColor(getTextHighlightBackgroundColor(requireContext(), mainColor, colorPrimary, colorAccent));
}
public static BaseNoteFragment newInstance(long accountId, long noteId) {
final var fragment = new NoteEditFragment();
final var args = new Bundle();
args.putLong(PARAM_NOTE_ID, noteId);
args.putLong(PARAM_ACCOUNT_ID, accountId);
fragment.setArguments(args);
return fragment;
}
public static BaseNoteFragment newInstanceWithNewNote(Note newNote) {
final var fragment = new NoteEditFragment();
final var args = new Bundle();
args.putSerializable(PARAM_NEWNOTE, newNote);
fragment.setArguments(args);
return fragment;
}
}
|