From 386d47b51b31d8068f1fc6617414ca1a92c3353b Mon Sep 17 00:00:00 2001 From: dsn5ft <1420597+dsn5ft@users.noreply.github.com> Date: Mon, 18 Sep 2023 13:45:06 +0000 Subject: [PATCH] [Catalog][Predictive Back][Bottom Sheet] Update bottom sheet state handling to be more accurate in demos Fixes inaccurate state label text and back callback enabled flag after rotation Resolves https://github.com/material-components/material-components-android/issues/3573 Resolves https://github.com/material-components/material-components-android/issues/3575 Resolves https://github.com/material-components/material-components-android/issues/3577 Resolves https://github.com/material-components/material-components-android/pull/3574 Resolves https://github.com/material-components/material-components-android/pull/3576 Resolves https://github.com/material-components/material-components-android/pull/3578 PiperOrigin-RevId: 566286622 (cherry picked from commit f9102c745ad8b84dc16a4914665f2bb40aa4f5a2) --- .../BottomAppBarMainDemoFragment.java | 35 +++--- .../BottomSheetMainDemoFragment.java | 102 ++++++++++-------- 2 files changed, 76 insertions(+), 61 deletions(-) diff --git a/catalog/java/io/material/catalog/bottomappbar/BottomAppBarMainDemoFragment.java b/catalog/java/io/material/catalog/bottomappbar/BottomAppBarMainDemoFragment.java index 885b8cc454b..bd1e1590efd 100644 --- a/catalog/java/io/material/catalog/bottomappbar/BottomAppBarMainDemoFragment.java +++ b/catalog/java/io/material/catalog/bottomappbar/BottomAppBarMainDemoFragment.java @@ -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); @@ -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) diff --git a/catalog/java/io/material/catalog/bottomsheet/BottomSheetMainDemoFragment.java b/catalog/java/io/material/catalog/bottomsheet/BottomSheetMainDemoFragment.java index 2e7eaa5cb92..cdf831d18e3 100644 --- a/catalog/java/io/material/catalog/bottomsheet/BottomSheetMainDemoFragment.java +++ b/catalog/java/io/material/catalog/bottomsheet/BottomSheetMainDemoFragment.java @@ -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); @@ -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 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 bottomSheetBehavior = + BottomSheetBehavior.from(bottomSheet); + text.setText( + getString( + R.string.cat_bottomsheet_state_half_expanded, + bottomSheetBehavior.getHalfExpandedRatio())); + break; + default: + break; + } } private void setupBackHandling(BottomSheetBehavior behavior) { @@ -294,25 +300,29 @@ private void setupBackHandling(BottomSheetBehavior 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; + } + } }