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

Unverified Commit 2a542dbd authored by Niedermann IT-Dienstleistungen's avatar Niedermann IT-Dienstleistungen Committed by GitHub
Browse files

Fix moving note to another account (#1235)

- Moving from within a note using the 3-dots-menu did not work at all because no account was passed to the chooser
- Moving a note does no longer require to re-read the full content. This is already done by `MainViewModel`. The method expects the full note to be passed
parent 695dbf28
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import androidx.core.content.ContextCompat;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LiveData;

import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
@@ -240,7 +241,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
        } else if (itemId == R.id.menu_move) {
            new Thread(() -> {
                AccountPickerDialogFragment
                        .newInstance(new ArrayList<>(), note.getAccountId())
                        .newInstance(new ArrayList<>(repo.getAccounts()), note.getAccountId())
                        .show(requireActivity().getSupportFragmentManager(), BaseNoteFragment.class.getSimpleName());
            }).start();
            return true;
@@ -372,7 +373,8 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
    }

    public void moveNote(Account account) {
        repo.moveNoteToAnotherAccount(account, note);
        final LiveData<Note> moveLiveData = repo.moveNoteToAnotherAccount(account, note);
        moveLiveData.observe(this, (v) -> moveLiveData.removeObservers(this));
        listener.close();
    }

+1 −1
Original line number Diff line number Diff line
@@ -488,7 +488,7 @@ public class MainViewModel extends AndroidViewModel {
        });
    }

    public LiveData<Note> moveNoteToAnotherAccount(Account account, Long noteId) {
    public LiveData<Note> moveNoteToAnotherAccount(Account account, long noteId) {
        return switchMap(repo.getNoteById$(noteId), (note) -> {
            Log.v(TAG, "[moveNoteToAnotherAccount] - note: " + note);
            return repo.moveNoteToAnotherAccount(account, note);
+6 −5
Original line number Diff line number Diff line
@@ -67,7 +67,6 @@ import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.O;
import static androidx.lifecycle.Transformations.distinctUntilChanged;
import static androidx.lifecycle.Transformations.map;
import static androidx.lifecycle.Transformations.switchMap;
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT;
import static it.niedermann.owncloud.notes.shared.util.NoteUtil.generateNoteExcerpt;
import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets;
@@ -402,10 +401,12 @@ public class NotesRepository {

    @MainThread
    public LiveData<Note> moveNoteToAnotherAccount(Account account, @NonNull Note note) {
        return switchMap(db.getNoteDao().getContent$(note.getId()), (content) -> {
            final Note fullNote = new Note(null, note.getModified(), note.getTitle(), content, note.getCategory(), note.getFavorite(), null);
        final Note fullNote = new Note(null, note.getModified(), note.getTitle(), note.getContent(), note.getCategory(), note.getFavorite(), null);
        deleteNoteAndSync(account, note.getId());
            return addNoteAndSync(account, fullNote);
        return map(addNoteAndSync(account, fullNote), (createdNote) -> {
            db.getNoteDao().updateStatus(createdNote.getId(), DBStatus.LOCAL_EDITED);
            createdNote.setStatus(DBStatus.LOCAL_EDITED);
            return createdNote;
        });
    }

+0 −10
Original line number Diff line number Diff line
@@ -29,11 +29,7 @@ public interface NoteDao {
    @Update(onConflict = OnConflictStrategy.REPLACE)
    int updateNote(Note newNote);

    @Query("DELETE FROM NOTE WHERE accountId = :accountId")
    int deleteByAccountId(Long accountId);

    String getNoteById = "SELECT * FROM NOTE WHERE id = :id";
    String getContent = "SELECT content FROM NOTE WHERE id = :id";
    String count = "SELECT COUNT(*) FROM NOTE WHERE status != 'LOCAL_DELETED' AND accountId = :accountId";
    String countFavorites = "SELECT COUNT(*) FROM NOTE WHERE status != 'LOCAL_DELETED' AND accountId = :accountId AND favorite = 1";
    String searchRecentByModified = "SELECT id, remoteId, accountId, title, favorite, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) ORDER BY favorite DESC, modified DESC";
@@ -66,12 +62,6 @@ public interface NoteDao {
    @Query(countFavorites)
    Integer countFavorites(long accountId);

    @Query(getContent)
    LiveData<String> getContent$(Long id);

    @Query(getContent)
    String getContent(Long id);

    @Query(searchRecentByModified)
    LiveData<List<Note>> searchRecentByModified$(long accountId, String query);

+0 −10
Original line number Diff line number Diff line
@@ -336,16 +336,6 @@ public class NotesDaoTest {
                localNote.getId(), localNote.getModified().getTimeInMillis(), localNote.getTitle(), localNote.getFavorite(), localNote.getCategory(), localNote.getETag(), localNote.getContent() + " ", localNote.getExcerpt()));
    }

    @Test
    public void getContent() throws InterruptedException {
        final Note note = new Note(1, 1L, Calendar.getInstance(), "My-Title", "My-Content", "", false, "1", LOCAL_DELETED, account.getId(), "", 0);
        db.getNoteDao().addNote(note);
        assertEquals("My-Content", db.getNoteDao().getContent(note.getId()));
        assertEquals("My-Content", NotesTestingUtil.getOrAwaitValue(db.getNoteDao().getContent$(note.getId())));
        assertNull(db.getNoteDao().getContent(note.getId() + 1));
        assertNull(NotesTestingUtil.getOrAwaitValue(db.getNoteDao().getContent$(note.getId() + 1)));
    }

    @Test
    public void getCategoriesLiveData() throws InterruptedException {
        final Account secondAccount = setupSecondAccountAndTestNotes();
Loading