From 4262e9aec751cc8343cf811d9e5cb690766a40cf Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Fri, 16 Mar 2018 18:40:03 +0200 Subject: [PATCH] Add small refactoring to MenuBottomSheetDialogFragment --- .../base/MenuBottomSheetDialogFragment.java | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java index e5536433dd..6ca1f973c3 100644 --- a/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java @@ -32,6 +32,7 @@ import java.util.List; public abstract class MenuBottomSheetDialogFragment extends BottomSheetDialogFragment { private static final String USED_ON_MAP_KEY = "used_on_map"; + private static final int DEFAULT_VALUE = -1; protected List items = new ArrayList<>(); @@ -75,7 +76,7 @@ public abstract class MenuBottomSheetDialogFragment extends BottomSheetDialogFra } int bottomDividerColorId = getBottomDividerColorId(); - if (bottomDividerColorId != -1) { + if (bottomDividerColorId != DEFAULT_VALUE) { mainView.findViewById(R.id.bottom_row_divider).setBackgroundColor(getResolvedColor(bottomDividerColorId)); } @@ -89,10 +90,10 @@ public abstract class MenuBottomSheetDialogFragment extends BottomSheetDialogFra ((TextView) mainView.findViewById(R.id.dismiss_button_text)).setText(getDismissButtonTextId()); int rightBottomButtonTextId = getRightBottomButtonTextId(); - if (rightBottomButtonTextId != -1) { + if (rightBottomButtonTextId != DEFAULT_VALUE) { View buttonsDivider = mainView.findViewById(R.id.bottom_buttons_divider); buttonsDivider.setVisibility(View.VISIBLE); - if (bottomDividerColorId != -1) { + if (bottomDividerColorId != DEFAULT_VALUE) { buttonsDivider.setBackgroundColor(getResolvedColor(bottomDividerColorId)); } View rightButton = mainView.findViewById(R.id.right_bottom_button); @@ -158,15 +159,15 @@ public abstract class MenuBottomSheetDialogFragment extends BottomSheetDialogFra final int screenHeight = AndroidUtils.getScreenHeight(activity); final int statusBarHeight = AndroidUtils.getStatusBarHeight(activity); - final int availableHeight = getAvailableHeight(screenHeight, statusBarHeight); + final int contentHeight = getContentHeight(screenHeight - statusBarHeight - AndroidUtils.getNavBarHeight(activity)); mainView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { - final View viewToAdjust = useScrollableItemsContainer() ? mainView.findViewById(R.id.scroll_view) : itemsContainer; - if (viewToAdjust.getHeight() > availableHeight) { - viewToAdjust.getLayoutParams().height = availableHeight; - viewToAdjust.requestLayout(); + final View contentView = useScrollableItemsContainer() ? mainView.findViewById(R.id.scroll_view) : itemsContainer; + if (contentView.getHeight() > contentHeight) { + contentView.getLayoutParams().height = contentHeight; + contentView.requestLayout(); } // 8dp is the shadow height @@ -187,16 +188,19 @@ public abstract class MenuBottomSheetDialogFragment extends BottomSheetDialogFra }); } - private int getAvailableHeight(int screenHeight, int statusBarHeight) { - int maxHeight = getMaximumHeight(); - return maxHeight != -1 ? maxHeight - : screenHeight - statusBarHeight - AndroidUtils.getNavBarHeight(getContext()) + private int getContentHeight(int availableScreenHeight) { + int customHeight = getCustomHeight(); + int maxHeight = availableScreenHeight - AndroidUtils.dpToPx(getContext(), 1) // divider height - getResources().getDimensionPixelSize(R.dimen.bottom_sheet_cancel_button_height); + if (customHeight != DEFAULT_VALUE && customHeight <= maxHeight) { + return customHeight; + } + return maxHeight; } - protected int getMaximumHeight() { - return -1; + protected int getCustomHeight() { + return DEFAULT_VALUE; } protected boolean useScrollableItemsContainer() { @@ -205,7 +209,7 @@ public abstract class MenuBottomSheetDialogFragment extends BottomSheetDialogFra @ColorRes protected int getBottomDividerColorId() { - return -1; + return DEFAULT_VALUE; } @StringRes @@ -219,7 +223,7 @@ public abstract class MenuBottomSheetDialogFragment extends BottomSheetDialogFra @StringRes protected int getRightBottomButtonTextId() { - return -1; + return DEFAULT_VALUE; } protected void onRightBottomButtonClick() {