set up back press action for search view

This commit is contained in:
Daniel Gultsch 2023-03-12 21:15:13 +01:00
parent e3f5f6404b
commit e22fcab844
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -29,7 +29,9 @@ import im.conversations.android.ui.activity.SetupActivity;
import im.conversations.android.ui.adapter.ChatOverviewAdapter; import im.conversations.android.ui.adapter.ChatOverviewAdapter;
import im.conversations.android.ui.adapter.ChatOverviewComparator; import im.conversations.android.ui.adapter.ChatOverviewComparator;
import im.conversations.android.ui.model.OverviewViewModel; import im.conversations.android.ui.model.OverviewViewModel;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -46,6 +48,13 @@ public class OverviewFragment extends Fragment {
binding.drawerLayout.close(); binding.drawerLayout.close();
} }
}; };
final OnBackPressedCallback searchViewOnBackPressedCallback =
new OnBackPressedCallback(false) {
@Override
public void handleOnBackPressed() {
binding.searchView.hide();
}
};
private OverviewViewModel overviewViewModel; private OverviewViewModel overviewViewModel;
private ChatOverviewAdapter chatOverviewAdapter; private ChatOverviewAdapter chatOverviewAdapter;
@ -73,6 +82,11 @@ public class OverviewFragment extends Fragment {
} else { } else {
window.setStatusBarColor(SurfaceColors.SURFACE_0.getColor(activity)); window.setStatusBarColor(SurfaceColors.SURFACE_0.getColor(activity));
} }
searchViewOnBackPressedCallback.setEnabled(
Arrays.asList(
SearchView.TransitionState.SHOWN,
SearchView.TransitionState.SHOWING)
.contains(newState));
}); });
binding.navigationView.setNavigationItemSelectedListener(this::onNavigationItemSelected); binding.navigationView.setNavigationItemSelectedListener(this::onNavigationItemSelected);
if (this.overviewViewModel.getChatFilter() == null) { if (this.overviewViewModel.getChatFilter() == null) {
@ -95,6 +109,9 @@ public class OverviewFragment extends Fragment {
pagingData -> { pagingData -> {
chatOverviewAdapter.submitData(getLifecycle(), pagingData); chatOverviewAdapter.submitData(getLifecycle(), pagingData);
}); });
requireActivity()
.getOnBackPressedDispatcher()
.addCallback(getViewLifecycleOwner(), this.searchViewOnBackPressedCallback);
return binding.getRoot(); return binding.getRoot();
} }
@ -104,6 +121,7 @@ public class OverviewFragment extends Fragment {
// after rotation (or similar) the drawer layout might get opened in restoreInstanceState // 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 // therefor we need to check again if we need to enable the callback
this.drawerLayoutOnBackPressedCallback.setEnabled(this.binding.drawerLayout.isOpen()); this.drawerLayoutOnBackPressedCallback.setEnabled(this.binding.drawerLayout.isOpen());
this.searchViewOnBackPressedCallback.setEnabled(this.binding.searchView.isShowing());
} }
private void configureDrawerLayoutToCloseOnBackPress() { private void configureDrawerLayoutToCloseOnBackPress() {