-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathNotePreviewFragment.java
More file actions
254 lines (212 loc) · 9.24 KB
/
NotePreviewFragment.java
File metadata and controls
254 lines (212 loc) · 9.24 KB
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
/*
* Nextcloud Notes - Android Client
*
* SPDX-FileCopyrightText: 2015-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package it.niedermann.owncloud.notes.edit;
import static androidx.core.view.ViewCompat.isAttachedToWindow;
import static it.niedermann.owncloud.notes.shared.util.NoteUtil.getFontSizeFromPreferences;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Layout;
import android.text.method.LinkMovementMethod;
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.widget.ScrollView;
import android.widget.Toast;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener;
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.owncloud.android.lib.common.utils.Log_OC;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandingUtil;
import it.niedermann.owncloud.notes.databinding.FragmentNotePreviewBinding;
import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.shared.util.SSOUtil;
import kotlin.Unit;
public class NotePreviewFragment extends SearchableBaseNoteFragment implements OnRefreshListener {
private static final String TAG = NotePreviewFragment.class.getSimpleName();
private String changedText;
protected FragmentNotePreviewBinding binding;
private boolean noteLoaded = false;
@Nullable
private Runnable setScrollY;
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_edit).setVisible(true);
if(getNormalEditButton().getVisibility() == View.VISIBLE) {
menu.findItem(R.id.menu_edit).setVisible(false);
}
menu.findItem(R.id.menu_preview).setVisible(false);
}
@Override
public ScrollView getScrollView() {
return binding.scrollView;
}
@Override
protected synchronized void scrollToY(int y) {
this.setScrollY = () -> {
if (binding != null) {
Log.v("SCROLL set (preview) to", y + "");
binding.scrollView.post(() -> binding.scrollView.setScrollY(y));
}
setScrollY = null;
};
}
@Override
protected FloatingActionButton getSearchNextButton() {
return binding.searchNext;
}
@Override
protected FloatingActionButton getSearchPrevButton() {
return binding.searchPrev;
}
@Override
protected @NonNull ExtendedFloatingActionButton getDirectEditingButton() {
return binding.directEditing;
}
@Override
protected ExtendedFloatingActionButton getNormalEditButton() {
return binding.edit;
}
@Override
protected Layout getLayout() {
binding.singleNoteContent.onPreDraw();
return binding.singleNoteContent.getLayout();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
binding = FragmentNotePreviewBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
binding.swiperefreshlayout.setOnRefreshListener(this);
registerInternalNoteLinkHandler();
binding.singleNoteContent.setMovementMethod(LinkMovementMethod.getInstance());
final var sp = PreferenceManager.getDefaultSharedPreferences(requireActivity().getApplicationContext());
binding.singleNoteContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, getFontSizeFromPreferences(requireContext(), sp));
if (sp.getBoolean(getString(R.string.pref_key_font), false)) {
binding.singleNoteContent.setTypeface(Typeface.MONOSPACE);
}
}
@Override
protected void onNoteLoaded(Note note) {
super.onNoteLoaded(note);
if (note == null) {
Log_OC.w(TAG, "Note is null, onNoteLoaded");
return;
}
noteLoaded = true;
registerInternalNoteLinkHandler();
lifecycleScopeIOJob(() -> {
final String content = note.getContent();
changedText = content;
onMainThread(() -> {
binding.singleNoteContent.setMarkdownString(content, setScrollY);
final var activity = getActivity();
if (activity == null) {
return Unit.INSTANCE;
}
binding.singleNoteContent.getMarkdownString().observe(activity, (newContent) -> {
changedText = newContent.toString();
saveNote(null);
});
return Unit.INSTANCE;
});
return Unit.INSTANCE;
});
}
protected void registerInternalNoteLinkHandler() {
binding.singleNoteContent.registerOnLinkClickCallback((link) -> {
try {
final long noteLocalId = repo.getLocalIdByRemoteId(this.note.getAccountId(), Long.parseLong(link));
Log.i(TAG, "Found note for remoteId \"" + link + "\" in account \"" + this.note.getAccountId() + "\" with localId + \"" + noteLocalId + "\". Attempt to open " + EditNoteActivity.class.getSimpleName() + " for this note.");
startActivity(new Intent(requireActivity().getApplicationContext(), EditNoteActivity.class).putExtra(EditNoteActivity.PARAM_NOTE_ID, noteLocalId));
return true;
} catch (NumberFormatException e) {
// Clicked link is not a long and therefore can't be a remote id.
} catch (IllegalArgumentException e) {
Log.i(TAG, "It looks like \"" + link + "\" might be a remote id of a note, but a note with this remote id could not be found in account \"" + note.getAccountId() + "\" .", e);
}
return false;
});
}
@Override
protected void colorWithText(@NonNull String newText, @Nullable Integer current, @ColorInt int color) {
if (binding != null && isAttachedToWindow(binding.singleNoteContent)) {
binding.singleNoteContent.clearFocus();
binding.singleNoteContent.setSearchText(newText, current);
}
}
@Override
protected String getContent() {
return changedText;
}
@Override
public void onRefresh() {
if (noteLoaded && repo.isSyncPossible() && SSOUtil.isConfigured(getContext())) {
binding.swiperefreshlayout.setRefreshing(true);
lifecycleScopeIOJob(() -> {
try {
final var account = repo.getAccountByName(SingleAccountHelper.getCurrentSingleSignOnAccount(requireContext()).name);
repo.addCallbackPull(account, () -> {
note = repo.getNoteById(note.getId());
final String content = note.getContent();
changedText = content;
onMainThread(() -> {
binding.singleNoteContent.setMarkdownString(content);
binding.swiperefreshlayout.setRefreshing(false);
return Unit.INSTANCE;
});
});
repo.scheduleSync(account, false);
} catch (Exception e) {
Log_OC.e(TAG, "onRefresh exception: " + e);
}
return Unit.INSTANCE;
});
} else {
binding.swiperefreshlayout.setRefreshing(false);
Toast.makeText(requireContext(), getString(R.string.error_sync, getString(R.string.error_no_network)), Toast.LENGTH_LONG).show();
}
}
@Override
public void applyBrand(int color) {
super.applyBrand(color);
final var util = BrandingUtil.of(color, requireContext());
lifecycleScopeIOJob(() -> {
try {
final var ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(getContext());
binding.singleNoteContent.setCurrentSingleSignOnAccount(ssoAccount, color);
} catch (Exception e) {
Log_OC.e(TAG, "applyBrand exception: " + e);
}
return Unit.INSTANCE;
});
binding.singleNoteContent.setHighlightColor(util.notes.getTextHighlightBackgroundColor(requireContext(), color, colorPrimary, colorAccent));
}
public static BaseNoteFragment newInstance(long accountId, long noteId) {
final var fragment = new NotePreviewFragment();
final var args = new Bundle();
args.putLong(PARAM_NOTE_ID, noteId);
args.putLong(PARAM_ACCOUNT_ID, accountId);
fragment.setArguments(args);
return fragment;
}
}