Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,31 @@ private void replaceFragment() {


/**
* Returns the preferred mode for the account. If the mode is "remember last" the last mode is returned.
* If the mode is "direct edit" and the account does not support direct edit, the default mode is returned.
* Returns the preferred mode for the note. Checks the per-note stored mode first, then falls
* back to the global preference. If the mode is "remember last" the last mode is returned.
* If the mode is "direct edit" and the account does not support direct edit, edit mode is returned.
*/
private String getPreferenceMode(long accountId) {
private String getPreferenceMode(long accountId, long noteId) {
final var defaultMode = getString(R.string.pref_value_mode_edit);
final var prefValueDirectEdit = getString(R.string.pref_value_mode_direct_edit);

if (noteId > 0) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use early returns

final Note note = repo.getNoteById(noteId);
if (note != null && note.getNoteMode() != null) {
final String noteMode = note.getNoteMode();
if (noteMode.equals(prefValueDirectEdit)) {
final Account accountById = repo.getAccountById(accountId);
if (accountById == null || !accountById.isDirectEditingAvailable()) {
return defaultMode;
}
}
return noteMode;
}
}

final var prefKeyNoteMode = getString(R.string.pref_key_note_mode);
final var prefKeyLastMode = getString(R.string.pref_key_last_note_mode);
final var defaultMode = getString(R.string.pref_value_mode_edit);
final var prefValueLast = getString(R.string.pref_value_mode_last);
final var prefValueDirectEdit = getString(R.string.pref_value_mode_direct_edit);


final var preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final String modePreference = preferences.getString(prefKeyNoteMode, defaultMode);
Expand All @@ -282,7 +296,7 @@ private String getPreferenceMode(long accountId) {

private BaseNoteFragment getNoteFragment(long accountId, long noteId, final @Nullable String modePref) {

final var effectiveMode = modePref == null ? getPreferenceMode(accountId) : modePref;
final var effectiveMode = modePref == null ? getPreferenceMode(accountId, noteId) : modePref;

final var prefValueEdit = getString(R.string.pref_value_mode_edit);
final var prefValueDirectEdit = getString(R.string.pref_value_mode_direct_edit);
Expand All @@ -302,7 +316,7 @@ private BaseNoteFragment getNoteFragment(long accountId, long noteId, final @Nul

@NonNull
private BaseNoteFragment getNewNoteFragment(Note newNote) {
final var mode = getPreferenceMode(getAccountId());
final var mode = getPreferenceMode(getAccountId(), 0);

final var prefValueDirectEdit = getString(R.string.pref_value_mode_direct_edit);

Expand Down Expand Up @@ -397,18 +411,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
* Send result and closes the Activity
*/
public void close() {
/* TODO enhancement: store last mode in note
* for cross device functionality per note mode should be stored on the server.
*/
final var preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final String prefKeyLastMode = getString(R.string.pref_key_last_note_mode);
if (fragment instanceof NoteEditFragment) {
preferences.edit().putString(prefKeyLastMode, getString(R.string.pref_value_mode_edit)).apply();
} else if (fragment instanceof NotePreviewFragment) {
preferences.edit().putString(prefKeyLastMode, getString(R.string.pref_value_mode_preview)).apply();
} else if (fragment instanceof NoteDirectEditFragment) {
preferences.edit().putString(prefKeyLastMode, getString(R.string.pref_value_mode_direct_edit)).apply();
}
fragment.onCloseNote();

if(isTaskRoot()) {
Expand All @@ -435,12 +437,19 @@ public void onNoteUpdated(Note note) {

@Override
public void changeMode(@NonNull Mode mode, boolean reloadNote) {
switch (mode) {
case EDIT -> launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_edit), reloadNote);
case PREVIEW -> launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_preview), reloadNote);
case DIRECT_EDIT -> launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_direct_edit), reloadNote);
final String modeString = switch (mode) {
case EDIT -> getString(R.string.pref_value_mode_edit);
case PREVIEW -> getString(R.string.pref_value_mode_preview);
case DIRECT_EDIT -> getString(R.string.pref_value_mode_direct_edit);
default -> throw new IllegalStateException("Unknown mode: " + mode);
};
final long noteId = getNoteId();
if (noteId > 0) {
repo.updateNoteMode(noteId, modeString);
}
PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
.edit().putString(getString(R.string.pref_key_last_note_mode), modeString).apply();
launchExistingNote(getAccountId(), noteId, modeString, reloadNote);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import it.niedermann.owncloud.notes.persistence.migration.Migration_22_23;
import it.niedermann.owncloud.notes.persistence.migration.Migration_23_24;
import it.niedermann.owncloud.notes.persistence.migration.Migration_24_25;
import it.niedermann.owncloud.notes.persistence.migration.Migration_29_30;
import it.niedermann.owncloud.notes.persistence.migration.Migration_9_10;
import it.niedermann.owncloud.notes.shared.model.Capabilities;

Expand All @@ -58,7 +59,7 @@
NotesListWidgetData.class,
ShareEntity.class,
Capabilities.class
}, version = 29,
}, version = 30,
autoMigrations = {
@AutoMigration(from = 25, to = 26),
@AutoMigration(from = 26, to = 27),
Expand Down Expand Up @@ -101,7 +102,8 @@ private static NotesDatabase create(final Context context) {
new Migration_21_22(context),
new Migration_22_23(),
new Migration_23_24(context),
new Migration_24_25()
new Migration_24_25(),
new Migration_29_30()
)
.addCallback(new RoomDatabase.Callback() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ public void updateScrollY(long id, int scrollY) {
db.getNoteDao().updateScrollY(id, scrollY);
}

public void updateNoteMode(long id, @Nullable String noteMode) {
db.getNoteDao().updateNoteMode(id, noteMode);
}

public LiveData<List<CategoryWithNotesCount>> searchCategories$(Long accountId, String searchTerm) {
return db.getNoteDao().searchCategories$(accountId, searchTerm);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package it.niedermann.owncloud.notes.persistence.dao;

import androidx.annotation.Nullable;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
Expand Down Expand Up @@ -125,6 +126,9 @@ public interface NoteDao {
@Query("UPDATE NOTE SET scrollY = :scrollY WHERE id = :id")
void updateScrollY(long id, int scrollY);

@Query("UPDATE NOTE SET noteMode = :noteMode WHERE id = :id")
void updateNoteMode(long id, @Nullable String noteMode);

@Query("UPDATE NOTE SET status = :status WHERE id = :id")
void updateStatus(long id, DBStatus status);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public class Note implements Serializable, Item {
@ColumnInfo(defaultValue = "0")
private int scrollY = 0;

@Nullable
@ColumnInfo(defaultValue = "NULL")
private String noteMode;

public Note() {
super();
}
Expand Down Expand Up @@ -272,6 +276,15 @@ public void setScrollY(int scrollY) {
this.scrollY = scrollY;
}

@Nullable
public String getNoteMode() {
return noteMode;
}

public void setNoteMode(@Nullable String noteMode) {
this.noteMode = noteMode;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Nextcloud Notes - Android Client
*
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package it.niedermann.owncloud.notes.persistence.migration

import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

@Suppress("ClassName", "Detekt.ClassNaming")
class Migration_29_30 : Migration(29, 30) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto migration should be enough to add new column to the note table

override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE Note ADD COLUMN noteMode TEXT DEFAULT NULL")
}
}
Loading