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

Unverified Commit d64889ef authored by alperozturk's avatar alperozturk Committed by Andy Scherzinger
Browse files

fix race condition & update

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


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

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


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


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

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


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


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


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