Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 663fdc49 authored by alperozturk's avatar alperozturk Committed by Andy Scherzinger
Browse files

fix race condition & update

parent 09c400e5
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -783,8 +783,15 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A

    @Override
    public void onNoteFavoriteClick(int position, View view) {
        final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(((Note) adapter.getItem(position)));
        toggleLiveData.observe(this, (next) -> toggleLiveData.removeObservers(this));
        if (!(adapter.getItem(position) instanceof Note note)) {
            return;
        }

        final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(note);
        toggleLiveData.observe(this, (next) -> {{
            toggleLiveData.removeObservers(this);
            adapter.notifyItemChanged(position);
        }});
    }

    @Override
+7 −7
Original line number Diff line number Diff line
@@ -514,14 +514,14 @@ public class MainViewModel extends AndroidViewModel {
    }

    public LiveData<Void> toggleFavoriteAndSync(Note note) {
        return switchMap(getCurrentAccount(), currentAccount -> {
        final var currentAccount = getCurrentAccount().getValue();

        if (currentAccount != null) {
            Log.v(TAG, "[toggleFavoriteAndSync] - currentAccount: " + currentAccount.getAccountName());
            repo.toggleFavoriteAndSync(currentAccount, note);
        }

        return new MutableLiveData<>(null);
        });
    }

    public LiveData<Void> deleteNoteAndSync(long id) {
+2 −3
Original line number Diff line number Diff line
@@ -540,7 +540,6 @@ public class NotesRepository {
                .collect(toMap(Note::getRemoteId, Note::getId));
    }

    // FIXME: RACE CONDITION
    @AnyThread
    public void toggleFavoriteAndSync(Account account, Note note) {
        executor.submit(() -> {
@@ -553,8 +552,8 @@ public class NotesRepository {
                if (response.isSuccessful()) {
                    final var updatedNote = response.body();
                    if (updatedNote != null) {
                        db.getNoteDao().updateNote(updatedNote);
                        scheduleSync(account, true);
                        //db.getNoteDao().updateNote(note);
                        scheduleSync(account, false);
                    }
                }
            } catch (Exception e) {