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

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

add update note call

parent ac148eb2
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -248,7 +248,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
            return true;
        } else if (itemId == R.id.menu_favorite) {
            note.setFavorite(!note.getFavorite());
            repo.toggleFavoriteAndSync(localAccount, note.getId());
            repo.toggleFavoriteAndSync(localAccount, note);
            listener.onNoteUpdated(note);
            prepareFavoriteOption(item);
            return true;
+1 −1
Original line number Diff line number Diff line
@@ -786,7 +786,7 @@ 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)).getId());
        final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(((Note) adapter.getItem(position)));
        toggleLiveData.observe(this, (next) -> toggleLiveData.removeObservers(this));
    }

+5 −6
Original line number Diff line number Diff line
@@ -513,15 +513,14 @@ public class MainViewModel extends AndroidViewModel {
        });
    }

    public LiveData<Void> toggleFavoriteAndSync(long noteId) {
    public LiveData<Void> toggleFavoriteAndSync(Note note) {
        return switchMap(getCurrentAccount(), currentAccount -> {
            if (currentAccount == null) {
                return new MutableLiveData<>(null);
            } else {
            if (currentAccount != null) {
                Log.v(TAG, "[toggleFavoriteAndSync] - currentAccount: " + currentAccount.getAccountName());
                repo.toggleFavoriteAndSync(currentAccount, noteId);
                return new MutableLiveData<>(null);
                repo.toggleFavoriteAndSync(currentAccount, note);
            }

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

+1 −1
Original line number Diff line number Diff line
@@ -101,7 +101,7 @@ public class NotesListViewItemTouchHelper extends ItemTouchHelper {
                    case ItemTouchHelper.RIGHT -> {
                        viewHolder.setIsRecyclable(false);
                        final var adapterNote = (Note) adapter.getItem(viewHolder.getLayoutPosition());
                        final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(adapterNote.getId());
                        final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(adapterNote);
                        toggleLiveData.observe(lifecycleOwner, (next) -> toggleLiveData.removeObservers(lifecycleOwner));
                    }
                    default -> {
+20 −3
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import static androidx.lifecycle.Transformations.distinctUntilChanged;
import static androidx.lifecycle.Transformations.map;
import static java.util.stream.Collectors.toMap;
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT;
import static it.niedermann.owncloud.notes.shared.util.ApiVersionUtil.getPreferredApiVersion;
import static it.niedermann.owncloud.notes.shared.util.NoteUtil.generateNoteExcerpt;
import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets;
import static it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget.updateSingleNoteWidgets;
@@ -47,6 +48,7 @@ import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundExce
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import com.owncloud.android.lib.common.utils.Log_OC;

import java.util.ArrayList;
import java.util.Calendar;
@@ -538,11 +540,26 @@ public class NotesRepository {
                .collect(toMap(Note::getRemoteId, Note::getId));
    }

    // FIXME: RACE CONDITION
    @AnyThread
    public void toggleFavoriteAndSync(Account account, long noteId) {
    public void toggleFavoriteAndSync(Account account, Note note) {
        executor.submit(() -> {
            db.getNoteDao().toggleFavorite(noteId);
            try {
                final var ssoAccount = AccountImporter.getSingleSignOnAccount(context, account.getAccountName());
                final var notesAPI = apiProvider.getNotesAPI(context, ssoAccount, getPreferredApiVersion(account.getApiVersion()));
                note.setFavorite(!note.getFavorite());
                final var result = notesAPI.updateNote(note);
                final var response = result.execute();
                if (response.isSuccessful()) {
                    final var updatedNote = response.body();
                    if (updatedNote != null) {
                        db.getNoteDao().updateNote(updatedNote);
                        scheduleSync(account, true);
                    }
                }
            } catch (Exception e) {
                Log_OC.e(TAG, "toggleFavoriteAndSync: " + e);
            }
        });
    }

Loading