How to make sure that not everyone can use the ItemTouchHelper?

Aaron DCosta :

In my app everyone can add Notes that are displayed in a RecyclerView.

I have added the swipe to delete functionality into the RecyclerView, by creating an ItemTouchHelper with a SimpleCallback and attaching it to the RecyclerView with the attachToRecyclerView method. In the onSwiped callback, I am deleting the corresponding document from the FirestoreDatabase, by calling delete on it’s DocumentReference. This DocumentReference comes from the DocumentSnapshot, by calling getSnapshots().getSnapshot().getReference() in the adapter and passing the position to it.

My issue: I want the user to be able to only delete the note that he has added and not any other note that was added by some other user. What is the logic to implement this?

NoteAdapter.java (adapterclass)

public void deleteItem(int position){
    getSnapshots().getSnapshot(position).getReference().delete();
}

Receive.java (mainactivity)

new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
            ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT) {
        @Override
        public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
            Note note = new Note();
            String cuid = FirebaseAuth.getInstance().getCurrentUser().getUid();
                if (note.getUserId().equals(cuid)) {
                    //do nothing;
                } else {
                    return 0;
                }
                return super.getMovementFlags(recyclerView, viewHolder);
        }


        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            adapter.deleteItem(viewHolder.getAdapterPosition());
        }
    }).attachToRecyclerView(recyclerView);
}
Aaron DCosta :

I finally was able to get the solution after going through this and with the help of the answer provided by theThapa.

Since his answer was in kotlin and I am developing my app in Java, I had to play around my getMovementFlag() to make it something similar to the one he provided and do the following changes insideItemTouchHelper.SimpleCallback

@Override
            public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                    if (adapter.getSnapshots().get(viewHolder.getAdapterPosition()).getUserId().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
                        //do nothing;
                    }
                    else {
                        return 0;
                    }
                    return super.getMovementFlags(recyclerView, viewHolder);
            }

Once again my sincere thanks to theThapa for helping me with this.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=21796&siteId=1