From ae2eef75f301f70ef26d4149a4051baa046191d4 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Fri, 16 Apr 2021 18:28:21 +0300 Subject: [PATCH] refactoring p.5: optimize bottom sheets dialogs --- .../plus/base/ModeSelectionBottomSheet.java | 80 ++++++++++ ...java => MultipleSelectionBottomSheet.java} | 118 +++++++------- .../base/MultipleWithModeBottomSheet.java | 40 +++++ .../plus/base/SelectModeBottomSheet.java | 96 ------------ .../SelectMultipleWithModeBottomSheet.java | 53 ------- .../plus/base/SelectionBottomSheet.java | 144 +++++++++++++----- .../plus/download/SelectIndexesUiHelper.java | 22 +-- 7 files changed, 299 insertions(+), 254 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/base/ModeSelectionBottomSheet.java rename OsmAnd/src/net/osmand/plus/base/{SelectMultipleItemsBottomSheet.java => MultipleSelectionBottomSheet.java} (54%) create mode 100644 OsmAnd/src/net/osmand/plus/base/MultipleWithModeBottomSheet.java delete mode 100644 OsmAnd/src/net/osmand/plus/base/SelectModeBottomSheet.java delete mode 100644 OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java diff --git a/OsmAnd/src/net/osmand/plus/base/ModeSelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/ModeSelectionBottomSheet.java new file mode 100644 index 0000000000..f8355c8805 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/base/ModeSelectionBottomSheet.java @@ -0,0 +1,80 @@ +package net.osmand.plus.base; + +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; +import androidx.fragment.app.FragmentManager; + +import net.osmand.AndroidUtils; +import net.osmand.plus.R; +import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; + +import java.util.Collections; +import java.util.List; + +public class ModeSelectionBottomSheet extends SelectionBottomSheet { + + public static final String TAG = ModeSelectionBottomSheet.class.getSimpleName(); + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + showElements(primaryDescription, toggleContainer); + hideElements(checkBox, checkBoxTitle, titleDescription, + secondaryDescription, selectedSize, selectAllButton); + } + + @Override + protected void updateItemView(SelectableItem item, View view) { + ImageView ivIcon = view.findViewById(R.id.icon); + TextView tvTitle = view.findViewById(R.id.title); + TextView tvDescr = view.findViewById(R.id.description); + + Drawable icon = uiUtilities.getIcon(item.getIconId(), activeColorRes); + ivIcon.setImageDrawable(icon); + tvTitle.setText(item.getTitle()); + tvDescr.setText(item.getDescription()); + tvDescr.setTextColor(ContextCompat.getColor(app, AndroidUtils.getSecondaryTextColorId(nightMode))); + } + + @Override + protected int getItemLayoutId() { + return R.layout.bottom_sheet_item_with_descr_56dp; + } + + public void setItem(SelectableItem item) { + setItems(Collections.singletonList(item)); + } + + @NonNull + @Override + public List getSelectedItems() { + return allItems; + } + + @Override + protected boolean shouldShowDivider() { + return false; + } + + public static ModeSelectionBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull SelectableItem previewItem, + @NonNull List radioItems, + boolean usedOnMap) { + ModeSelectionBottomSheet fragment = new ModeSelectionBottomSheet(); + fragment.setUsedOnMap(usedOnMap); + fragment.setModes(radioItems); + fragment.setItems(Collections.singletonList(previewItem)); + FragmentManager fm = activity.getSupportFragmentManager(); + fragment.show(fm, TAG); + return fragment; + } + +} diff --git a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java similarity index 54% rename from OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java rename to OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java index d9a6204e2c..728dfbaa65 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/MultipleSelectionBottomSheet.java @@ -1,7 +1,12 @@ package net.osmand.plus.base; import android.content.res.ColorStateList; +import android.os.Bundle; import android.view.View; +import android.view.View.OnClickListener; +import android.widget.CheckBox; +import android.widget.ImageView; +import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -12,9 +17,7 @@ import androidx.fragment.app.FragmentManager; import net.osmand.AndroidUtils; import net.osmand.plus.R; -import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; -import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton; -import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton.Builder; +import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.util.Algorithms; import java.util.ArrayList; @@ -24,16 +27,16 @@ import static net.osmand.view.ThreeStateCheckbox.State.CHECKED; import static net.osmand.view.ThreeStateCheckbox.State.MISC; import static net.osmand.view.ThreeStateCheckbox.State.UNCHECKED; -public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { +public class MultipleSelectionBottomSheet extends SelectionBottomSheet { - public static final String TAG = SelectMultipleItemsBottomSheet.class.getSimpleName(); + public static final String TAG = MultipleSelectionBottomSheet.class.getSimpleName(); - private final List allItems = new ArrayList<>(); private final List selectedItems = new ArrayList<>(); private SelectionUpdateListener selectionUpdateListener; @Override - protected void initHeaderUi() { + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); selectAllButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -45,39 +48,49 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { selectedItems.clear(); } onSelectedItemsChanged(); - updateItems(checked); + updateItemsSelection(checked); } }); } @Override - protected void createSelectionUi() { - for (final SelectableItem item : allItems) { - boolean checked = selectedItems.contains(item); - final BottomSheetItemWithCompoundButton[] uiItem = new BottomSheetItemWithCompoundButton[1]; - final Builder builder = (BottomSheetItemWithCompoundButton.Builder) new Builder(); - builder.setChecked(checked) - .setButtonTintList(AndroidUtils.createCheckedColorStateList(app, secondaryColorRes, activeColorRes)) - .setLayoutId(R.layout.bottom_sheet_item_with_descr_and_checkbox_56dp) - .setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - boolean checked = !uiItem[0].isChecked(); - uiItem[0].setChecked(checked); - SelectableItem tag = (SelectableItem) uiItem[0].getTag(); - if (checked) { - selectedItems.add(tag); - } else { - selectedItems.remove(tag); - } - onSelectedItemsChanged(); - } - }) - .setTag(item); - setupListItem(builder, item); - uiItem[0] = builder.create(); - items.add(uiItem[0]); - } + protected boolean shouldShowDivider() { + return true; + } + + @Override + protected void updateItemView(final SelectableItem item, View view) { + boolean checked = selectedItems.contains(item); + ImageView imageView = view.findViewById(R.id.icon); + TextView title = view.findViewById(R.id.title); + TextView description = view.findViewById(R.id.description); + final CheckBox checkBox = view.findViewById(R.id.compound_button); + AndroidUiHelper.setVisibility(View.VISIBLE, imageView, title, description, checkBox); + + checkBox.setChecked(checked); + CompoundButtonCompat.setButtonTintList(checkBox, AndroidUtils.createCheckedColorStateList(app, secondaryColorRes, activeColorRes)); + + view.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + boolean checked = !checkBox.isChecked(); + checkBox.setChecked(checked); + if (checked) { + selectedItems.add(item); + } else { + selectedItems.remove(item); + } + onSelectedItemsChanged(); + } + }); + title.setText(item.getTitle()); + description.setText(item.getDescription()); + imageView.setImageDrawable(uiUtilities.getIcon(item.getIconId(), activeColorRes)); + } + + @Override + protected int getItemLayoutId() { + return R.layout.bottom_sheet_item_with_descr_and_checkbox_56dp; } @Override @@ -95,12 +108,6 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { } } - private void setupListItem(Builder builder, SelectableItem item) { - builder.setTitle(item.getTitle()); - builder.setDescription(item.getDescription()); - builder.setIcon(uiUtilities.getIcon(item.getIconId(), activeColorRes)); - } - private void updateSelectAllButton() { String checkBoxTitle; if (Algorithms.isEmpty(selectedItems)) { @@ -126,21 +133,16 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { rightButton.setEnabled(noEmptySelection); } - private void updateItems(boolean checked) { - for (BaseBottomSheetItem item : items) { - if (item instanceof BottomSheetItemWithCompoundButton) { - ((BottomSheetItemWithCompoundButton) item).setChecked(checked); + private void updateItemsSelection(boolean checked) { + for (SelectableItem item : allItems) { + View v = listViews.get(item); + CheckBox checkBox = v != null ? (CheckBox) v.findViewById(R.id.compound_button) : null; + if (checkBox != null) { + checkBox.setChecked(checked); } } } - protected void setItems(List allItems) { - if (!Algorithms.isEmpty(allItems)) { - this.allItems.clear(); - this.allItems.addAll(allItems); - } - } - protected void setSelectedItems(List selected) { if (!Algorithms.isEmpty(selected)) { selectedItems.clear(); @@ -150,7 +152,7 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { @NonNull @Override - public List getSelection() { + public List getSelectedItems() { return selectedItems; } @@ -158,11 +160,11 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet { this.selectionUpdateListener = selectionUpdateListener; } - public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity, - @NonNull List items, - @Nullable List selected, - boolean usedOnMap) { - SelectMultipleItemsBottomSheet fragment = new SelectMultipleItemsBottomSheet(); + public static MultipleSelectionBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull List items, + @Nullable List selected, + boolean usedOnMap) { + MultipleSelectionBottomSheet fragment = new MultipleSelectionBottomSheet(); fragment.setUsedOnMap(usedOnMap); fragment.setItems(items); fragment.setSelectedItems(selected); diff --git a/OsmAnd/src/net/osmand/plus/base/MultipleWithModeBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/MultipleWithModeBottomSheet.java new file mode 100644 index 0000000000..0c3eb8c795 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/base/MultipleWithModeBottomSheet.java @@ -0,0 +1,40 @@ +package net.osmand.plus.base; + +import android.os.Bundle; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.FragmentManager; + +import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; + +import java.util.List; + +public class MultipleWithModeBottomSheet extends MultipleSelectionBottomSheet { + + public static final String TAG = MultipleWithModeBottomSheet.class.getSimpleName(); + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + showElements(secondaryDescription, toggleContainer, checkBox, + checkBoxTitle, titleDescription, selectedSize, selectAllButton); + } + + public static MultipleWithModeBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull List items, + @Nullable List selected, + @NonNull List modes, + boolean usedOnMap) { + MultipleWithModeBottomSheet fragment = new MultipleWithModeBottomSheet(); + fragment.setUsedOnMap(usedOnMap); + fragment.setItems(items); + fragment.setSelectedItems(selected); + fragment.setModes(modes); + FragmentManager fm = activity.getSupportFragmentManager(); + fragment.show(fm, TAG); + return fragment; + } + +} diff --git a/OsmAnd/src/net/osmand/plus/base/SelectModeBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectModeBottomSheet.java deleted file mode 100644 index d16e61896e..0000000000 --- a/OsmAnd/src/net/osmand/plus/base/SelectModeBottomSheet.java +++ /dev/null @@ -1,96 +0,0 @@ -package net.osmand.plus.base; - -import android.view.View; - -import androidx.annotation.NonNull; -import androidx.appcompat.app.AppCompatActivity; -import androidx.fragment.app.FragmentManager; - -import net.osmand.AndroidUtils; -import net.osmand.plus.R; -import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; -import net.osmand.plus.helpers.AndroidUiHelper; -import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; - -import java.util.Collections; -import java.util.List; - -public class SelectModeBottomSheet extends SelectionBottomSheet { - - public static final String TAG = SelectModeBottomSheet.class.getSimpleName(); - - private BottomSheetItemWithDescription previewUi; - - private List modes; - private SelectableItem previewItem; - - @Override - protected void initHeaderUi() { - radioGroup.setItems(modes); - - AndroidUiHelper.setVisibility(View.VISIBLE, primaryDescription, toggleContainer); - - AndroidUiHelper.setVisibility(View.GONE, checkBox, checkBoxTitle, - titleDescription, secondaryDescription, selectedSize, selectAllButton); - } - - @Override - protected void createSelectionUi() { - previewUi = (BottomSheetItemWithDescription) new BottomSheetItemWithDescription.Builder() - .setDescription(previewItem.getDescription()) - .setDescriptionColorId(AndroidUtils.getSecondaryTextColorId(nightMode)) - .setTitle(previewItem.getTitle()) - .setIcon(uiUtilities.getIcon(previewItem.getIconId(), activeColorRes)) - .setTag(previewItem) - .setLayoutId(R.layout.bottom_sheet_item_with_descr_56dp) - .create(); - items.add(previewUi); - } - - private void updatePreviewUi() { - previewUi.setTitle(previewItem.getTitle()); - previewUi.setIcon(uiUtilities.getIcon(previewItem.getIconId(), activeColorRes)); - previewUi.setDescription(previewItem.getDescription()); - previewUi.setTag(previewItem); - } - - private void setModes(@NonNull List modes) { - this.modes = modes; - } - - public void setSelectedMode(@NonNull RadioItem mode) { - radioGroup.setSelectedItem(mode); - } - - public void setPreviewItem(@NonNull SelectableItem preview) { - this.previewItem = preview; - if (previewUi != null) { - updatePreviewUi(); - } - } - - @NonNull - @Override - public List getSelection() { - return Collections.singletonList(previewItem); - } - - @Override - protected boolean shouldShowDivider() { - return false; - } - - public static SelectModeBottomSheet showInstance(@NonNull AppCompatActivity activity, - @NonNull SelectableItem previewItem, - @NonNull List radioItems, - boolean usedOnMap) { - SelectModeBottomSheet fragment = new SelectModeBottomSheet(); - fragment.setUsedOnMap(usedOnMap); - fragment.setModes(radioItems); - fragment.setPreviewItem(previewItem); - FragmentManager fm = activity.getSupportFragmentManager(); - fragment.show(fm, TAG); - return fragment; - } - -} diff --git a/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java deleted file mode 100644 index 4318d9c3c7..0000000000 --- a/OsmAnd/src/net/osmand/plus/base/SelectMultipleWithModeBottomSheet.java +++ /dev/null @@ -1,53 +0,0 @@ -package net.osmand.plus.base; - -import android.view.View; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatActivity; -import androidx.fragment.app.FragmentManager; - -import net.osmand.plus.helpers.AndroidUiHelper; -import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; - -import java.util.List; - -public class SelectMultipleWithModeBottomSheet extends SelectMultipleItemsBottomSheet { - - public static final String TAG = SelectMultipleWithModeBottomSheet.class.getSimpleName(); - - private List modes; - - @Override - protected void initHeaderUi() { - super.initHeaderUi(); - radioGroup.setItems(modes); - - AndroidUiHelper.setVisibility(View.VISIBLE, secondaryDescription, toggleContainer, - checkBox, checkBoxTitle, titleDescription, selectedSize, selectAllButton); - } - - private void setModes(@NonNull List modes) { - this.modes = modes; - } - - public void setSelectedMode(@NonNull RadioItem mode) { - radioGroup.setSelectedItem(mode); - } - - public static SelectMultipleWithModeBottomSheet showInstance(@NonNull AppCompatActivity activity, - @NonNull List items, - @Nullable List selected, - @NonNull List modes, - boolean usedOnMap) { - SelectMultipleWithModeBottomSheet fragment = new SelectMultipleWithModeBottomSheet(); - fragment.setUsedOnMap(usedOnMap); - fragment.setItems(items); - fragment.setSelectedItems(selected); - fragment.setModes(modes); - FragmentManager fm = activity.getSupportFragmentManager(); - fragment.show(fm, TAG); - return fragment; - } - -} diff --git a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java index 6f788a9be2..26a66cdb61 100644 --- a/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/base/SelectionBottomSheet.java @@ -1,10 +1,12 @@ package net.osmand.plus.base; +import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; +import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; import androidx.annotation.NonNull; @@ -16,14 +18,21 @@ import net.osmand.plus.UiUtilities; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.SimpleDividerItem; +import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.widgets.MultiStateToggleButton; +import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem; +import net.osmand.util.Algorithms; import net.osmand.view.ThreeStateCheckbox; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment { protected OsmandApplication app; + protected LayoutInflater inflater; protected UiUtilities uiUtilities; protected TextView title; @@ -45,6 +54,10 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment private OnUiInitializedListener uiInitializedListener; private OnApplySelectionListener applySelectionListener; + protected List allItems = new ArrayList<>(); + protected Map listViews = new HashMap<>(); + private List modes; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { @@ -57,27 +70,19 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment public void createMenuItems(Bundle savedInstanceState) { app = requiredMyApplication(); uiUtilities = app.getUIUtilities(); + inflater = UiUtilities.getInflater(requireContext(), nightMode); activeColorRes = nightMode ? R.color.icon_color_active_dark : R.color.icon_color_active_light; secondaryColorRes = nightMode ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light; - items.add(createHeaderUi()); + items.add(createHeaderView()); if (shouldShowDivider()) { items.add(new SimpleDividerItem(app)); } - createSelectionUi(); + items.add(createSelectionView()); } - @Override - public void onPause() { - super.onPause(); - if (requireActivity().isChangingConfigurations()) { - dismiss(); - } - } - - private BaseBottomSheetItem createHeaderUi() { - LayoutInflater themedInflater = UiUtilities.getInflater(requireContext(), nightMode); - View view = themedInflater.inflate(R.layout.settings_group_title, null); + private BaseBottomSheetItem createHeaderView() { + View view = inflater.inflate(R.layout.settings_group_title, null); title = view.findViewById(R.id.title); titleDescription = view.findViewById(R.id.title_description); @@ -90,22 +95,22 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment checkBoxTitle = view.findViewById(R.id.check_box_title); checkBox = view.findViewById(R.id.check_box); - initHeaderUi(); + if (modes != null) { + radioGroup.setItems(modes); + } + return new SimpleBottomSheetItem.Builder().setCustomView(view).create(); } - protected abstract void createSelectionUi(); - - protected abstract void initHeaderUi(); - - protected boolean shouldShowDivider() { - return true; - } - - @Override - protected void setupRightButton() { - super.setupRightButton(); - applyButtonTitle = rightButton.findViewById(R.id.button_text); + private BaseBottomSheetItem createSelectionView() { + Context themedCtx = UiUtilities.getThemedContext(requireContext(), nightMode); + listContainer = new LinearLayout(themedCtx); + listContainer.setLayoutParams(new LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT)); + listContainer.setOrientation(LinearLayout.VERTICAL); + fillInSelectionList(); + return new SimpleBottomSheetItem.Builder().setCustomView(listContainer).create(); } public void setTitle(@NonNull String title) { @@ -128,20 +133,23 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment applyButtonTitle.setText(title); } - @Override - protected void onRightBottomButtonClick() { - if (applySelectionListener != null) { - applySelectionListener.onSelectionApplied(getSelection()); + public void setModes(@NonNull List modes) { + this.modes = modes; + if (radioGroup != null) { + radioGroup.setItems(modes); } - dismiss(); } - @NonNull - public abstract List getSelection(); + public void setSelectedMode(@NonNull RadioItem mode) { + radioGroup.setSelectedItem(mode); + } - @Override - protected int getRightBottomButtonTextId() { - return R.string.shared_string_apply; + public void setItems(List allItems) { + if (!Algorithms.isEmpty(allItems)) { + this.allItems.clear(); + this.allItems.addAll(allItems); + fillInSelectionList(); + } } public void setUiInitializedListener(OnUiInitializedListener uiInitializedListener) { @@ -152,17 +160,81 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment this.applySelectionListener = onApplySelectionListener; } + private void fillInSelectionList() { + if (listContainer != null && allItems != null) { + recreateList(); + } + } + + private void recreateList() { + listViews.clear(); + listContainer.removeAllViews(); + for (SelectableItem item : allItems) { + setupItemView(item, inflater.inflate(getItemLayoutId(), null)); + } + } + + private void setupItemView(SelectableItem item, View view) { + updateItemView(item, view); + listViews.put(item, view); + listContainer.addView(view); + } + + @NonNull + public abstract List getSelectedItems(); + + protected abstract void updateItemView(SelectableItem item, View view); + + protected abstract int getItemLayoutId(); + + protected abstract boolean shouldShowDivider(); + protected void notifyUiInitialized() { if (uiInitializedListener != null) { uiInitializedListener.onUiInitialized(); } } + protected void showElements(View... views) { + AndroidUiHelper.setVisibility(View.VISIBLE, views); + } + + protected void hideElements(View... views) { + AndroidUiHelper.setVisibility(View.GONE, views); + } + + @Override + protected void setupRightButton() { + super.setupRightButton(); + applyButtonTitle = rightButton.findViewById(R.id.button_text); + } + + @Override + protected void onRightBottomButtonClick() { + if (applySelectionListener != null) { + applySelectionListener.onSelectionApplied(getSelectedItems()); + } + dismiss(); + } + + @Override + protected int getRightBottomButtonTextId() { + return R.string.shared_string_apply; + } + @Override protected boolean useVerticalButtons() { return true; } + @Override + public void onPause() { + super.onPause(); + if (requireActivity().isChangingConfigurations()) { + dismiss(); + } + } + public interface OnUiInitializedListener { void onUiInitialized(); } diff --git a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java index 7933b4eeff..03b9bc7b9b 100644 --- a/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/SelectIndexesUiHelper.java @@ -8,10 +8,10 @@ import androidx.appcompat.app.AppCompatActivity; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import net.osmand.plus.base.SelectMultipleItemsBottomSheet; -import net.osmand.plus.base.SelectMultipleItemsBottomSheet.SelectionUpdateListener; -import net.osmand.plus.base.SelectModeBottomSheet; -import net.osmand.plus.base.SelectMultipleWithModeBottomSheet; +import net.osmand.plus.base.MultipleSelectionBottomSheet; +import net.osmand.plus.base.MultipleSelectionBottomSheet.SelectionUpdateListener; +import net.osmand.plus.base.ModeSelectionBottomSheet; +import net.osmand.plus.base.MultipleWithModeBottomSheet; import net.osmand.plus.base.SelectionBottomSheet; import net.osmand.plus.base.SelectionBottomSheet.OnApplySelectionListener; import net.osmand.plus.base.SelectionBottomSheet.OnUiInitializedListener; @@ -96,8 +96,8 @@ public class SelectIndexesUiHelper { } } - final SelectMultipleItemsBottomSheet dialog = - SelectMultipleItemsBottomSheet.showInstance(activity, allItems, selectedItems, true); + final MultipleSelectionBottomSheet dialog = + MultipleSelectionBottomSheet.showInstance(activity, allItems, selectedItems, true); dialog.setUiInitializedListener(new OnUiInitializedListener() { @Override @@ -131,7 +131,7 @@ public class SelectIndexesUiHelper { radioItems.add(meters); radioItems.add(feet); - dialog = SelectModeBottomSheet.showInstance(activity, + dialog = ModeSelectionBottomSheet.showInstance(activity, baseSRTM ? meterItem : feetItem, radioItems, true); final RadioItem initRadio = baseSRTM ? meters : feet; @@ -139,7 +139,7 @@ public class SelectIndexesUiHelper { dialog.setUiInitializedListener(new OnUiInitializedListener() { @Override public void onUiInitialized() { - SelectModeBottomSheet dialog = (SelectModeBottomSheet) SelectIndexesUiHelper.this.dialog; + ModeSelectionBottomSheet dialog = (ModeSelectionBottomSheet) SelectIndexesUiHelper.this.dialog; dialog.setTitle(app.getString(R.string.srtm_unit_format)); dialog.setPrimaryDescription(app.getString(R.string.srtm_download_single_help_message)); updateSize(dialog, false); @@ -169,7 +169,7 @@ public class SelectIndexesUiHelper { radioItem.setOnClickListener(new OnRadioItemClickListener() { @Override public boolean onRadioItemClick(RadioItem radioItem, View view) { - ((SelectModeBottomSheet)dialog).setPreviewItem(selectableItem); + ((ModeSelectionBottomSheet)dialog).setItem(selectableItem); updateSize(dialog, false); return true; } @@ -210,7 +210,7 @@ public class SelectIndexesUiHelper { radioItems.add(meters); radioItems.add(feet); - final SelectMultipleWithModeBottomSheet dialog = SelectMultipleWithModeBottomSheet.showInstance( + final MultipleWithModeBottomSheet dialog = MultipleWithModeBottomSheet.showInstance( activity, itemsList, selectedItems, radioItems, true); meters.setOnClickListener(new OnRadioItemClickListener() { @@ -265,7 +265,7 @@ public class SelectIndexesUiHelper { private void updateSize(SelectionBottomSheet dialog, boolean updateDescription) { - double sizeToDownload = getDownloadSizeInMb(dialog.getSelection()); + double sizeToDownload = getDownloadSizeInMb(dialog.getSelectedItems()); String size = DownloadItem.getFormattedMb(app, sizeToDownload); if (updateDescription) { String total = app.getString(R.string.shared_string_total);