-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathNotePreviewFragment.java
210 lines (180 loc) · 8.35 KB
/
NotePreviewFragment.java
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
/*
* 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.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
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;
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);
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 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);
noteLoaded = true;
registerInternalNoteLinkHandler();
binding.singleNoteContent.setMarkdownImageUrlPrefix(repo.getAccountById(note.getAccountId()).getUrl()+"/index.php/apps/notes/notes/"+note.getRemoteId()+"/attachment?path=");
changedText = note.getContent();
binding.singleNoteContent.setMarkdownString(note.getContent(), setScrollY);
binding.singleNoteContent.getMarkdownString().observe(requireActivity(), (newContent) -> {
changedText = newContent.toString();
saveNote(null);
});
}
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);
executor.submit(() -> {
try {
final var account = repo.getAccountByName(SingleAccountHelper.getCurrentSingleSignOnAccount(requireContext()).name);
repo.addCallbackPull(account, () -> executor.submit(() -> {
note = repo.getNoteById(note.getId());
changedText = note.getContent();
requireActivity().runOnUiThread(() -> {
binding.singleNoteContent.setMarkdownString(note.getContent());
binding.swiperefreshlayout.setRefreshing(false);
});
}));
repo.scheduleSync(account, false);
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
e.printStackTrace();
}
});
} 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());
binding.singleNoteContent.setSearchColor(color);
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;
}
}