yuyakaido / CardStackView

📱Tinder like swipeable card view for Android
Apache License 2.0
2.37k stars 448 forks source link

I can't run the remove methods properly #271

Closed broscr closed 4 years ago

broscr commented 5 years ago

`public class FragmentHome extends Fragment implements CardStackListener {

private Context mContext;
private final String TAG = "FragmentHome";
private UserListAdapter userListAdapter;
private CardStackLayoutManager manager;
private CardStackView cardStackView;
private ImageButton undoButton, rejectButton, likedButton;
private SwipeAnimationSetting.Builder builderSwipeSetting;
private FirebaseAuth mAuth;
private List<User> userList;
private FirebaseUser mUser;
private FirebaseDatabase mDatabase;
private DatabaseReference myRef;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    mContext = getContext();
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    initialize(view);
    getAllUser();
    allOnclick();

    return view;
}

@Override
public void onCardSwiped(Direction direction) {
    //Card Swipe Direction (LEFT-OR RİGHT)
    if (direction == Direction.Left) {
        setUserLike("userRejectList");
    } else if (direction == Direction.Right) {
        setUserLike("userLikeList");
    }
}

@Override
public void onCardRewound() {
    //Card Rewound
}

@Override
public void onCardDragging(Direction direction, float ratio) {
    /*
    This method is called while the card is dragging.
     */
}

@Override
public void onCardCanceled() {
   /*
   This method is called when the card is dragged less than threshold.
    */
}

@Override
public void onCardAppeared(View view, int position) {
    /*
    This method is called when the card appeared.
     */
}

@Override
public void onCardDisappeared(View view, int position) {
    /*
    This method is called when the card disappeared.
    */

    Log.e(TAG, "CARD OUT");
    Log.e(TAG,"POS: " + position);
    removeLast(position);
}

private void initialize(View view) {
    cardStackView = view.findViewById(R.id.cardStackView);
    mAuth = FirebaseAuth.getInstance();
    mUser = mAuth.getCurrentUser();
    mDatabase = FirebaseDatabase.getInstance();
    myRef = mDatabase.getReference();
    undoButton = view.findViewById(R.id.undoBtn);
    rejectButton = view.findViewById(R.id.rejectBtn);
    likedButton = view.findViewById(R.id.likedBtn);

    userList = new ArrayList<User>();

}

private void swap() {
    List<User> oldList = userListAdapter.getSpots();
    List<User> newList = new ArrayList<User>() {{
        addAll(oldList);
        User first = remove(manager.getTopPosition());
        User last = remove(this.size() - 1);
        add(manager.getTopPosition(), last);
        add(first);
    }};

    SpotDiffCallback callback = new SpotDiffCallback(oldList, newList);
    DiffUtil.DiffResult result = DiffUtil.calculateDiff(callback);
    userListAdapter.setSpots(newList);
    result.dispatchUpdatesTo(userListAdapter);
}

private void removeFirst(final int size) {
    if (userListAdapter.getSpots().isEmpty()) {
        return;
    }

    List<User> oldList = userListAdapter.getSpots();
    List<User> newList = new ArrayList<User>() {{
        addAll(userListAdapter.getSpots());
        for (int i = 0; i < size; i++) {
            remove(manager.getTopPosition());
        }
    }};
    SpotDiffCallback callback = new SpotDiffCallback(oldList, newList);
    DiffUtil.DiffResult result = DiffUtil.calculateDiff(callback);
    userListAdapter.setSpots(newList);
    result.dispatchUpdatesTo(userListAdapter);
}

private void removeLast(final int size) {
    if (userListAdapter.getSpots().isEmpty()) {
        return;
    }

    List<User> oldList = userListAdapter.getSpots();
    List<User> newList = new ArrayList<User>() {{
        addAll(userListAdapter.getSpots());
        for (int i = 0; i < size; i++) {
            remove(size() - 1);
        }
    }};
    SpotDiffCallback callback = new SpotDiffCallback(oldList, newList);
    DiffUtil.DiffResult result = DiffUtil.calculateDiff(callback);
    userListAdapter.setSpots(newList);
    result.dispatchUpdatesTo(userListAdapter);
}

private void getAllUser() {

    myRef.child("Users").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    final User user = ds.getValue(User.class);
                    if (!user.getUserUid().equals(mUser.getUid())) {
                        myRef.child("Users").child(mUser.getUid()).child("userLikeList").child(user.getUserUid()).addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                if (!dataSnapshot.exists()) {
                                    myRef.child("Users").child(mUser.getUid()).child("userRejectList").child(user.getUserUid()).addListenerForSingleValueEvent(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                            if (!dataSnapshot.exists()) {
                                                userList.add(user);
                                                userListAdapter.notifyDataSetChanged();
                                            }
                                        }

                                        @Override
                                        public void onCancelled(@NonNull DatabaseError databaseError) {

                                        }
                                    });
                                }
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                            }
                        });
                    }
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    manager = new CardStackLayoutManager(mContext, this);
    manager.setStackFrom(StackFrom.Bottom);
    manager.setVisibleCount(3);
    manager.setTranslationInterval(8.0f);
    manager.setScaleInterval(0.95f);
    manager.setSwipeThreshold(0.3f);
    manager.setMaxDegree(20.0f);
    manager.setDirections(Direction.HORIZONTAL);
    manager.setCanScrollHorizontal(true);
    manager.setCanScrollVertical(true);
    manager.setSwipeableMethod(SwipeableMethod.AutomaticAndManual);
    manager.setOverlayInterpolator(new LinearInterpolator());

    userListAdapter = new UserListAdapter(mContext, userList);
    cardStackView.setLayoutManager(manager);
    cardStackView.setAdapter(userListAdapter);

}

private void allOnclick() {

    rejectButton.setOnClickListener(v -> {
        builderSwipeSetting = new SwipeAnimationSetting.Builder();
        builderSwipeSetting.setDirection(Direction.Left)
                .setDuration(Duration.Slow.duration)
                .setInterpolator(new DecelerateInterpolator())
                .build();
        manager.setSwipeAnimationSetting(builderSwipeSetting.build());
        cardStackView.swipe();
        likedOrReject("userRejectList");
    });

    likedButton.setOnClickListener(v -> {

        builderSwipeSetting = new SwipeAnimationSetting.Builder();
        builderSwipeSetting.setDirection(Direction.Right)
                .setDuration(Duration.Slow.duration)
                .setInterpolator(new DecelerateInterpolator())
                .build();
        manager.setSwipeAnimationSetting(builderSwipeSetting.build());
        cardStackView.swipe();
        likedOrReject("userLikeList");
    });
}

private void setUserLike(String string) {
    try {
        final User getUser = userList.get(manager.getTopPosition() - 1);
        myRef.child("Users").child(mUser.getUid()).child(string)
                .child(getUser.getUserUid()).setValue(getUser);
    } catch (IndexOutOfBoundsException e) {
        Log.e(TAG, e.getLocalizedMessage());
        AllStatics.toastGet(mContext, getString(R.string.user_list_zero));
    }

}

private void likedOrReject(String string) {

    final User getUSer1 = userList.get(manager.getTopPosition());
    myRef.child("Users").child(mUser.getUid()).child(string)
            .child(getUSer1.getUserUid()).setValue(getUSer1);

}

}`

yuyakaido commented 4 years ago

@ridone6 Sorry, I can't understand your situation. Can you inform me about expected behavior and actual behavior with your development environment and library version?