From 60b9cbd91f7eac15d814dd58eb8032b401aa07d2 Mon Sep 17 00:00:00 2001 From: androiddevkkotlin Date: Thu, 3 Dec 2020 12:03:23 +0200 Subject: [PATCH] Refactor --- .../base/MenuBottomSheetDialogFragment.java | 25 +++++++++----- .../dialogs/SendGpxBottomSheetFragment.java | 33 +++++-------------- .../VehicleParametersBottomSheet.java | 32 +++++------------- 3 files changed, 33 insertions(+), 57 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java index 84bd292aa5..71192fafe6 100644 --- a/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java @@ -187,18 +187,27 @@ public abstract class MenuBottomSheetDialogFragment extends BottomSheetDialogFra // 8dp is the shadow height boolean showTopShadow = screenHeight - statusBarHeight - mainView.getHeight() >= AndroidUtils.dpToPx(activity, 8); - if (AndroidUiHelper.isOrientationPortrait(activity)) { - AndroidUtils.setBackground(mainView, showTopShadow ? getPortraitBg(activity) : getColoredBg(activity)); - if (!showTopShadow) { - mainView.setPadding(0, 0, 0, 0); - } - } else { - AndroidUtils.setBackground(mainView, showTopShadow ? getLandscapeTopsidesBg(activity) : getLandscapeSidesBg(activity)); - } + drawTopShadow(showTopShadow); } }); } + protected void drawTopShadow(boolean showTopShadow) { + final Activity activity = getActivity(); + View mainView = getView(); + if (activity == null || mainView == null) { + return; + } + if (AndroidUiHelper.isOrientationPortrait(activity)) { + AndroidUtils.setBackground(mainView, showTopShadow ? getPortraitBg(activity) : getColoredBg(activity)); + if (!showTopShadow) { + mainView.setPadding(0, 0, 0, 0); + } + } else { + AndroidUtils.setBackground(mainView, showTopShadow ? getLandscapeTopsidesBg(activity) : getLandscapeSidesBg(activity)); + } + } + private int getContentHeight(int availableScreenHeight) { int customHeight = getCustomHeight(); int buttonsHeight; diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendGpxBottomSheetFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendGpxBottomSheetFragment.java index ca7bf3ec53..759f924d10 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendGpxBottomSheetFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/SendGpxBottomSheetFragment.java @@ -1,6 +1,5 @@ package net.osmand.plus.osmedit.dialogs; -import android.app.Activity; import android.graphics.Rect; import android.os.AsyncTask; import android.os.Bundle; @@ -22,7 +21,6 @@ import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.textfield.TextInputEditText; -import net.osmand.AndroidUtils; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; @@ -30,7 +28,6 @@ import net.osmand.plus.UiUtilities.DialogButtonType; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; -import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter; import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionAdapterListener; import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionItem; @@ -58,6 +55,9 @@ public class SendGpxBottomSheetFragment extends MenuBottomSheetDialogFragment { private TextInputEditText tagsField; private TextInputEditText messageField; private int contentHeightPrevious = 0; + private int buttonsHeight; + private int shadowHeight; + private ScrollView scrollView; public void setGpxInfos(GpxInfo[] gpxInfos) { this.gpxInfos = gpxInfos; @@ -137,37 +137,20 @@ public class SendGpxBottomSheetFragment extends MenuBottomSheetDialogFragment { @Override public void onGlobalLayout() { Rect visibleDisplayFrame = new Rect(); - Activity activity = getActivity(); - int buttonsHeight = getResources().getDimensionPixelSize(R.dimen.dialog_button_ex_max_width); - int shadowHeight = getResources().getDimensionPixelSize(R.dimen.bottom_sheet_top_shadow_height); - boolean showTopShadow; - final ScrollView scrollView = getView().findViewById(R.id.scroll_view); + buttonsHeight = getResources().getDimensionPixelSize(R.dimen.dialog_button_ex_max_width); + shadowHeight = getResources().getDimensionPixelSize(R.dimen.bottom_sheet_top_shadow_height); + scrollView = getView().findViewById(R.id.scroll_view); scrollView.getWindowVisibleDisplayFrame(visibleDisplayFrame); int viewHeight = scrollView.getHeight(); int contentHeight = visibleDisplayFrame.bottom - visibleDisplayFrame.top - buttonsHeight; - if (contentHeightPrevious != contentHeight && activity != null) { + if (contentHeightPrevious != contentHeight) { + boolean showTopShadow; showTopShadow = viewHeight + shadowHeight < contentHeight; scrollView.requestLayout(); contentHeightPrevious = contentHeight; drawTopShadow(showTopShadow); } } - - private void drawTopShadow(boolean showTopShadow) { - final Activity activity = getActivity(); - View mainView = getView(); - if (activity == null || mainView == null) { - return; - } - if (AndroidUiHelper.isOrientationPortrait(activity)) { - AndroidUtils.setBackground(mainView, showTopShadow ? getPortraitBg(activity) : getColoredBg(activity)); - if (!showTopShadow) { - mainView.setPadding(0, 0, 0, 0); - } - } else { - AndroidUtils.setBackground(mainView, showTopShadow ? getLandscapeTopsidesBg(activity) : getLandscapeSidesBg(activity)); - } - } }; } diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersBottomSheet.java index b3f160f910..8f4b4b4181 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersBottomSheet.java @@ -1,7 +1,6 @@ package net.osmand.plus.settings.bottomsheets; import android.annotation.SuppressLint; -import android.app.Activity; import android.graphics.Rect; import android.os.Build; import android.os.Bundle; @@ -23,13 +22,11 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.recyclerview.widget.RecyclerView; -import net.osmand.AndroidUtils; import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; -import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.fragments.ApplyQueryType; @@ -52,6 +49,9 @@ public class VehicleParametersBottomSheet extends BasePreferenceBottomSheet { private float currentValue; private int contentHeightPrevious = 0; private EditText text; + private int buttonsHeight; + private int shadowHeight; + private ScrollView scrollView; @Override public void createMenuItems(Bundle savedInstanceState) { @@ -164,13 +164,13 @@ public class VehicleParametersBottomSheet extends BasePreferenceBottomSheet { @Override public void onGlobalLayout() { Rect visibleDisplayFrame = new Rect(); - int buttonsHeight = getResources().getDimensionPixelSize(R.dimen.dialog_button_ex_height); - int shadowHeight = getResources().getDimensionPixelSize(R.dimen.bottom_sheet_top_shadow_height); - final ScrollView scrollView = getView().findViewById(R.id.scroll_view); + buttonsHeight = getResources().getDimensionPixelSize(R.dimen.dialog_button_ex_height); + shadowHeight = getResources().getDimensionPixelSize(R.dimen.bottom_sheet_top_shadow_height); + scrollView = getView().findViewById(R.id.scroll_view); scrollView.getWindowVisibleDisplayFrame(visibleDisplayFrame); - boolean showTopShadow; int contentHeight = visibleDisplayFrame.bottom - visibleDisplayFrame.top - buttonsHeight; if (contentHeightPrevious != contentHeight) { + boolean showTopShadow; if (scrollView.getHeight() + shadowHeight > contentHeight) { scrollView.getLayoutParams().height = contentHeight; showTopShadow = false; @@ -189,22 +189,6 @@ public class VehicleParametersBottomSheet extends BasePreferenceBottomSheet { drawTopShadow(showTopShadow); } } - - private void drawTopShadow(boolean showTopShadow) { - final Activity activity = getActivity(); - View mainView = getView(); - if (activity == null || mainView == null) { - return; - } - if (AndroidUiHelper.isOrientationPortrait(activity)) { - AndroidUtils.setBackground(mainView, showTopShadow ? getPortraitBg(activity) : getColoredBg(activity)); - if (!showTopShadow) { - mainView.setPadding(0, 0, 0, 0); - } - } else { - AndroidUtils.setBackground(mainView, showTopShadow ? getLandscapeTopsidesBg(activity) : getLandscapeSidesBg(activity)); - } - } }; } @@ -225,7 +209,7 @@ public class VehicleParametersBottomSheet extends BasePreferenceBottomSheet { } public static void showInstance(@NonNull FragmentManager fm, String key, Fragment target, - boolean usedOnMap, @Nullable ApplicationMode appMode) { + boolean usedOnMap, @Nullable ApplicationMode appMode) { try { if (!fm.isStateSaved()) { Bundle args = new Bundle();