8
8
import android .util .Log ;
9
9
import android .view .Menu ;
10
10
import android .view .MenuItem ;
11
+ import android .view .View ;
11
12
import android .view .WindowManager ;
12
13
import android .widget .Toast ;
13
14
14
15
import androidx .annotation .NonNull ;
16
+ import androidx .annotation .Nullable ;
15
17
import androidx .fragment .app .Fragment ;
16
18
import androidx .lifecycle .ViewModelProvider ;
17
19
import androidx .preference .PreferenceManager ;
18
20
19
21
import com .nextcloud .android .sso .exceptions .NextcloudFilesAppAccountNotFoundException ;
20
22
import com .nextcloud .android .sso .exceptions .NoCurrentAccountSelectedException ;
21
23
import com .nextcloud .android .sso .helper .SingleAccountHelper ;
24
+ import com .nextcloud .android .sso .model .SingleSignOnAccount ;
22
25
23
26
import java .io .BufferedReader ;
24
27
import java .io .IOException ;
34
37
import it .niedermann .owncloud .notes .databinding .ActivityEditBinding ;
35
38
import it .niedermann .owncloud .notes .edit .category .CategoryViewModel ;
36
39
import it .niedermann .owncloud .notes .main .MainActivity ;
40
+ import it .niedermann .owncloud .notes .persistence .NotesRepository ;
37
41
import it .niedermann .owncloud .notes .persistence .entity .Account ;
38
42
import it .niedermann .owncloud .notes .persistence .entity .Note ;
39
43
import it .niedermann .owncloud .notes .shared .model .NavigationCategory ;
@@ -57,11 +61,14 @@ public class EditNoteActivity extends LockedActivity implements BaseNoteFragment
57
61
private ActivityEditBinding binding ;
58
62
59
63
private BaseNoteFragment fragment ;
64
+ private NotesRepository repo ;
60
65
61
66
@ Override
62
67
protected void onCreate (final Bundle savedInstanceState ) {
63
68
super .onCreate (savedInstanceState );
64
69
70
+ repo = NotesRepository .getInstance (getApplicationContext ());
71
+
65
72
try {
66
73
if (SingleAccountHelper .getCurrentSingleSignOnAccount (this ) == null ) {
67
74
throw new NoCurrentAccountSelectedException ();
@@ -118,9 +125,20 @@ private long getNoteId() {
118
125
}
119
126
120
127
private long getAccountId () {
121
- return getIntent ().getLongExtra (PARAM_ACCOUNT_ID , 0 );
128
+ final long idParam = getIntent ().getLongExtra (PARAM_ACCOUNT_ID , 0 );
129
+ if (idParam == 0 ) {
130
+ try {
131
+ final SingleSignOnAccount ssoAcc = SingleAccountHelper .getCurrentSingleSignOnAccount (this );
132
+ return repo .getAccountByName (ssoAcc .name ).getId ();
133
+ } catch (NextcloudFilesAppAccountNotFoundException |
134
+ NoCurrentAccountSelectedException e ) {
135
+ Log .w (TAG , "getAccountId: no current account" , e );
136
+ }
137
+ }
138
+ return idParam ;
122
139
}
123
140
141
+
124
142
/**
125
143
* Starts the note fragment for an existing note or a new note.
126
144
* The actual behavior is triggered by the activity's intent.
@@ -145,44 +163,109 @@ private void launchNoteFragment() {
145
163
* @param noteId ID of the existing note.
146
164
*/
147
165
private void launchExistingNote (long accountId , long noteId ) {
148
- final var prefKeyNoteMode = getString (R .string .pref_key_note_mode );
149
- final var prefKeyLastMode = getString (R .string .pref_key_last_note_mode );
150
- final var prefValueEdit = getString (R .string .pref_value_mode_edit );
151
- final var prefValuePreview = getString (R .string .pref_value_mode_preview );
152
- final var prefValueLast = getString (R .string .pref_value_mode_last );
166
+ launchExistingNote (accountId , noteId , null );
167
+ }
153
168
154
- final var preferences = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
155
- final String mode = preferences .getString (prefKeyNoteMode , prefValueEdit );
156
- final String lastMode = preferences .getString (prefKeyLastMode , prefValueEdit );
157
- boolean editMode = true ;
158
- if (prefValuePreview .equals (mode ) || (prefValueLast .equals (mode ) && prefValuePreview .equals (lastMode ))) {
159
- editMode = false ;
160
- }
161
- launchExistingNote (accountId , noteId , editMode );
169
+ private void launchExistingNote (long accountId , long noteId , @ Nullable final String mode ) {
170
+ launchExistingNote (accountId , noteId , mode , false );
162
171
}
163
172
164
173
/**
165
174
* Starts a {@link NoteEditFragment} or {@link NotePreviewFragment} for an existing note.
166
175
*
167
- * @param noteId ID of the existing note.
168
- * @param edit View-mode of the fragment:
169
- * <code>true</code> for {@link NoteEditFragment},
170
- * <code>false</code> for {@link NotePreviewFragment}.
176
+ * @param noteId ID of the existing note.
177
+ * @param mode View-mode of the fragment (pref value or null). If null will be chosen based on
178
+ * user preferences.
179
+ * @param discardState If true, the state of the fragment will be discarded and a new fragment will be created
171
180
*/
172
- private void launchExistingNote (long accountId , long noteId , boolean edit ) {
181
+ private void launchExistingNote (long accountId , long noteId , @ Nullable final String mode , final boolean discardState ) {
173
182
// save state of the fragment in order to resume with the same note and originalNote
174
- Fragment .SavedState savedState = null ;
175
- if (fragment != null ) {
176
- savedState = getSupportFragmentManager ().saveFragmentInstanceState (fragment );
183
+ runOnUiThread (() -> {
184
+ Fragment .SavedState savedState = null ;
185
+ if (fragment != null && !discardState ) {
186
+ savedState = getSupportFragmentManager ().saveFragmentInstanceState (fragment );
187
+ }
188
+ fragment = getNoteFragment (accountId , noteId , mode );
189
+ if (savedState != null ) {
190
+ fragment .setInitialSavedState (savedState );
191
+ }
192
+ replaceFragment ();
193
+ });
194
+ }
195
+
196
+ private void replaceFragment () {
197
+ getSupportFragmentManager ().beginTransaction ().replace (R .id .fragment_container_view , fragment ).commit ();
198
+ if (!fragment .shouldShowToolbar ()) {
199
+ binding .toolbar .setVisibility (View .GONE );
200
+ } else {
201
+ binding .toolbar .setVisibility (View .VISIBLE );
177
202
}
178
- fragment = edit
179
- ? NoteEditFragment .newInstance (accountId , noteId )
180
- : NotePreviewFragment .newInstance (accountId , noteId );
203
+ }
204
+
205
+
206
+ /**
207
+ * Returns the preferred mode for the account. If the mode is "remember last" the last mode is returned.
208
+ * If the mode is "direct edit" and the account does not support direct edit, the default mode is returned.
209
+ */
210
+ private String getPreferenceMode (long accountId ) {
211
+
212
+ final var prefKeyNoteMode = getString (R .string .pref_key_note_mode );
213
+ final var prefKeyLastMode = getString (R .string .pref_key_last_note_mode );
214
+ final var defaultMode = getString (R .string .pref_value_mode_edit );
215
+ final var prefValueLast = getString (R .string .pref_value_mode_last );
216
+ final var prefValueDirectEdit = getString (R .string .pref_value_mode_direct_edit );
181
217
182
- if (savedState != null ) {
183
- fragment .setInitialSavedState (savedState );
218
+
219
+ final var preferences = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
220
+ final String modePreference = preferences .getString (prefKeyNoteMode , defaultMode );
221
+
222
+ String effectiveMode = modePreference ;
223
+ if (modePreference .equals (prefValueLast )) {
224
+ effectiveMode = preferences .getString (prefKeyLastMode , defaultMode );
225
+ }
226
+
227
+ if (effectiveMode .equals (prefValueDirectEdit )) {
228
+ final Account accountById = repo .getAccountById (accountId );
229
+ final var directEditAvailable = accountById != null && accountById .isDirectEditingAvailable ();
230
+ if (!directEditAvailable ) {
231
+ effectiveMode = defaultMode ;
232
+ }
233
+ }
234
+
235
+ return effectiveMode ;
236
+ }
237
+
238
+ private BaseNoteFragment getNoteFragment (long accountId , long noteId , final @ Nullable String modePref ) {
239
+
240
+ final var effectiveMode = modePref == null ? getPreferenceMode (accountId ) : modePref ;
241
+
242
+ final var prefValueEdit = getString (R .string .pref_value_mode_edit );
243
+ final var prefValueDirectEdit = getString (R .string .pref_value_mode_direct_edit );
244
+ final var prefValuePreview = getString (R .string .pref_value_mode_preview );
245
+
246
+ if (effectiveMode .equals (prefValueEdit )) {
247
+ return NoteEditFragment .newInstance (accountId , noteId );
248
+ } else if (effectiveMode .equals (prefValueDirectEdit )) {
249
+ return NoteDirectEditFragment .newInstance (accountId , noteId );
250
+ } else if (effectiveMode .equals (prefValuePreview )) {
251
+ return NotePreviewFragment .newInstance (accountId , noteId );
252
+ } else {
253
+ throw new IllegalStateException ("Unknown note modePref: " + modePref );
254
+ }
255
+ }
256
+
257
+
258
+ @ NonNull
259
+ private BaseNoteFragment getNewNoteFragment (Note newNote ) {
260
+ final var mode = getPreferenceMode (getAccountId ());
261
+
262
+ final var prefValueDirectEdit = getString (R .string .pref_value_mode_direct_edit );
263
+
264
+ if (mode .equals (prefValueDirectEdit )) {
265
+ return NoteDirectEditFragment .newInstanceWithNewNote (newNote );
266
+ } else {
267
+ return NoteEditFragment .newInstanceWithNewNote (newNote );
184
268
}
185
- getSupportFragmentManager ().beginTransaction ().replace (R .id .fragment_container_view , fragment ).commit ();
186
269
}
187
270
188
271
/**
@@ -219,10 +302,11 @@ private void launchNewNote() {
219
302
content = "" ;
220
303
}
221
304
final var newNote = new Note (null , Calendar .getInstance (), NoteUtil .generateNonEmptyNoteTitle (content , this ), content , categoryTitle , favorite , null );
222
- fragment = NoteEditFragment . newInstanceWithNewNote (newNote );
223
- getSupportFragmentManager (). beginTransaction (). replace ( R . id . fragment_container_view , fragment ). commit ();
305
+ fragment = getNewNoteFragment (newNote );
306
+ replaceFragment ();
224
307
}
225
308
309
+
226
310
private void launchReadonlyNote () {
227
311
final var intent = getIntent ();
228
312
final var content = new StringBuilder ();
@@ -238,7 +322,7 @@ private void launchReadonlyNote() {
238
322
}
239
323
240
324
fragment = NoteReadonlyFragment .newInstance (content .toString ());
241
- getSupportFragmentManager (). beginTransaction (). replace ( R . id . fragment_container_view , fragment ). commit ();
325
+ replaceFragment ();
242
326
}
243
327
244
328
@ Override
@@ -260,10 +344,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
260
344
close ();
261
345
return true ;
262
346
} else if (itemId == R .id .menu_preview ) {
263
- launchExistingNote ( getAccountId (), getNoteId () , false );
347
+ changeMode ( Mode . PREVIEW , false );
264
348
return true ;
265
349
} else if (itemId == R .id .menu_edit ) {
266
- launchExistingNote ( getAccountId (), getNoteId (), true );
350
+ changeMode ( Mode . EDIT , false );
267
351
return true ;
268
352
}
269
353
return super .onOptionsItemSelected (item );
@@ -281,8 +365,10 @@ public void close() {
281
365
final String prefKeyLastMode = getString (R .string .pref_key_last_note_mode );
282
366
if (fragment instanceof NoteEditFragment ) {
283
367
preferences .edit ().putString (prefKeyLastMode , getString (R .string .pref_value_mode_edit )).apply ();
284
- } else {
368
+ } else if ( fragment instanceof NotePreviewFragment ) {
285
369
preferences .edit ().putString (prefKeyLastMode , getString (R .string .pref_value_mode_preview )).apply ();
370
+ } else if (fragment instanceof NoteDirectEditFragment ) {
371
+ preferences .edit ().putString (prefKeyLastMode , getString (R .string .pref_value_mode_direct_edit )).apply ();
286
372
}
287
373
fragment .onCloseNote ();
288
374
@@ -308,6 +394,24 @@ public void onNoteUpdated(Note note) {
308
394
}
309
395
}
310
396
397
+ @ Override
398
+ public void changeMode (@ NonNull Mode mode , boolean reloadNote ) {
399
+ switch (mode ) {
400
+ case EDIT :
401
+ launchExistingNote (getAccountId (), getNoteId (), getString (R .string .pref_value_mode_edit ), reloadNote );
402
+ break ;
403
+ case PREVIEW :
404
+ launchExistingNote (getAccountId (), getNoteId (), getString (R .string .pref_value_mode_preview ), reloadNote );
405
+ break ;
406
+ case DIRECT_EDIT :
407
+ launchExistingNote (getAccountId (), getNoteId (), getString (R .string .pref_value_mode_direct_edit ), reloadNote );
408
+ break ;
409
+ default :
410
+ throw new IllegalStateException ("Unknown mode: " + mode );
411
+ }
412
+ }
413
+
414
+
311
415
@ Override
312
416
public void onAccountPicked (@ NonNull Account account ) {
313
417
fragment .moveNote (account );
@@ -318,4 +422,4 @@ public void applyBrand(int color) {
318
422
final var util = BrandingUtil .of (color , this );
319
423
util .notes .applyBrandToPrimaryToolbar (binding .appBar , binding .toolbar , colorAccent );
320
424
}
321
- }
425
+ }
0 commit comments