From ee86148209a8ebeadd3cc891da1d4c2b000973bd Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Sat, 29 Apr 2023 00:09:43 +0800 Subject: Fixed ItemTouchHelper conflict with Slidr. --- .../adapters/HistoryPostRecyclerViewAdapter.java | 35 +++++++++++++++------ .../adapters/PostRecyclerViewAdapter.java | 36 ++++++++++++++++------ .../fragments/HistoryPostFragment.java | 17 +++++++++- .../infinityforreddit/fragments/PostFragment.java | 19 ++++++++++-- 4 files changed, 86 insertions(+), 21 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java index 4b4a86a9..88e83e30 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java @@ -2227,6 +2227,19 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter { + if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { + if (mFragment.isRecyclerViewItemSwipeable(PostBaseViewHolder.this)) { + mActivity.unlockSwipeRightToGoBack(); + } + } else { + if (mFragment.isRecyclerViewItemSwipeable(PostBaseViewHolder.this)) { + mActivity.lockSwipeRightToGoBack(); + } + } + return false; + }); + userTextView.setOnClickListener(view -> { if (canStartActivity) { int position = getBindingAdapterPosition(); @@ -3241,15 +3254,6 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter { - swipeLocked = false; - return false; - }); - bottomConstraintLayout.setOnTouchListener((view, motionEvent) -> { - swipeLocked = false; - return false; - }); - noPreviewImageView.setOnClickListener(view -> { int position = getBindingAdapterPosition(); if (position < 0) { @@ -3572,6 +3576,19 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter { + if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { + if (mFragment.isRecyclerViewItemSwipeable(PostCompactBaseViewHolder.this)) { + mActivity.unlockSwipeRightToGoBack(); + } + } else { + if (mFragment.isRecyclerViewItemSwipeable(PostCompactBaseViewHolder.this)) { + mActivity.lockSwipeRightToGoBack(); + } + } + return false; + }); + nameTextView.setOnClickListener(view -> { int position = getBindingAdapterPosition(); if (position < 0) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index ee94325f..f3612e1e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -11,6 +11,7 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Handler; +import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; @@ -2332,6 +2333,19 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { + if (mFragment.isRecyclerViewItemSwipeable(PostBaseViewHolder.this)) { + mActivity.unlockSwipeRightToGoBack(); + } + } else { + if (mFragment.isRecyclerViewItemSwipeable(PostBaseViewHolder.this)) { + mActivity.lockSwipeRightToGoBack(); + } + } + return false; + }); + userTextView.setOnClickListener(view -> { if (!canStartActivity) { return; @@ -3379,15 +3393,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - swipeLocked = false; - return false; - }); - bottomConstraintLayout.setOnTouchListener((view, motionEvent) -> { - swipeLocked = false; - return false; - }); - noPreviewImageView.setOnClickListener(view -> { int position = getBindingAdapterPosition(); if (position < 0) { @@ -3714,6 +3719,19 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { + if (mFragment.isRecyclerViewItemSwipeable(PostCompactBaseViewHolder.this)) { + mActivity.unlockSwipeRightToGoBack(); + } + } else { + if (mFragment.isRecyclerViewItemSwipeable(PostCompactBaseViewHolder.this)) { + mActivity.lockSwipeRightToGoBack(); + } + } + return false; + }); + nameTextView.setOnClickListener(view -> { int position = getBindingAdapterPosition(); if (position < 0) { 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 e85e2b2a..bdb0b004 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java @@ -209,6 +209,7 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato private boolean isLazyModePaused = false; private boolean hasPost = false; private boolean rememberMutingOptionInPostFeed; + private boolean swipeActionEnabled; private Boolean masterMutingOption; private HistoryPostRecyclerViewAdapter mAdapter; private RecyclerView.SmoothScroller smoothScroller; @@ -563,6 +564,7 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato }); if (nColumns == 1 && mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_SWIPE_ACTION, false)) { + swipeActionEnabled = true; touchHelper.attachToRecyclerView(mPostRecyclerView); } mPostRecyclerView.setAdapter(mAdapter); @@ -1011,6 +1013,18 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } } + public boolean isRecyclerViewItemSwipeable(RecyclerView.ViewHolder viewHolder) { + if (swipeActionEnabled) { + if (viewHolder instanceof HistoryPostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) { + return !((HistoryPostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) viewHolder).isSwipeLocked(); + } + + return true; + } + + return false; + } + @Subscribe public void onPostUpdateEvent(PostUpdateEventToPostList event) { ItemSnapshotList posts = mAdapter.snapshot(); @@ -1199,7 +1213,8 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato @Subscribe public void onChangeEnableSwipeActionSwitchEvent(ChangeEnableSwipeActionSwitchEvent changeEnableSwipeActionSwitchEvent) { - if (touchHelper != null) { + if (getNColumns(getResources()) == 1 && touchHelper != null) { + swipeActionEnabled = changeEnableSwipeActionSwitchEvent.enableSwipeAction; if (changeEnableSwipeActionSwitchEvent.enableSwipeAction) { touchHelper.attachToRecyclerView(mPostRecyclerView); } else { 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 d41894a3..766eadf1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -16,6 +16,7 @@ import android.os.Build; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; +import android.util.Log; import android.view.HapticFeedbackConstants; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -233,6 +234,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private boolean hasPost = false; private boolean savePostFeedScrolledPosition; private boolean rememberMutingOptionInPostFeed; + private boolean swipeActionEnabled; private Boolean masterMutingOption; private PostRecyclerViewAdapter mAdapter; private RecyclerView.SmoothScroller smoothScroller; @@ -1064,7 +1066,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (((PostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) viewHolder).isSwipeLocked()) { return makeMovementFlags(0, 0); } - } int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; return makeMovementFlags(0, swipeFlags); @@ -1153,6 +1154,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); if (nColumns == 1 && mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_SWIPE_ACTION, false)) { + swipeActionEnabled = true; touchHelper.attachToRecyclerView(mPostRecyclerView); } mPostRecyclerView.setAdapter(mAdapter); @@ -1978,7 +1980,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Subscribe public void onChangeEnableSwipeActionSwitchEvent(ChangeEnableSwipeActionSwitchEvent changeEnableSwipeActionSwitchEvent) { - if (touchHelper != null) { + if (getNColumns(getResources()) == 1 && touchHelper != null) { + swipeActionEnabled = changeEnableSwipeActionSwitchEvent.enableSwipeAction; if (changeEnableSwipeActionSwitchEvent.enableSwipeAction) { touchHelper.attachToRecyclerView(mPostRecyclerView); } else { @@ -2219,6 +2222,18 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + public boolean isRecyclerViewItemSwipeable(RecyclerView.ViewHolder viewHolder) { + if (swipeActionEnabled) { + if (viewHolder instanceof PostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) { + return !((PostRecyclerViewAdapter.PostBaseGalleryTypeViewHolder) viewHolder).isSwipeLocked(); + } + + return true; + } + + return false; + } + private static abstract class LazyModeRunnable implements Runnable { private int currentPosition = -1; -- cgit v1.2.3