I try to persist the position of items of a todo list app after I vertically draged & droped the todo items. I use a RecyclerView/FirestoreRecylerAdapter/ItemTouchHelper/Firestore. In the onMove()-Method of the ItemTouchHelper.Callback I access Firestore I guess with asyncronious calls. This leads to unwanted content changes in my RecyclerView. Imagine I have a list of 4 items with Titles A, B, C, D. Now I drag the Item with title A from position 0 (at the top) to position 1. Like this item B's title also changes to A. But this is only on the UI and not in the DB. When I change to another activity and come back then the titles are correct again. I guess it has todo with the asyncronious calls.
//part of MainActivity onCreate()
mFirestore = FirebaseFirestore.getInstance();
FirebaseFirestore.setLoggingEnabled(true);
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(true)
.build();
mFirestore.setFirestoreSettings(settings);
mRecyclerView = findViewById(R.id.recycler_view);
mUser = FirebaseAuth.getInstance().getCurrentUser();
if(mUser == null){
startSignIn();
}else{
Query mQuery = mFirestore.collection("todos").whereEqualTo("author", mUser.getUid()).whereEqualTo("done", false).orderBy("position", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Todo> options = new FirestoreRecyclerOptions.Builder<Todo>()
.setQuery(mQuery, Todo.class)
.build();
mAdapter = new TodoAdapter(options);
initRecyclerView();
//ItemTouchHelper.Callback method as part of initRecyclerView()
@Override
public boolean onMove(final RecyclerView recyclerView, final RecyclerView.ViewHolder source, final RecyclerView.ViewHolder target) {
if (source.getItemViewType() != target.getItemViewType()) {
return false;
}
int fromPos = source.getAdapterPosition();
int toPos = target.getAdapterPosition();
//Change items position when an item was draged & droped vertically downwards
if(toPos > fromPos) {
DocumentReference docTo = mAdapter.getSnapshots().getSnapshot(fromPos).getReference();
docTo.update("position", toPos);
int current = fromPos+1;
while(current <= toPos){
DocumentReference docCurrent = mAdapter.getSnapshots().getSnapshot(current).getReference();
docCurrent.update("position", current-1);
current ++;
}
//Change items position when an item was draged & droped vertically upwards
}else if (toPos < fromPos){
//TODO implement
} else {
return false;
}
mAdapter.notifyItemMoved(fromPos, toPos);
return true;
}
//adapter class
public class TodoAdapter extends FirestoreRecyclerAdapter<Todo, TodoAdapter.TodoHolder> {
private static final String TAG = "DOIT_DEBUG: ";
private OnItemClickListener listener;
private View viewItem;
public TodoAdapter(@NonNull FirestoreRecyclerOptions<Todo> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull TodoHolder holder, int position, @NonNull Todo model) {
holder.textViewTitle.setText(model.getTitle());
holder.textViewDescription.setText(model.getDescription());
}
@NonNull
@Override
public TodoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
viewItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_todo,
parent, false);
return new TodoHolder(viewItem);
}
public void deleteItem(final int position) {
getSnapshots().getSnapshot(position).getReference().delete();
}
public void setDoneItem(int position){
getSnapshots().getSnapshot(position).getReference().update("done", true);
viewItem.setEnabled(false);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
public interface OnItemClickListener{
void onItemClick (DocumentSnapshot documentSnapshot, int position);
}
public class TodoHolder extends RecyclerView.ViewHolder {
private TextView textViewTitle;
private TextView textViewDescription;
private String documentID;
public TodoHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.todo_title);
textViewDescription = itemView.findViewById(R.id.todo_description);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION && listener != null){
listener.onItemClick(getSnapshots().getSnapshot(position), position);
}
}
});
}
}
//model class
public class Todo {
private String title;
private String description;
private String author;
private boolean done;
private int position;
private Date created;
public Todo(){} // Needed for Firebase
public Todo(String title, String description, String author, boolean done, int position, Date created) {
this.title = title;
this.description = description;
this.author = author;
this.done = done;
this.position = position;
this.created = created;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getAuthor() {
return author;
}
public boolean getDone() {
return done;
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setAuthor(String author) {
this.author = author;
}
public void setDone(boolean done) {
this.done = done;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
@ServerTimestamp
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
//DB structure
Collection "todos" Document fields: author string, created timestamp, description string, done boolean, position number, title string
I expect the drag & drop position change to be persistet in Firestore and that the RecyclerView only displays the correct move of the items and no other changes.