Skip to content

Commit 663fdc4

Browse files
alperozturk96AndyScherzinger
authored andcommitted
fix race condition & update
Signed-off-by: alperozturk <[email protected]>
1 parent 09c400e commit 663fdc4

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,15 @@ public void onNoteClick(int position, View v) {
783783

784784
@Override
785785
public void onNoteFavoriteClick(int position, View view) {
786-
final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(((Note) adapter.getItem(position)));
787-
toggleLiveData.observe(this, (next) -> toggleLiveData.removeObservers(this));
786+
if (!(adapter.getItem(position) instanceof Note note)) {
787+
return;
788+
}
789+
790+
final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(note);
791+
toggleLiveData.observe(this, (next) -> {{
792+
toggleLiveData.removeObservers(this);
793+
adapter.notifyItemChanged(position);
794+
}});
788795
}
789796

790797
@Override

app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,14 @@ public LiveData<Note> moveNoteToAnotherAccount(Account account, long noteId) {
514514
}
515515

516516
public LiveData<Void> toggleFavoriteAndSync(Note note) {
517-
return switchMap(getCurrentAccount(), currentAccount -> {
518-
if (currentAccount != null) {
519-
Log.v(TAG, "[toggleFavoriteAndSync] - currentAccount: " + currentAccount.getAccountName());
520-
repo.toggleFavoriteAndSync(currentAccount, note);
521-
}
517+
final var currentAccount = getCurrentAccount().getValue();
522518

523-
return new MutableLiveData<>(null);
524-
});
519+
if (currentAccount != null) {
520+
Log.v(TAG, "[toggleFavoriteAndSync] - currentAccount: " + currentAccount.getAccountName());
521+
repo.toggleFavoriteAndSync(currentAccount, note);
522+
}
523+
524+
return new MutableLiveData<>(null);
525525
}
526526

527527
public LiveData<Void> deleteNoteAndSync(long id) {

app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,6 @@ public Map<Long, Long> getIdMap(long accountId) {
540540
.collect(toMap(Note::getRemoteId, Note::getId));
541541
}
542542

543-
// FIXME: RACE CONDITION
544543
@AnyThread
545544
public void toggleFavoriteAndSync(Account account, Note note) {
546545
executor.submit(() -> {
@@ -553,8 +552,8 @@ public void toggleFavoriteAndSync(Account account, Note note) {
553552
if (response.isSuccessful()) {
554553
final var updatedNote = response.body();
555554
if (updatedNote != null) {
556-
db.getNoteDao().updateNote(updatedNote);
557-
scheduleSync(account, true);
555+
//db.getNoteDao().updateNote(note);
556+
scheduleSync(account, false);
558557
}
559558
}
560559
} catch (Exception e) {

0 commit comments

Comments
 (0)