From 49780a61b0571d19a687ec16bb696a16b890edfd Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 6 Jul 2021 06:40:50 +0900 Subject: [PATCH] Fix swipe to reply not working. Fixes austinhuang0131/barinsta#1570 --- .../DirectMessageThreadFragment.java | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java index 520dfae8..fe274d57 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java @@ -167,6 +167,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact private EmojiPickerInsetsAnimationCallback emojiPickerAnimationCallback; private boolean hasKbOpenedOnce; private boolean wasToggled; + private SwipeAndRestoreItemTouchHelperCallback touchHelperCallback; private final AppExecutors appExecutors = AppExecutors.INSTANCE; private final Animatable2Compat.AnimationCallback micToSendAnimationCallback = new Animatable2Compat.AnimationCallback() { @@ -613,20 +614,6 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact } }); binding.chats.addItemDecoration(headerItemDecoration); - final SwipeAndRestoreItemTouchHelperCallback touchHelperCallback = new SwipeAndRestoreItemTouchHelperCallback( - context, - (adapterPosition, viewHolder) -> { - if (itemsAdapter == null) return; - final DirectItemOrHeader directItemOrHeader = itemsAdapter.getList().get(adapterPosition); - if (directItemOrHeader.isHeader()) return; - viewModel.setReplyToItem(directItemOrHeader.item); - } - ); - final Integer inputMode = viewModel.getInputMode().getValue(); - if (inputMode != null && inputMode != 1) { - itemTouchHelper = new ItemTouchHelper(touchHelperCallback); - itemTouchHelper.attachToRecyclerView(binding.chats); - } } private void setObservers() { @@ -654,8 +641,11 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact inputModeLiveData = viewModel.getInputMode(); inputModeLiveData.observe(getViewLifecycleOwner(), inputMode -> { final Boolean isPending = viewModel.isPending().getValue(); - if (isPending != null && isPending) return; - if (inputMode == null || inputMode == 0) return; + if (isPending != null && isPending || inputMode == null) return; + if (inputMode != 1) { + setupTouchHelper(); + } + if (inputMode == 0) return; if (inputMode == 1) { hideInput(); } @@ -755,6 +745,22 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact }); } + private void setupTouchHelper() { + final Context context = getContext(); + if (context == null) return; + touchHelperCallback = new SwipeAndRestoreItemTouchHelperCallback( + context, + (adapterPosition, viewHolder) -> { + if (itemsAdapter == null) return; + final DirectItemOrHeader directItemOrHeader = itemsAdapter.getList().get(adapterPosition); + if (directItemOrHeader.isHeader()) return; + viewModel.setReplyToItem(directItemOrHeader.item); + } + ); + itemTouchHelper = new ItemTouchHelper(touchHelperCallback); + itemTouchHelper.attachToRecyclerView(binding.chats); + } + private void removeObservers() { pendingLiveData.removeObservers(getViewLifecycleOwner()); inputModeLiveData.removeObservers(getViewLifecycleOwner());