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

Unverified Commit 0ac90b7a authored by Alper Öztürk's avatar Alper Öztürk Committed by GitHub
Browse files

Merge pull request #2723 from nextcloud/backport/2714/stable-4.4

[stable-4.4] BugFix - Set As Favorite
parents 49680e36 bfd08e33
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;
+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)).getId());
        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
+9 −10
Original line number Diff line number Diff line
@@ -513,16 +513,15 @@ public class MainViewModel extends AndroidViewModel {
        });
    }

    public LiveData<Void> toggleFavoriteAndSync(long noteId) {
        return switchMap(getCurrentAccount(), currentAccount -> {
            if (currentAccount == null) {
                return new MutableLiveData<>(null);
            } else {
    public LiveData<Void> toggleFavoriteAndSync(Note note) {
        final var currentAccount = getCurrentAccount().getValue();

        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);
    }

    public LiveData<Void> deleteNoteAndSync(long id) {
+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 -> {
+18 −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;
@@ -539,10 +541,23 @@ public class NotesRepository {
    }

    @AnyThread
    public void toggleFavoriteAndSync(Account account, long noteId) {
    public void toggleFavoriteAndSync(Account account, Note note) {
        executor.submit(() -> {
            db.getNoteDao().toggleFavorite(noteId);
            scheduleSync(account, true);
            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) {
                        scheduleSync(account, false);
                    }
                }
            } catch (Exception e) {
                Log_OC.e(TAG, "toggleFavoriteAndSync: " + e);
            }
        });
    }

Loading