diff options
author | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-10-22 19:34:08 +0000 |
---|---|---|
committer | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-10-22 19:34:08 +0000 |
commit | 451a5947fd318f01eee53960c2249bc10d205c33 (patch) | |
tree | 039806958b8d1fdc845a9d779bd6c1be5ff37397 /app/src | |
parent | b3eee45358ff61271f5eb1e05ec37df266f5103e (diff) | |
download | infinity-for-reddit-451a5947fd318f01eee53960c2249bc10d205c33.tar infinity-for-reddit-451a5947fd318f01eee53960c2249bc10d205c33.tar.gz infinity-for-reddit-451a5947fd318f01eee53960c2249bc10d205c33.tar.bz2 infinity-for-reddit-451a5947fd318f01eee53960c2249bc10d205c33.tar.lz infinity-for-reddit-451a5947fd318f01eee53960c2249bc10d205c33.tar.xz infinity-for-reddit-451a5947fd318f01eee53960c2249bc10d205c33.tar.zst infinity-for-reddit-451a5947fd318f01eee53960c2249bc10d205c33.zip |
Limit the translation distance of items inb ItemTouchHelper in PostFragmentBase.
Diffstat (limited to 'app/src')
3 files changed, 24 insertions, 124 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java index fd21e73a..c7f1b284 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java @@ -169,13 +169,6 @@ public class HistoryPostFragment extends PostFragmentBase implements FragmentCom 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 diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index a3d0dab9..8acbc099 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -6,11 +6,9 @@ import static ml.docilealligator.infinityforreddit.videoautoplay.media.PlaybackI 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; @@ -22,7 +20,6 @@ import androidx.annotation.NonNull; 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; @@ -220,13 +217,6 @@ public class PostFragment extends PostFragmentBase implements FragmentCommunicat 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 @@ -825,105 +815,6 @@ public class PostFragment extends PostFragmentBase implements FragmentCommunicat } } - 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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragmentBase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragmentBase.java index 6d8a8e00..7a7c2a67 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragmentBase.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragmentBase.java @@ -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; @@ -15,6 +17,7 @@ import android.view.HapticFeedbackConstants; 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; @@ -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() { @@ -217,7 +221,7 @@ public abstract class PostFragmentBase extends Fragment { @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 @@ -231,23 +235,25 @@ public abstract class PostFragmentBase extends Fragment { } @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) { @@ -269,6 +275,7 @@ public abstract class PostFragmentBase extends Fragment { 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) { @@ -294,6 +301,7 @@ public abstract class PostFragmentBase extends Fragment { exceedThreshold = false; } } + super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } @Override @@ -302,6 +310,14 @@ public abstract class PostFragmentBase extends Fragment { } }); + 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); } |