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
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,7 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
val updateDisposable = Single.just(note.remoteId)
.map { remoteId ->
val newNote = notesApi.getNote(remoteId).singleOrError().blockingGet().response
val localAccount = repo.getAccountByName(account.name)
repo.updateNoteAndSync(localAccount, note, newNote.content, newNote.title, null)
repo.updateNoteFromRemote(note.id, newNote)
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,20 @@ public int updateIfNotModifiedLocallyAndAnyRemoteColumnHasChanged(long id, Long
return db.getNoteDao().updateIfNotModifiedLocallyAndAnyRemoteColumnHasChanged(id, modified, title, favorite, category, eTag, content, excerpt);
}

@WorkerThread
public int updateNoteFromRemote(long localNoteId, @NonNull Note remoteNote) {
final var modified = Objects.requireNonNull(remoteNote.getModified()).getTimeInMillis();
return updateIfNotModifiedLocallyAndAnyRemoteColumnHasChanged(
localNoteId,
modified,
remoteNote.getTitle(),
remoteNote.getFavorite(),
remoteNote.getCategory(),
remoteNote.getETag(),
remoteNote.getContent(),
generateNoteExcerpt(remoteNote.getContent(), remoteNote.getTitle()));
}

public long countUnsynchronizedNotes(long accountId) {
final Long unsynchronizedNotesCount = db.getNoteDao().countUnsynchronizedNotes(accountId);
return unsynchronizedNotesCount == null ? 0 : unsynchronizedNotesCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,85 @@ public void testAddNote() {
assertEquals("MyContent", createdNoteFromRemote.getExcerpt());
}

@Test
public void updateNoteFromRemoteUpdatesSynchronizedNote() {
final var remoteModified = Calendar.getInstance();
final var remoteNote = new Note(
1001L,
remoteModified,
"Remote title",
"Remote title\nRemote content",
"Remote category",
true,
"remote-etag");

assertEquals(1, repo.updateNoteFromRemote(1, remoteNote));

final var storedNote = repo.getNoteById(1);
assertEquals("Remote title", storedNote.getTitle());
assertEquals("Remote title\nRemote content", storedNote.getContent());
assertEquals("Remote category", storedNote.getCategory());
assertTrue(storedNote.getFavorite());
assertEquals("remote-etag", storedNote.getETag());
assertEquals(remoteModified.getTimeInMillis(), storedNote.getModified().getTimeInMillis());
assertEquals("Remote content", storedNote.getExcerpt());
assertEquals(VOID, storedNote.getStatus());
}

@Test
public void updateNoteFromRemoteDoesNotOverwriteLocalEdits() {
final var localNote = repo.getNoteById(3);
final var remoteNote = new Note(
localNote.getRemoteId(),
Calendar.getInstance(),
"Remote title",
"Remote content",
"Remote category",
true,
"remote-etag");

assertEquals(0, repo.updateNoteFromRemote(localNote.getId(), remoteNote));

final var storedNote = repo.getNoteById(localNote.getId());
assertEquals(localNote.getTitle(), storedNote.getTitle());
assertEquals(localNote.getContent(), storedNote.getContent());
assertEquals(localNote.getCategory(), storedNote.getCategory());
assertEquals(localNote.getFavorite(), storedNote.getFavorite());
assertEquals(localNote.getETag(), storedNote.getETag());
assertEquals(localNote.getModified().getTimeInMillis(), storedNote.getModified().getTimeInMillis());
assertEquals(localNote.getExcerpt(), storedNote.getExcerpt());
assertEquals(LOCAL_EDITED, storedNote.getStatus());
}

@Test
public void updateNoteFromRemoteDoesNotDirtyIdenticalNoteOrScheduleSync() {
final var modified = Calendar.getInstance();
final var localNote = new Note(
1010L,
modified,
"Same title",
"Same content",
"Same category",
false,
"same-etag");
final var storedLocalNote = repo.addNote(account.getId(), localNote);
final var repoSpy = spy(repo);
final var identicalRemoteNote = new Note(
localNote.getRemoteId(),
modified,
localNote.getTitle(),
localNote.getContent(),
localNote.getCategory(),
localNote.getFavorite(),
localNote.getETag());

assertEquals(0, repoSpy.updateNoteFromRemote(storedLocalNote.getId(), identicalRemoteNote));

final var storedNote = repoSpy.getNoteById(storedLocalNote.getId());
assertEquals(VOID, storedNote.getStatus());
verify(repoSpy, times(0)).scheduleSync(any(), anyBoolean());
}

@Test
public void updateApiVersion() {
repo.updateApiVersion(account.getId(), "");
Expand Down