Keep an empty note if the title has been set manually

This commit is contained in:
Stefan Niedermann 2020-06-04 11:46:45 +02:00
parent 49a0e09a68
commit b9ccfd41ea
3 changed files with 24 additions and 6 deletions

View file

@ -62,6 +62,7 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
private DBNote originalNote;
protected NotesDatabase db;
private NoteFragmentListener listener;
private boolean titleModified = false;
protected boolean isNew = true;
@ -242,7 +243,7 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
}
public void onCloseNote() {
if (originalNote == null && getContent().isEmpty()) {
if (!titleModified && originalNote == null && getContent().isEmpty()) {
db.deleteNoteAndSync(ssoAccount, note.getId());
}
}
@ -261,6 +262,7 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
} else {
note = db.updateNoteAndSync(ssoAccount, localAccount.getId(), note, newContent, callback);
listener.onNoteUpdated(note);
requireActivity().invalidateOptionsMenu();
}
} else {
Log.e(TAG, "note is null");
@ -327,7 +329,9 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
@Override
public void onTitleEdited(String newTitle) {
// db.setTitle(ssoAccount, note, newTitle);
titleModified = true;
note.setTitle(newTitle);
note = db.updateNoteAndSync(ssoAccount, localAccount.getId(), note, note.getContent(), newTitle, null);
listener.onNoteUpdated(note);
}

View file

@ -492,22 +492,36 @@ public class NotesDatabase extends AbstractNotesDatabase {
return db.insert(table_category, null, values);
}
public DBNote updateNoteAndSync(SingleSignOnAccount ssoAccount, long accountId, @NonNull DBNote oldNote, @Nullable String newContent, @Nullable ISyncCallback callback) {
return updateNoteAndSync(ssoAccount, accountId, oldNote, newContent, null, callback);
}
/**
* Updates a single Note with a new content.
* The title is derived from the new content automatically, and modified date as well as DBStatus are updated, too -- if the content differs to the state in the database.
*
* @param oldNote Note to be changed
* @param newContent New content. If this is <code>null</code>, then <code>oldNote</code> is saved again (useful for undoing changes).
* @param newTitle New title. If this is <code>null</code>, then either the old title is reused (in case the note has been synced before) or a title is generated (in case it is a new note)
* @param callback When the synchronization is finished, this callback will be invoked (optional).
* @return changed note if differs from database, otherwise the old note.
*/
public DBNote updateNoteAndSync(SingleSignOnAccount ssoAccount, long accountId, @NonNull DBNote oldNote, @Nullable String newContent, @Nullable ISyncCallback callback) {
//debugPrintFullDB();
public DBNote updateNoteAndSync(SingleSignOnAccount ssoAccount, long accountId, @NonNull DBNote oldNote, @Nullable String newContent, @Nullable String newTitle, @Nullable ISyncCallback callback) {
DBNote newNote;
if (newContent == null) {
newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.isFavorite(), oldNote.getCategory(), oldNote.getEtag(), DBStatus.LOCAL_EDITED, accountId, oldNote.getExcerpt());
} else {
newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), Calendar.getInstance(), oldNote.getRemoteId() == 0 ? NoteUtil.generateNonEmptyNoteTitle(newContent, getContext()) : oldNote.getTitle(), newContent, oldNote.isFavorite(), oldNote.getCategory(), oldNote.getEtag(), DBStatus.LOCAL_EDITED, accountId, NoteUtil.generateNoteExcerpt(newContent));
final String title;
if (newTitle != null) {
title = newTitle;
} else {
if (oldNote.getRemoteId() == 0) {
title = NoteUtil.generateNonEmptyNoteTitle(newContent, getContext());
} else {
title = oldNote.getTitle();
}
}
newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), Calendar.getInstance(), title, newContent, oldNote.isFavorite(), oldNote.getCategory(), oldNote.getEtag(), DBStatus.LOCAL_EDITED, accountId, NoteUtil.generateNoteExcerpt(newContent));
}
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();

View file

@ -73,7 +73,7 @@ public class NoteUtil {
* @return truncated string
*/
@NonNull
private static String truncateString(@NonNull String str, int len) {
private static String truncateString(@NonNull String str, @SuppressWarnings("SameParameterValue") int len) {
return str.substring(0, Math.min(len, str.length()));
}