#1198 Fix multiple created notes when creating a new note and wait or toggle between edit and preview mode

This commit is contained in:
Stefan Niedermann 2021-05-10 15:22:00 +02:00
parent d508552b93
commit f07d799e47
3 changed files with 13 additions and 5 deletions

View file

@ -458,23 +458,27 @@ public class NotesRepository {
* @return changed {@link Note} if differs from database, otherwise the old {@link Note}.
*/
@WorkerThread
public Note updateNoteAndSync(Account localAccount, @NonNull Note oldNote, @Nullable String newContent, @Nullable String newTitle, @Nullable ISyncCallback callback) {
public Note updateNoteAndSync(@NonNull Account localAccount, @NonNull Note oldNote, @Nullable String newContent, @Nullable String newTitle, @Nullable ISyncCallback callback) {
final Note newNote;
// Re-read the up to date remoteId from the database because the UI might not have the state after synchronization yet
// https://github.com/stefan-niedermann/nextcloud-notes/issues/1198
@Nullable
final Long remoteId = db.getNoteDao().getRemoteId(oldNote.getId());
if (newContent == null) {
newNote = new Note(oldNote.getId(), oldNote.getRemoteId(), oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), oldNote.getExcerpt(), oldNote.getScrollY());
newNote = new Note(oldNote.getId(), remoteId, oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), oldNote.getExcerpt(), oldNote.getScrollY());
} else {
final String title;
if (newTitle != null) {
title = newTitle;
} else {
if ((oldNote.getRemoteId() == null || localAccount.getPreferredApiVersion() == null || localAccount.getPreferredApiVersion().compareTo(ApiVersion.API_VERSION_1_0) < 0) &&
if ((remoteId == null || localAccount.getPreferredApiVersion() == null || localAccount.getPreferredApiVersion().compareTo(ApiVersion.API_VERSION_1_0) < 0) &&
(defaultNonEmptyTitle.equals(oldNote.getTitle()))) {
title = NoteUtil.generateNonEmptyNoteTitle(newContent, context);
} else {
title = oldNote.getTitle();
}
}
newNote = new Note(oldNote.getId(), oldNote.getRemoteId(), Calendar.getInstance(), title, newContent, oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), generateNoteExcerpt(newContent, title), oldNote.getScrollY());
newNote = new Note(oldNote.getId(), remoteId, Calendar.getInstance(), title, newContent, oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), generateNoteExcerpt(newContent, title), oldNote.getScrollY());
}
int rows = db.getNoteDao().updateNote(newNote);
// if data was changed, set new status and schedule sync (with callback); otherwise invoke callback directly.

View file

@ -51,6 +51,9 @@ public interface NoteDao {
@Query(getNoteById)
Note getNoteById(long id);
@Query("SELECT remoteId FROM NOTE WHERE id = :id")
Long getRemoteId(long id);
@Query(count)
LiveData<Integer> count$(long accountId);

View file

@ -1,4 +1,5 @@
- 🌐 Enhanced linkifying - by @Cui-Yusong
- ⚙️ Use Retrofit for API calls (#1167)
- ⚙ Switched based for markdown rendering in widgets
- ⚙ Enable background synchronization by default (#1168) - by @MasterWanna
- ⚙ Enable background synchronization by default (#1168) - by @MasterWanna
- 🐞 Fix multiple created notes when creating a new note and wait or toggle between edit and preview mode (#1198)