This commit is contained in:
Stefan Niedermann 2020-10-16 11:37:13 +02:00
parent 174512b4e8
commit 3637327904
3 changed files with 12 additions and 26 deletions

View file

@ -26,6 +26,7 @@ import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.GravityCompat; import androidx.core.view.GravityCompat;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -677,7 +678,6 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A
@Override @Override
public void onNoteFavoriteClick(int position, View view) { public void onNoteFavoriteClick(int position, View view) {
NoteWithCategory note = (NoteWithCategory) adapter.getItem(position); NoteWithCategory note = (NoteWithCategory) adapter.getItem(position);
NotesDatabase db = NotesDatabase.getInstance(view.getContext());
db.toggleFavoriteAndSync(ssoAccount, note.getId()); db.toggleFavoriteAndSync(ssoAccount, note.getId());
adapter.notifyItemChanged(position); adapter.notifyItemChanged(position);
} }
@ -767,23 +767,12 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A
@Override @Override
public void onAccountPicked(@NonNull Account account) { public void onAccountPicked(@NonNull Account account) {
List<Integer> selection = new ArrayList<>(adapter.getSelected()); for (Integer i : adapter.getSelected()) {
final LiveData<NoteWithCategory> moveLiveData = db.moveNoteToAnotherAccount(ssoAccount, (NoteWithCategory) adapter.getItem(i), account.getId());
adapter.deselect(0); moveLiveData.observe(this, (v) -> moveLiveData.removeObservers(this));
for (Integer i : selection) {
NoteWithCategory note = (NoteWithCategory) adapter.getItem(i);
db.moveNoteToAnotherAccount(ssoAccount, db.getNoteDao().getNoteWithCategory(note.getAccountId(), note.getId()), account.getId());
RecyclerView.ViewHolder viewHolder = listView.findViewHolderForAdapterPosition(i);
if (viewHolder != null) {
viewHolder.itemView.setSelected(false);
} else {
Log.w(TAG, "Could not found viewholder to remove selection");
} }
} }
mActionMode.finish();
}
@Override @Override
public void onCategoryChosen(String category) { public void onCategoryChosen(String category) {
for (Integer i : new ArrayList<>(adapter.getSelected())) { for (Integer i : new ArrayList<>(adapter.getSelected())) {

View file

@ -162,9 +162,7 @@ public abstract class NotesDatabase extends RoomDatabase {
@NonNull @NonNull
@MainThread @MainThread
public LiveData<NoteWithCategory> addNoteAndSync(SingleSignOnAccount ssoAccount, long accountId, NoteWithCategory note) { public LiveData<NoteWithCategory> addNoteAndSync(SingleSignOnAccount ssoAccount, long accountId, NoteWithCategory note) {
NoteWithCategory entity = new NoteWithCategory(); NoteWithCategory entity = new NoteWithCategory(new Note(0, null, note.getModified(), note.getTitle(), note.getContent(), note.getFavorite(), note.getETag(), DBStatus.LOCAL_EDITED, accountId, generateNoteExcerpt(note.getContent(), note.getTitle()), 0), note.getCategory());
entity.setNote(new Note(0, null, note.getModified(), note.getTitle(), note.getContent(), note.getFavorite(), note.getETag(), DBStatus.LOCAL_EDITED, accountId, generateNoteExcerpt(note.getContent(), note.getTitle()), 0));
entity.setCategory(note.getCategory());
final MutableLiveData<NoteWithCategory> ret = new MutableLiveData<>(); final MutableLiveData<NoteWithCategory> ret = new MutableLiveData<>();
new Thread(() -> ret.postValue(addNote(accountId, entity))).start(); new Thread(() -> ret.postValue(addNote(accountId, entity))).start();
return map(ret, newNoteWithCategory -> { return map(ret, newNoteWithCategory -> {
@ -209,14 +207,10 @@ public abstract class NotesDatabase extends RoomDatabase {
} }
@AnyThread @AnyThread
public void moveNoteToAnotherAccount(SingleSignOnAccount ssoAccount, NoteWithCategory note, long newAccountId) { public LiveData<NoteWithCategory> moveNoteToAnotherAccount(SingleSignOnAccount ssoAccount, NoteWithCategory note, long newAccountId) {
new Thread(() -> { NoteWithCategory noteWithCategory = new NoteWithCategory(new Note(null, note.getModified(), note.getTitle(), getNoteDao().getContent(note.getId()), note.getFavorite(), null), note.getCategory());
NoteWithCategory noteWithCategory = new NoteWithCategory(new Note(null, note.getModified(), note.getTitle(), note.getContent(), note.getFavorite(), null), note.getCategory());
addNoteAndSync(ssoAccount, newAccountId, noteWithCategory);
deleteNoteAndSync(ssoAccount, note.getId()); deleteNoteAndSync(ssoAccount, note.getId());
notifyWidgets(); return addNoteAndSync(ssoAccount, newAccountId, noteWithCategory);
serverSyncHelper.scheduleSync(ssoAccount, true);
}).start();
} }
@NonNull @NonNull

View file

@ -140,4 +140,7 @@ public interface NoteDao {
@Query("UPDATE NOTE SET id = :id, title = :title, modified = :modified, title = :title, favorite = :favorite, etag = :eTag, content = :content " + @Query("UPDATE NOTE SET id = :id, title = :title, modified = :modified, title = :title, favorite = :favorite, etag = :eTag, content = :content " +
"WHERE id = :id AND status = '' AND (modified != :modified OR favorite != :favorite OR categoryId != :categoryTitle OR (eTag == NULL OR eTag != :eTag) OR content != :content)") "WHERE id = :id AND status = '' AND (modified != :modified OR favorite != :favorite OR categoryId != :categoryTitle OR (eTag == NULL OR eTag != :eTag) OR content != :content)")
void updateIfNotModifiedLocallyAndRemoteColumnHasChanged(long id, long modified, String title, Boolean favorite, String categoryTitle, String eTag, String content); void updateIfNotModifiedLocallyAndRemoteColumnHasChanged(long id, long modified, String title, Boolean favorite, String categoryTitle, String eTag, String content);
@Query("SELECT content FROM NOTE WHERE id = :id")
String getContent(Long id);
} }