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.ViewCompat;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@ -677,7 +678,6 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A
@Override
public void onNoteFavoriteClick(int position, View view) {
NoteWithCategory note = (NoteWithCategory) adapter.getItem(position);
NotesDatabase db = NotesDatabase.getInstance(view.getContext());
db.toggleFavoriteAndSync(ssoAccount, note.getId());
adapter.notifyItemChanged(position);
}
@ -767,23 +767,12 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A
@Override
public void onAccountPicked(@NonNull Account account) {
List<Integer> selection = new ArrayList<>(adapter.getSelected());
adapter.deselect(0);
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");
for (Integer i : adapter.getSelected()) {
final LiveData<NoteWithCategory> moveLiveData = db.moveNoteToAnotherAccount(ssoAccount, (NoteWithCategory) adapter.getItem(i), account.getId());
moveLiveData.observe(this, (v) -> moveLiveData.removeObservers(this));
}
}
mActionMode.finish();
}
@Override
public void onCategoryChosen(String category) {
for (Integer i : new ArrayList<>(adapter.getSelected())) {

View file

@ -162,9 +162,7 @@ public abstract class NotesDatabase extends RoomDatabase {
@NonNull
@MainThread
public LiveData<NoteWithCategory> addNoteAndSync(SingleSignOnAccount ssoAccount, long accountId, NoteWithCategory note) {
NoteWithCategory entity = new NoteWithCategory();
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());
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());
final MutableLiveData<NoteWithCategory> ret = new MutableLiveData<>();
new Thread(() -> ret.postValue(addNote(accountId, entity))).start();
return map(ret, newNoteWithCategory -> {
@ -209,14 +207,10 @@ public abstract class NotesDatabase extends RoomDatabase {
}
@AnyThread
public void moveNoteToAnotherAccount(SingleSignOnAccount ssoAccount, NoteWithCategory note, long newAccountId) {
new Thread(() -> {
NoteWithCategory noteWithCategory = new NoteWithCategory(new Note(null, note.getModified(), note.getTitle(), note.getContent(), note.getFavorite(), null), note.getCategory());
addNoteAndSync(ssoAccount, newAccountId, noteWithCategory);
public LiveData<NoteWithCategory> moveNoteToAnotherAccount(SingleSignOnAccount ssoAccount, NoteWithCategory note, long newAccountId) {
NoteWithCategory noteWithCategory = new NoteWithCategory(new Note(null, note.getModified(), note.getTitle(), getNoteDao().getContent(note.getId()), note.getFavorite(), null), note.getCategory());
deleteNoteAndSync(ssoAccount, note.getId());
notifyWidgets();
serverSyncHelper.scheduleSync(ssoAccount, true);
}).start();
return addNoteAndSync(ssoAccount, newAccountId, noteWithCategory);
}
@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 " +
"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);
@Query("SELECT content FROM NOTE WHERE id = :id")
String getContent(Long id);
}