Skip to content

Commit

Permalink
[Catalog][Predictive Back][Bottom Sheet] Update bottom sheet state ha…
Browse files Browse the repository at this point in the history
…ndling to be more accurate in demos

Fixes inaccurate state label text and back callback enabled flag after rotation

Resolves #3573
Resolves #3575
Resolves #3577

Resolves #3574
Resolves #3576
Resolves #3578

PiperOrigin-RevId: 566286622
(cherry picked from commit f9102c7)
  • Loading branch information
dsn5ft authored and afohrman committed Sep 21, 2023
1 parent bcc2f91 commit 386d47b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,25 +208,12 @@ protected void setUpBottomDrawer(View view) {
bottomDrawerBehavior = BottomSheetBehavior.from(bottomDrawer);
bottomDrawerBehavior.setUpdateImportantForAccessibilityOnSiblings(true);
bottomDrawerBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
bottomDrawer.post(() -> updateBackHandlingEnabled(bottomDrawerBehavior.getState()));
bottomDrawerBehavior.addBottomSheetCallback(
new BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_EXPANDED:
case BottomSheetBehavior.STATE_HALF_EXPANDED:
bottomDrawerOnBackPressedCallback.setEnabled(true);
break;
case BottomSheetBehavior.STATE_COLLAPSED:
case BottomSheetBehavior.STATE_HIDDEN:
bottomDrawerOnBackPressedCallback.setEnabled(false);
break;
case BottomSheetBehavior.STATE_DRAGGING:
case BottomSheetBehavior.STATE_SETTLING:
default:
// Do nothing, only change callback enabled for "stable" states.
break;
}
updateBackHandlingEnabled(newState);

if (newState == BottomSheetBehavior.STATE_HIDDEN) {
barNavView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Expand All @@ -245,6 +232,24 @@ public void onSlide(@NonNull View bottomSheet, float slideOffset) {}
v -> bottomDrawerBehavior.setState(BottomSheetBehavior.STATE_HALF_EXPANDED));
}

private void updateBackHandlingEnabled(int state) {
switch (state) {
case BottomSheetBehavior.STATE_EXPANDED:
case BottomSheetBehavior.STATE_HALF_EXPANDED:
bottomDrawerOnBackPressedCallback.setEnabled(true);
break;
case BottomSheetBehavior.STATE_COLLAPSED:
case BottomSheetBehavior.STATE_HIDDEN:
bottomDrawerOnBackPressedCallback.setEnabled(false);
break;
case BottomSheetBehavior.STATE_DRAGGING:
case BottomSheetBehavior.STATE_SETTLING:
default:
// Do nothing, only change callback enabled for "stable" states.
break;
}
}

private void showSnackbar(CharSequence text) {
Snackbar.make(coordinatorLayout, text, Snackbar.LENGTH_SHORT)
.setAnchorView(fab.getVisibility() == View.VISIBLE ? fab : bar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ public View onCreateDemoView(
persistentBottomSheetBehavior = BottomSheetBehavior.from(bottomSheetPersistent);
persistentBottomSheetBehavior.addBottomSheetCallback(
createBottomSheetCallback(bottomSheetText));
bottomSheetPersistent.post(
() -> {
int state = persistentBottomSheetBehavior.getState();
updateStateTextView(bottomSheetPersistent, bottomSheetText, state);
updateBackHandlingEnabled(state);
});
setupBackHandling(persistentBottomSheetBehavior);

Button button1 = view.findViewById(R.id.cat_bottomsheet_button);
Expand Down Expand Up @@ -251,39 +257,39 @@ protected int getStandardBottomSheetLayout() {
}

private BottomSheetCallback createBottomSheetCallback(@NonNull TextView text) {
// Set up BottomSheetCallback
BottomSheetCallback bottomSheetCallback =
new BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
return new BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
updateStateTextView(bottomSheet, text, newState);
}

switch (newState) {
case BottomSheetBehavior.STATE_DRAGGING:
text.setText(R.string.cat_bottomsheet_state_dragging);
break;
case BottomSheetBehavior.STATE_EXPANDED:
text.setText(R.string.cat_bottomsheet_state_expanded);
break;
case BottomSheetBehavior.STATE_COLLAPSED:
text.setText(R.string.cat_bottomsheet_state_collapsed);
break;
case BottomSheetBehavior.STATE_HALF_EXPANDED:
BottomSheetBehavior<View> bottomSheetBehavior =
BottomSheetBehavior.from(bottomSheet);
text.setText(
getString(
R.string.cat_bottomsheet_state_half_expanded,
bottomSheetBehavior.getHalfExpandedRatio()));
break;
default:
break;
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {}
};
}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {}
};
return bottomSheetCallback;
private void updateStateTextView(@NonNull View bottomSheet, @NonNull TextView text, int state) {
switch (state) {
case BottomSheetBehavior.STATE_DRAGGING:
text.setText(R.string.cat_bottomsheet_state_dragging);
break;
case BottomSheetBehavior.STATE_EXPANDED:
text.setText(R.string.cat_bottomsheet_state_expanded);
break;
case BottomSheetBehavior.STATE_COLLAPSED:
text.setText(R.string.cat_bottomsheet_state_collapsed);
break;
case BottomSheetBehavior.STATE_HALF_EXPANDED:
BottomSheetBehavior<View> bottomSheetBehavior =
BottomSheetBehavior.from(bottomSheet);
text.setText(
getString(
R.string.cat_bottomsheet_state_half_expanded,
bottomSheetBehavior.getHalfExpandedRatio()));
break;
default:
break;
}
}

private void setupBackHandling(BottomSheetBehavior<View> behavior) {
Expand All @@ -294,25 +300,29 @@ private void setupBackHandling(BottomSheetBehavior<View> behavior) {
new BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_EXPANDED:
case BottomSheetBehavior.STATE_HALF_EXPANDED:
persistentBottomSheetBackCallback.setEnabled(true);
break;
case BottomSheetBehavior.STATE_COLLAPSED:
case BottomSheetBehavior.STATE_HIDDEN:
persistentBottomSheetBackCallback.setEnabled(false);
break;
case BottomSheetBehavior.STATE_DRAGGING:
case BottomSheetBehavior.STATE_SETTLING:
default:
// Do nothing, only change callback enabled for "stable" states.
break;
}
updateBackHandlingEnabled(newState);
}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {}
});
}

private void updateBackHandlingEnabled(int state) {
switch (state) {
case BottomSheetBehavior.STATE_EXPANDED:
case BottomSheetBehavior.STATE_HALF_EXPANDED:
persistentBottomSheetBackCallback.setEnabled(true);
break;
case BottomSheetBehavior.STATE_COLLAPSED:
case BottomSheetBehavior.STATE_HIDDEN:
persistentBottomSheetBackCallback.setEnabled(false);
break;
case BottomSheetBehavior.STATE_DRAGGING:
case BottomSheetBehavior.STATE_SETTLING:
default:
// Do nothing, only change callback enabled for "stable" states.
break;
}
}
}

0 comments on commit 386d47b

Please sign in to comment.