@@ -249,17 +249,31 @@ 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+ private String getPreferenceMode (long accountId , long noteId ) {
257+ final var defaultMode = getString (R .string .pref_value_mode_edit );
258+ final var prefValueDirectEdit = getString (R .string .pref_value_mode_direct_edit );
259+
260+ if (noteId > 0 ) {
261+ final Note note = repo .getNoteById (noteId );
262+ if (note != null && note .getNoteMode () != null ) {
263+ final String noteMode = note .getNoteMode ();
264+ if (noteMode .equals (prefValueDirectEdit )) {
265+ final Account accountById = repo .getAccountById (accountId );
266+ if (accountById == null || !accountById .isDirectEditingAvailable ()) {
267+ return defaultMode ;
268+ }
269+ }
270+ return noteMode ;
271+ }
272+ }
256273
257274 final var prefKeyNoteMode = getString (R .string .pref_key_note_mode );
258275 final var prefKeyLastMode = getString (R .string .pref_key_last_note_mode );
259- final var defaultMode = getString (R .string .pref_value_mode_edit );
260276 final var prefValueLast = getString (R .string .pref_value_mode_last );
261- final var prefValueDirectEdit = getString (R .string .pref_value_mode_direct_edit );
262-
263277
264278 final var preferences = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
265279 final String modePreference = preferences .getString (prefKeyNoteMode , defaultMode );
@@ -282,7 +296,7 @@ private String getPreferenceMode(long accountId) {
282296
283297 private BaseNoteFragment getNoteFragment (long accountId , long noteId , final @ Nullable String modePref ) {
284298
285- final var effectiveMode = modePref == null ? getPreferenceMode (accountId ) : modePref ;
299+ final var effectiveMode = modePref == null ? getPreferenceMode (accountId , noteId ) : modePref ;
286300
287301 final var prefValueEdit = getString (R .string .pref_value_mode_edit );
288302 final var prefValueDirectEdit = getString (R .string .pref_value_mode_direct_edit );
@@ -302,7 +316,7 @@ private BaseNoteFragment getNoteFragment(long accountId, long noteId, final @Nul
302316
303317 @ NonNull
304318 private BaseNoteFragment getNewNoteFragment (Note newNote ) {
305- final var mode = getPreferenceMode (getAccountId ());
319+ final var mode = getPreferenceMode (getAccountId (), 0 );
306320
307321 final var prefValueDirectEdit = getString (R .string .pref_value_mode_direct_edit );
308322
@@ -397,18 +411,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
397411 * Send result and closes the Activity
398412 */
399413 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- }
412414 fragment .onCloseNote ();
413415
414416 if (isTaskRoot ()) {
@@ -435,12 +437,19 @@ public void onNoteUpdated(Note note) {
435437
436438 @ Override
437439 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 );
440+ final String modeString = switch (mode ) {
441+ case EDIT -> getString (R .string .pref_value_mode_edit );
442+ case PREVIEW -> getString (R .string .pref_value_mode_preview );
443+ case DIRECT_EDIT -> getString (R .string .pref_value_mode_direct_edit );
442444 default -> throw new IllegalStateException ("Unknown mode: " + mode );
445+ };
446+ final long noteId = getNoteId ();
447+ if (noteId > 0 ) {
448+ repo .updateNoteMode (noteId , modeString );
443449 }
450+ PreferenceManager .getDefaultSharedPreferences (getApplicationContext ())
451+ .edit ().putString (getString (R .string .pref_key_last_note_mode ), modeString ).apply ();
452+ launchExistingNote (getAccountId (), noteId , modeString , reloadNote );
444453 }
445454
446455
0 commit comments