From e22fcab8447a5af150299261a272c6dfd55744e8 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 12 Mar 2023 21:15:13 +0100 Subject: [PATCH] set up back press action for search view --- .../ui/fragment/main/OverviewFragment.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/src/main/java/im/conversations/android/ui/fragment/main/OverviewFragment.java b/app/src/main/java/im/conversations/android/ui/fragment/main/OverviewFragment.java index 46347ff01..26844b74b 100644 --- a/app/src/main/java/im/conversations/android/ui/fragment/main/OverviewFragment.java +++ b/app/src/main/java/im/conversations/android/ui/fragment/main/OverviewFragment.java @@ -29,7 +29,9 @@ import im.conversations.android.ui.activity.SetupActivity; import im.conversations.android.ui.adapter.ChatOverviewAdapter; import im.conversations.android.ui.adapter.ChatOverviewComparator; import im.conversations.android.ui.model.OverviewViewModel; +import java.util.Arrays; import java.util.List; +import java.util.Objects; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -46,6 +48,13 @@ public class OverviewFragment extends Fragment { binding.drawerLayout.close(); } }; + final OnBackPressedCallback searchViewOnBackPressedCallback = + new OnBackPressedCallback(false) { + @Override + public void handleOnBackPressed() { + binding.searchView.hide(); + } + }; private OverviewViewModel overviewViewModel; private ChatOverviewAdapter chatOverviewAdapter; @@ -73,6 +82,11 @@ public class OverviewFragment extends Fragment { } else { window.setStatusBarColor(SurfaceColors.SURFACE_0.getColor(activity)); } + searchViewOnBackPressedCallback.setEnabled( + Arrays.asList( + SearchView.TransitionState.SHOWN, + SearchView.TransitionState.SHOWING) + .contains(newState)); }); binding.navigationView.setNavigationItemSelectedListener(this::onNavigationItemSelected); if (this.overviewViewModel.getChatFilter() == null) { @@ -95,6 +109,9 @@ public class OverviewFragment extends Fragment { pagingData -> { chatOverviewAdapter.submitData(getLifecycle(), pagingData); }); + requireActivity() + .getOnBackPressedDispatcher() + .addCallback(getViewLifecycleOwner(), this.searchViewOnBackPressedCallback); return binding.getRoot(); } @@ -104,6 +121,7 @@ public class OverviewFragment extends Fragment { // after rotation (or similar) the drawer layout might get opened in restoreInstanceState // therefor we need to check again if we need to enable the callback this.drawerLayoutOnBackPressedCallback.setEnabled(this.binding.drawerLayout.isOpen()); + this.searchViewOnBackPressedCallback.setEnabled(this.binding.searchView.isShowing()); } private void configureDrawerLayoutToCloseOnBackPress() {