@@ -249,40 +249,52 @@ private void replaceFragment() {
249249
250250
251251 /**
252- * Returns the preferred mode for the account. If the mode is "remember last" the last mode is returned.
253- * If the mode is "direct edit" and the account does not support direct edit, the default mode is returned.
252+ * Returns the preferred mode for the note. Checks the per-note stored mode first, then falls
253+ * back to the global preference. If the mode is "remember last" the last mode is returned.
254+ * If the mode is "direct edit" and the account does not support direct edit, edit mode is returned.
254255 */
255- private String getPreferenceMode (long accountId ) {
256-
257- final var prefKeyNoteMode = getString (R .string .pref_key_note_mode );
258- final var prefKeyLastMode = getString (R .string .pref_key_last_note_mode );
256+ private String getPreferenceMode (long accountId , long noteId ) {
259257 final var defaultMode = getString (R .string .pref_value_mode_edit );
260- final var prefValueLast = getString (R .string .pref_value_mode_last );
261258 final var prefValueDirectEdit = getString (R .string .pref_value_mode_direct_edit );
262259
260+ final Note note = noteId > 0 ? repo .getNoteById (noteId ) : null ;
261+ final String storedMode = note == null ? null : note .getNoteMode ();
262+ if (storedMode != null ) {
263+ if (storedMode .equals (prefValueDirectEdit ) && !isDirectEditingAvailable (accountId )) {
264+ return defaultMode ;
265+ }
266+ return storedMode ;
267+ }
263268
264- final var preferences = PreferenceManager . getDefaultSharedPreferences ( getApplicationContext () );
265- final String modePreference = preferences . getString (prefKeyNoteMode , defaultMode );
269+ final var prefKeyNoteMode = getString ( R . string . pref_key_note_mode );
270+ final var prefValuePreview = getString (R . string . pref_value_mode_preview );
266271
267- String effectiveMode = modePreference ;
268- if (modePreference .equals (prefValueLast )) {
269- effectiveMode = preferences .getString (prefKeyLastMode , defaultMode );
272+ final var preferences = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
273+ String effectiveMode = preferences .getString (prefKeyNoteMode , defaultMode );
274+
275+ // A previously stored "remember last" value is no longer a valid mode; fall back to the default.
276+ final boolean knownMode = effectiveMode .equals (defaultMode )
277+ || effectiveMode .equals (prefValuePreview )
278+ || effectiveMode .equals (prefValueDirectEdit );
279+ if (!knownMode ) {
280+ effectiveMode = defaultMode ;
270281 }
271282
272- if (effectiveMode .equals (prefValueDirectEdit )) {
273- final Account accountById = repo .getAccountById (accountId );
274- final var directEditAvailable = accountById != null && accountById .isDirectEditingAvailable ();
275- if (!directEditAvailable ) {
276- effectiveMode = defaultMode ;
277- }
283+ if (effectiveMode .equals (prefValueDirectEdit ) && !isDirectEditingAvailable (accountId )) {
284+ effectiveMode = defaultMode ;
278285 }
279286
280287 return effectiveMode ;
281288 }
282289
290+ private boolean isDirectEditingAvailable (long accountId ) {
291+ final Account account = repo .getAccountById (accountId );
292+ return account != null && account .isDirectEditingAvailable ();
293+ }
294+
283295 private BaseNoteFragment getNoteFragment (long accountId , long noteId , final @ Nullable String modePref ) {
284296
285- final var effectiveMode = modePref == null ? getPreferenceMode (accountId ) : modePref ;
297+ final var effectiveMode = modePref == null ? getPreferenceMode (accountId , noteId ) : modePref ;
286298
287299 final var prefValueEdit = getString (R .string .pref_value_mode_edit );
288300 final var prefValueDirectEdit = getString (R .string .pref_value_mode_direct_edit );
@@ -302,7 +314,7 @@ private BaseNoteFragment getNoteFragment(long accountId, long noteId, final @Nul
302314
303315 @ NonNull
304316 private BaseNoteFragment getNewNoteFragment (Note newNote ) {
305- final var mode = getPreferenceMode (getAccountId ());
317+ final var mode = getPreferenceMode (getAccountId (), 0 );
306318
307319 final var prefValueDirectEdit = getString (R .string .pref_value_mode_direct_edit );
308320
@@ -397,18 +409,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
397409 * Send result and closes the Activity
398410 */
399411 public void close () {
400- /* TODO enhancement: store last mode in note
401- * for cross device functionality per note mode should be stored on the server.
402- */
403- final var preferences = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
404- final String prefKeyLastMode = getString (R .string .pref_key_last_note_mode );
405- if (fragment instanceof NoteEditFragment ) {
406- preferences .edit ().putString (prefKeyLastMode , getString (R .string .pref_value_mode_edit )).apply ();
407- } else if (fragment instanceof NotePreviewFragment ) {
408- preferences .edit ().putString (prefKeyLastMode , getString (R .string .pref_value_mode_preview )).apply ();
409- } else if (fragment instanceof NoteDirectEditFragment ) {
410- preferences .edit ().putString (prefKeyLastMode , getString (R .string .pref_value_mode_direct_edit )).apply ();
411- }
412412 fragment .onCloseNote ();
413413
414414 if (isTaskRoot ()) {
@@ -435,12 +435,17 @@ public void onNoteUpdated(Note note) {
435435
436436 @ Override
437437 public void changeMode (@ NonNull Mode mode , boolean reloadNote ) {
438- switch (mode ) {
439- case EDIT -> launchExistingNote ( getAccountId (), getNoteId (), getString (R .string .pref_value_mode_edit ), reloadNote );
440- case PREVIEW -> launchExistingNote ( getAccountId (), getNoteId (), getString (R .string .pref_value_mode_preview ), reloadNote );
441- case DIRECT_EDIT -> launchExistingNote ( getAccountId (), getNoteId (), getString (R .string .pref_value_mode_direct_edit ), reloadNote );
438+ final String modeString = switch (mode ) {
439+ case EDIT -> getString (R .string .pref_value_mode_edit );
440+ case PREVIEW -> getString (R .string .pref_value_mode_preview );
441+ case DIRECT_EDIT -> getString (R .string .pref_value_mode_direct_edit );
442442 default -> throw new IllegalStateException ("Unknown mode: " + mode );
443+ };
444+ final long noteId = getNoteId ();
445+ if (noteId > 0 ) {
446+ repo .updateNoteMode (noteId , modeString );
443447 }
448+ launchExistingNote (getAccountId (), noteId , modeString , reloadNote );
444449 }
445450
446451
0 commit comments