Skip to content

Commit

Permalink
Limit the translation distance of items inb ItemTouchHelper in PostFr…
Browse files Browse the repository at this point in the history
…agmentBase.
  • Loading branch information
Docile-Alligator committed Oct 22, 2024
1 parent b3eee45 commit 451a594
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
postFragmentId = System.currentTimeMillis() + new Random().nextInt(1000);
}

binding.recyclerViewHistoryPostFragment.setOnTouchListener((view, motionEvent) -> {
if (isInLazyMode) {
pauseLazyMode(true);
}
return false;
});

if (activity instanceof RecyclerViewContentScrollingInterface) {
binding.recyclerViewHistoryPostFragment.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -22,7 +20,6 @@
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.paging.LoadState;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
Expand Down Expand Up @@ -220,13 +217,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
readPostsList = new ReadPostsList(mRedditDataRoomDatabase.readPostDao(), activity.accountName,
getArguments().getBoolean(EXTRA_DISABLE_READ_POSTS, false));

binding.recyclerViewPostFragment.setOnTouchListener((view, motionEvent) -> {
if (isInLazyMode) {
pauseLazyMode(true);
}
return false;
});

if (activity instanceof RecyclerViewContentScrollingInterface) {
binding.recyclerViewPostFragment.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
Expand Down Expand Up @@ -825,105 +815,6 @@ public void delayTransition() {
}
}

touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
boolean exceedThreshold = false;

@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
if (!(viewHolder instanceof PostRecyclerViewAdapter.PostBaseViewHolder) &&
!(viewHolder instanceof PostRecyclerViewAdapter.PostCompactBaseViewHolder)) {
return makeMovementFlags(0, 0);
} else if (viewHolder instanceof PostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) {
if (((PostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) viewHolder).isSwipeLocked()) {
return makeMovementFlags(0, 0);
}
}
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(0, swipeFlags);
}

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

@Override
public boolean isItemViewSwipeEnabled() {
return true;
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if (touchHelper != null) {
exceedThreshold = false;
touchHelper.attachToRecyclerView(null);
touchHelper.attachToRecyclerView(binding.recyclerViewPostFragment);
}
}

@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

if (isCurrentlyActive) {
View itemView = viewHolder.itemView;
int horizontalOffset = (int) Utils.convertDpToPixel(16, activity);
if (dX > 0) {
if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) {
if (!exceedThreshold) {
exceedThreshold = true;
if (vibrateWhenActionTriggered) {
viewHolder.itemView.setHapticFeedbackEnabled(true);
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
}
}
backgroundSwipeRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeRight.setBounds(0, 0, 0, 0);
}

drawableSwipeRight.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableSwipeRight.getIntrinsicWidth(),
(itemView.getBottom() + itemView.getTop() - drawableSwipeRight.getIntrinsicHeight()) / 2,
itemView.getLeft() + ((int) dX) - horizontalOffset,
(itemView.getBottom() + itemView.getTop() + drawableSwipeRight.getIntrinsicHeight()) / 2);
backgroundSwipeRight.draw(c);
drawableSwipeRight.draw(c);
} else if (dX < 0) {
if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) {
if (!exceedThreshold) {
exceedThreshold = true;
if (vibrateWhenActionTriggered) {
viewHolder.itemView.setHapticFeedbackEnabled(true);
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
}
}
backgroundSwipeLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeLeft.setBounds(0, 0, 0, 0);
}
drawableSwipeLeft.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset,
(itemView.getBottom() + itemView.getTop() - drawableSwipeLeft.getIntrinsicHeight()) / 2,
itemView.getRight() + ((int) dX) + horizontalOffset + drawableSwipeLeft.getIntrinsicWidth(),
(itemView.getBottom() + itemView.getTop() + drawableSwipeLeft.getIntrinsicHeight()) / 2);
backgroundSwipeLeft.draw(c);
drawableSwipeLeft.draw(c);
}
} else {
if (exceedThreshold) {
mAdapter.onItemSwipe(viewHolder, dX > 0 ? ItemTouchHelper.END : ItemTouchHelper.START, swipeLeftAction, swipeRightAction);
exceedThreshold = false;
}
}
}

@Override
public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
return 1;
}
});

if (nColumns == 1 && mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_SWIPE_ACTION, false)) {
swipeActionEnabled = true;
touchHelper.attachToRecyclerView(binding.recyclerViewPostFragment);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ml.docilealligator.infinityforreddit.fragments;

import static androidx.recyclerview.widget.ItemTouchHelper.ACTION_STATE_IDLE;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
Expand All @@ -15,6 +17,7 @@
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
Expand Down Expand Up @@ -141,6 +144,7 @@ public abstract class PostFragmentBase extends Fragment {
protected int swipeLeftAction;
protected int swipeRightAction;
protected ItemTouchHelper touchHelper;
private boolean shouldSwipeBack;
protected final Map<String, String> subredditOrUserIcons = new HashMap<>();

public PostFragmentBase() {
Expand Down Expand Up @@ -217,7 +221,7 @@ public void onFinish() {

@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
return makeMovementFlags(0, calculateMovementFlags(recyclerView, viewHolder));
return makeMovementFlags(ACTION_STATE_IDLE, calculateMovementFlags(recyclerView, viewHolder));
}

@Override
Expand All @@ -231,23 +235,25 @@ public boolean isItemViewSwipeEnabled() {
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if (touchHelper != null) {
exceedThreshold = false;
touchHelper.attachToRecyclerView(null);
touchHelper.attachToRecyclerView(getPostRecyclerView());
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {}

@Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
if (shouldSwipeBack) {
shouldSwipeBack = false;
return 0;
}
return super.convertToAbsoluteDirection(flags, layoutDirection);
}

@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

if (isCurrentlyActive) {
View itemView = viewHolder.itemView;
int horizontalOffset = (int) Utils.convertDpToPixel(16, activity);
if (dX > 0) {
if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) {
dX = (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold;
if (!exceedThreshold) {
exceedThreshold = true;
if (vibrateWhenActionTriggered) {
Expand All @@ -269,6 +275,7 @@ public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @
drawableSwipeRight.draw(c);
} else if (dX < 0) {
if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) {
dX = -(viewHolder.itemView.getRight() - viewHolder.itemView.getLeft()) * swipeActionThreshold;
if (!exceedThreshold) {
exceedThreshold = true;
if (vibrateWhenActionTriggered) {
Expand All @@ -294,6 +301,7 @@ public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @
exceedThreshold = false;
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}

@Override
Expand All @@ -302,6 +310,14 @@ public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
}
});

getPostRecyclerView().setOnTouchListener((view, motionEvent) -> {
shouldSwipeBack = motionEvent.getAction() == MotionEvent.ACTION_CANCEL || motionEvent.getAction() == MotionEvent.ACTION_UP;
if (isInLazyMode) {
pauseLazyMode(true);
}
return false;
});

return super.onCreateView(inflater, container, savedInstanceState);
}

Expand Down

0 comments on commit 451a594

Please sign in to comment.