Skip to content

Commit

Permalink
[Catalog][BottomSheet] Fix full-screen dialog layout after restrict e…
Browse files Browse the repository at this point in the history
…xpansion is enabled once

In the original implementation, we wrongly applied layout height on the bottom sheet dialog root view, instead of its content, which makes full-screen dialog broken since it's setting layout height to the content instead. Refactors the logic so the layout height will always be set to the content, instead of the root view.

PiperOrigin-RevId: 450932326
  • Loading branch information
drchen authored and afohrman committed May 25, 2022
1 parent 1e08c68 commit 109748e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
Expand Up @@ -24,7 +24,6 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
Expand All @@ -43,15 +42,19 @@

/** A fragment that displays the main BottomSheet demo for the Catalog app. */
public class BottomSheetMainDemoFragment extends DemoFragment {

private WindowPreferencesManager windowPreferencesManager;
private BottomSheetDialog bottomSheetDialog;
private WindowInsetsCompat windowInsets;
private int peekHeightPx;

private SwitchMaterial fullScreenSwitch;
private SwitchMaterial restrictExpansionSwitch;

@Override
public void onCreate(@Nullable Bundle bundle) {
super.onCreate(bundle);
windowPreferencesManager = new WindowPreferencesManager(getContext());
windowPreferencesManager = new WindowPreferencesManager(requireContext());
peekHeightPx = getResources().getDimensionPixelSize(R.dimen.cat_bottom_sheet_peek_height);
}

@Override
Expand All @@ -60,13 +63,13 @@ public View onCreateDemoView(
View view = layoutInflater.inflate(getDemoContent(), viewGroup, false /* attachToRoot */);

// Set up BottomSheetDialog
bottomSheetDialog = new BottomSheetDialog(getContext());
bottomSheetDialog = new BottomSheetDialog(requireContext());
bottomSheetDialog.setContentView(R.layout.cat_bottomsheet_content);
// Opt in to perform swipe to dismiss animation when dismissing bottom sheet dialog.
bottomSheetDialog.setDismissWithAnimation(true);
windowPreferencesManager.applyEdgeToEdgePreference(bottomSheetDialog.getWindow());
View bottomSheetInternal = bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheetInternal).setPeekHeight(400);
BottomSheetBehavior.from(bottomSheetInternal).setPeekHeight(peekHeightPx);
View button = view.findViewById(R.id.bottomsheet_button);
button.setOnClickListener(
v -> {
Expand Down Expand Up @@ -95,36 +98,25 @@ public View onCreateDemoView(
});
});

SwitchMaterial expansionSwitch = view.findViewById(R.id.cat_bottomsheet_expansion_switch);

SwitchMaterial fullScreenSwitch = view.findViewById(R.id.cat_fullscreen_switch);
fullScreenSwitch = view.findViewById(R.id.cat_fullscreen_switch);
fullScreenSwitch.setOnCheckedChangeListener(
(buttonView, isChecked) -> {
setBottomSheetHeights(view, isChecked);
expansionSwitch.setEnabled(!isChecked);
restrictExpansionSwitch.setEnabled(!isChecked);
updateBottomSheetHeights();
});

View bottomSheetPersistent = view.findViewById(R.id.bottom_drawer);

expansionSwitch.setOnCheckedChangeListener(
restrictExpansionSwitch = view.findViewById(R.id.cat_bottomsheet_expansion_switch);
restrictExpansionSwitch.setOnCheckedChangeListener(
(buttonView, isChecked) -> {
LayoutParams lp = bottomSheetInternal.getLayoutParams();
lp.height = isChecked ? 400 : getBottomSheetDialogDefaultHeight();
bottomSheetInternal.setLayoutParams(lp);

lp = bottomSheetPersistent.getLayoutParams();
lp.height = isChecked ? 400 : getBottomSheetPersistentDefaultHeight();
bottomSheetPersistent.setLayoutParams(lp);

fullScreenSwitch.setEnabled(!isChecked);

view.requestLayout();
updateBottomSheetHeights();
});

TextView dialogText = bottomSheetInternal.findViewById(R.id.bottomsheet_state);
BottomSheetBehavior.from(bottomSheetInternal)
.addBottomSheetCallback(createBottomSheetCallback(dialogText));
TextView bottomSheetText = view.findViewById(R.id.cat_persistent_bottomsheet_state);
View bottomSheetPersistent = view.findViewById(R.id.bottom_drawer);
BottomSheetBehavior.from(bottomSheetPersistent)
.addBottomSheetCallback(createBottomSheetCallback(bottomSheetText));

Expand All @@ -151,7 +143,7 @@ public View onCreateDemoView(
view,
(ignored, insets) -> {
windowInsets = insets;
setBottomSheetHeights(view, fullScreenSwitch.isChecked());
updateBottomSheetHeights();
return insets;
});

Expand All @@ -166,23 +158,27 @@ private int getBottomSheetPersistentDefaultHeight() {
return getWindowHeight() * 3 / 5;
}

private void setBottomSheetHeights(View view, boolean fullScreen) {
private void updateBottomSheetHeights() {
View view = getView();
View bottomSheetChildView = view.findViewById(R.id.bottom_drawer);
ViewGroup.LayoutParams params = bottomSheetChildView.getLayoutParams();
BottomSheetBehavior<View> bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetChildView);
bottomSheetBehavior.setUpdateImportantForAccessibilityOnSiblings(fullScreen);
bottomSheetBehavior.setUpdateImportantForAccessibilityOnSiblings(fullScreenSwitch.isChecked());
View modalBottomSheetChildView = bottomSheetDialog.findViewById(R.id.bottom_drawer_2);
ViewGroup.LayoutParams layoutParams = modalBottomSheetChildView.getLayoutParams();
BottomSheetBehavior<FrameLayout> modalBottomSheetBehavior = bottomSheetDialog.getBehavior();
boolean fitToContents = true;
float halfExpandedRatio = 0.5f;
int windowHeight = getWindowHeight();
if (params != null && layoutParams != null) {
if (fullScreen) {
if (fullScreenSwitch.isEnabled() && fullScreenSwitch.isChecked()) {
params.height = windowHeight;
layoutParams.height = windowHeight;
fitToContents = false;
halfExpandedRatio = 0.7f;
} else if (restrictExpansionSwitch.isEnabled() && restrictExpansionSwitch.isChecked()) {
params.height = peekHeightPx;
layoutParams.height = peekHeightPx;
} else {
params.height = getBottomSheetPersistentDefaultHeight();
layoutParams.height = getBottomSheetDialogDefaultHeight();
Expand Down
19 changes: 19 additions & 0 deletions catalog/java/io/material/catalog/bottomsheet/res/values/dimens.xml
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2019 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<dimen name="cat_bottom_sheet_peek_height">200dp</dimen>
</resources>

0 comments on commit 109748e

Please sign in to comment.