refactoring p.5: optimize bottom sheets dialogs
This commit is contained in:
parent
45be6b1919
commit
ae2eef75f3
7 changed files with 299 additions and 254 deletions
|
@ -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<SelectableItem> getSelectedItems() {
|
||||||
|
return allItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean shouldShowDivider() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ModeSelectionBottomSheet showInstance(@NonNull AppCompatActivity activity,
|
||||||
|
@NonNull SelectableItem previewItem,
|
||||||
|
@NonNull List<RadioItem> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,7 +1,12 @@
|
||||||
package net.osmand.plus.base;
|
package net.osmand.plus.base;
|
||||||
|
|
||||||
import android.content.res.ColorStateList;
|
import android.content.res.ColorStateList;
|
||||||
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
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.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
@ -12,9 +17,7 @@ import androidx.fragment.app.FragmentManager;
|
||||||
|
|
||||||
import net.osmand.AndroidUtils;
|
import net.osmand.AndroidUtils;
|
||||||
import net.osmand.plus.R;
|
import net.osmand.plus.R;
|
||||||
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
|
import net.osmand.plus.helpers.AndroidUiHelper;
|
||||||
import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton;
|
|
||||||
import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton.Builder;
|
|
||||||
import net.osmand.util.Algorithms;
|
import net.osmand.util.Algorithms;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
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.MISC;
|
||||||
import static net.osmand.view.ThreeStateCheckbox.State.UNCHECKED;
|
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<SelectableItem> allItems = new ArrayList<>();
|
|
||||||
private final List<SelectableItem> selectedItems = new ArrayList<>();
|
private final List<SelectableItem> selectedItems = new ArrayList<>();
|
||||||
private SelectionUpdateListener selectionUpdateListener;
|
private SelectionUpdateListener selectionUpdateListener;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initHeaderUi() {
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||||
|
super.onViewCreated(view, savedInstanceState);
|
||||||
selectAllButton.setOnClickListener(new View.OnClickListener() {
|
selectAllButton.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
|
@ -45,39 +48,49 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet {
|
||||||
selectedItems.clear();
|
selectedItems.clear();
|
||||||
}
|
}
|
||||||
onSelectedItemsChanged();
|
onSelectedItemsChanged();
|
||||||
updateItems(checked);
|
updateItemsSelection(checked);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void createSelectionUi() {
|
protected boolean shouldShowDivider() {
|
||||||
for (final SelectableItem item : allItems) {
|
return true;
|
||||||
boolean checked = selectedItems.contains(item);
|
}
|
||||||
final BottomSheetItemWithCompoundButton[] uiItem = new BottomSheetItemWithCompoundButton[1];
|
|
||||||
final Builder builder = (BottomSheetItemWithCompoundButton.Builder) new Builder();
|
@Override
|
||||||
builder.setChecked(checked)
|
protected void updateItemView(final SelectableItem item, View view) {
|
||||||
.setButtonTintList(AndroidUtils.createCheckedColorStateList(app, secondaryColorRes, activeColorRes))
|
boolean checked = selectedItems.contains(item);
|
||||||
.setLayoutId(R.layout.bottom_sheet_item_with_descr_and_checkbox_56dp)
|
ImageView imageView = view.findViewById(R.id.icon);
|
||||||
.setOnClickListener(new View.OnClickListener() {
|
TextView title = view.findViewById(R.id.title);
|
||||||
@Override
|
TextView description = view.findViewById(R.id.description);
|
||||||
public void onClick(View v) {
|
final CheckBox checkBox = view.findViewById(R.id.compound_button);
|
||||||
boolean checked = !uiItem[0].isChecked();
|
AndroidUiHelper.setVisibility(View.VISIBLE, imageView, title, description, checkBox);
|
||||||
uiItem[0].setChecked(checked);
|
|
||||||
SelectableItem tag = (SelectableItem) uiItem[0].getTag();
|
checkBox.setChecked(checked);
|
||||||
if (checked) {
|
CompoundButtonCompat.setButtonTintList(checkBox, AndroidUtils.createCheckedColorStateList(app, secondaryColorRes, activeColorRes));
|
||||||
selectedItems.add(tag);
|
|
||||||
} else {
|
view.setOnClickListener(new OnClickListener() {
|
||||||
selectedItems.remove(tag);
|
@Override
|
||||||
}
|
public void onClick(View v) {
|
||||||
onSelectedItemsChanged();
|
boolean checked = !checkBox.isChecked();
|
||||||
}
|
checkBox.setChecked(checked);
|
||||||
})
|
if (checked) {
|
||||||
.setTag(item);
|
selectedItems.add(item);
|
||||||
setupListItem(builder, item);
|
} else {
|
||||||
uiItem[0] = builder.create();
|
selectedItems.remove(item);
|
||||||
items.add(uiItem[0]);
|
}
|
||||||
}
|
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
|
@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() {
|
private void updateSelectAllButton() {
|
||||||
String checkBoxTitle;
|
String checkBoxTitle;
|
||||||
if (Algorithms.isEmpty(selectedItems)) {
|
if (Algorithms.isEmpty(selectedItems)) {
|
||||||
|
@ -126,21 +133,16 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet {
|
||||||
rightButton.setEnabled(noEmptySelection);
|
rightButton.setEnabled(noEmptySelection);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateItems(boolean checked) {
|
private void updateItemsSelection(boolean checked) {
|
||||||
for (BaseBottomSheetItem item : items) {
|
for (SelectableItem item : allItems) {
|
||||||
if (item instanceof BottomSheetItemWithCompoundButton) {
|
View v = listViews.get(item);
|
||||||
((BottomSheetItemWithCompoundButton) item).setChecked(checked);
|
CheckBox checkBox = v != null ? (CheckBox) v.findViewById(R.id.compound_button) : null;
|
||||||
|
if (checkBox != null) {
|
||||||
|
checkBox.setChecked(checked);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setItems(List<SelectableItem> allItems) {
|
|
||||||
if (!Algorithms.isEmpty(allItems)) {
|
|
||||||
this.allItems.clear();
|
|
||||||
this.allItems.addAll(allItems);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setSelectedItems(List<SelectableItem> selected) {
|
protected void setSelectedItems(List<SelectableItem> selected) {
|
||||||
if (!Algorithms.isEmpty(selected)) {
|
if (!Algorithms.isEmpty(selected)) {
|
||||||
selectedItems.clear();
|
selectedItems.clear();
|
||||||
|
@ -150,7 +152,7 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet {
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public List<SelectableItem> getSelection() {
|
public List<SelectableItem> getSelectedItems() {
|
||||||
return selectedItems;
|
return selectedItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,11 +160,11 @@ public class SelectMultipleItemsBottomSheet extends SelectionBottomSheet {
|
||||||
this.selectionUpdateListener = selectionUpdateListener;
|
this.selectionUpdateListener = selectionUpdateListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity,
|
public static MultipleSelectionBottomSheet showInstance(@NonNull AppCompatActivity activity,
|
||||||
@NonNull List<SelectableItem> items,
|
@NonNull List<SelectableItem> items,
|
||||||
@Nullable List<SelectableItem> selected,
|
@Nullable List<SelectableItem> selected,
|
||||||
boolean usedOnMap) {
|
boolean usedOnMap) {
|
||||||
SelectMultipleItemsBottomSheet fragment = new SelectMultipleItemsBottomSheet();
|
MultipleSelectionBottomSheet fragment = new MultipleSelectionBottomSheet();
|
||||||
fragment.setUsedOnMap(usedOnMap);
|
fragment.setUsedOnMap(usedOnMap);
|
||||||
fragment.setItems(items);
|
fragment.setItems(items);
|
||||||
fragment.setSelectedItems(selected);
|
fragment.setSelectedItems(selected);
|
|
@ -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<SelectableItem> items,
|
||||||
|
@Nullable List<SelectableItem> selected,
|
||||||
|
@NonNull List<RadioItem> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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<RadioItem> 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<RadioItem> 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<SelectableItem> getSelection() {
|
|
||||||
return Collections.singletonList(previewItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean shouldShowDivider() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SelectModeBottomSheet showInstance(@NonNull AppCompatActivity activity,
|
|
||||||
@NonNull SelectableItem previewItem,
|
|
||||||
@NonNull List<RadioItem> 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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<RadioItem> 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<RadioItem> modes) {
|
|
||||||
this.modes = modes;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSelectedMode(@NonNull RadioItem mode) {
|
|
||||||
radioGroup.setSelectedItem(mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SelectMultipleWithModeBottomSheet showInstance(@NonNull AppCompatActivity activity,
|
|
||||||
@NonNull List<SelectableItem> items,
|
|
||||||
@Nullable List<SelectableItem> selected,
|
|
||||||
@NonNull List<RadioItem> 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,10 +1,12 @@
|
||||||
package net.osmand.plus.base;
|
package net.osmand.plus.base;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
|
import android.widget.LinearLayout.LayoutParams;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
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.BaseBottomSheetItem;
|
||||||
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
|
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
|
||||||
import net.osmand.plus.base.bottomsheetmenu.simpleitems.SimpleDividerItem;
|
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;
|
||||||
|
import net.osmand.plus.widgets.MultiStateToggleButton.RadioItem;
|
||||||
|
import net.osmand.util.Algorithms;
|
||||||
import net.osmand.view.ThreeStateCheckbox;
|
import net.osmand.view.ThreeStateCheckbox;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment {
|
public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment {
|
||||||
|
|
||||||
protected OsmandApplication app;
|
protected OsmandApplication app;
|
||||||
|
protected LayoutInflater inflater;
|
||||||
protected UiUtilities uiUtilities;
|
protected UiUtilities uiUtilities;
|
||||||
|
|
||||||
protected TextView title;
|
protected TextView title;
|
||||||
|
@ -45,6 +54,10 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment
|
||||||
private OnUiInitializedListener uiInitializedListener;
|
private OnUiInitializedListener uiInitializedListener;
|
||||||
private OnApplySelectionListener applySelectionListener;
|
private OnApplySelectionListener applySelectionListener;
|
||||||
|
|
||||||
|
protected List<SelectableItem> allItems = new ArrayList<>();
|
||||||
|
protected Map<SelectableItem, View> listViews = new HashMap<>();
|
||||||
|
private List<RadioItem> modes;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
|
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) {
|
public void createMenuItems(Bundle savedInstanceState) {
|
||||||
app = requiredMyApplication();
|
app = requiredMyApplication();
|
||||||
uiUtilities = app.getUIUtilities();
|
uiUtilities = app.getUIUtilities();
|
||||||
|
inflater = UiUtilities.getInflater(requireContext(), nightMode);
|
||||||
activeColorRes = nightMode ? R.color.icon_color_active_dark : R.color.icon_color_active_light;
|
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;
|
secondaryColorRes = nightMode ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light;
|
||||||
|
|
||||||
items.add(createHeaderUi());
|
items.add(createHeaderView());
|
||||||
if (shouldShowDivider()) {
|
if (shouldShowDivider()) {
|
||||||
items.add(new SimpleDividerItem(app));
|
items.add(new SimpleDividerItem(app));
|
||||||
}
|
}
|
||||||
createSelectionUi();
|
items.add(createSelectionView());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private BaseBottomSheetItem createHeaderView() {
|
||||||
public void onPause() {
|
View view = inflater.inflate(R.layout.settings_group_title, null);
|
||||||
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);
|
|
||||||
|
|
||||||
title = view.findViewById(R.id.title);
|
title = view.findViewById(R.id.title);
|
||||||
titleDescription = view.findViewById(R.id.title_description);
|
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);
|
checkBoxTitle = view.findViewById(R.id.check_box_title);
|
||||||
checkBox = view.findViewById(R.id.check_box);
|
checkBox = view.findViewById(R.id.check_box);
|
||||||
|
|
||||||
initHeaderUi();
|
if (modes != null) {
|
||||||
|
radioGroup.setItems(modes);
|
||||||
|
}
|
||||||
|
|
||||||
return new SimpleBottomSheetItem.Builder().setCustomView(view).create();
|
return new SimpleBottomSheetItem.Builder().setCustomView(view).create();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void createSelectionUi();
|
private BaseBottomSheetItem createSelectionView() {
|
||||||
|
Context themedCtx = UiUtilities.getThemedContext(requireContext(), nightMode);
|
||||||
protected abstract void initHeaderUi();
|
listContainer = new LinearLayout(themedCtx);
|
||||||
|
listContainer.setLayoutParams(new LayoutParams(
|
||||||
protected boolean shouldShowDivider() {
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
return true;
|
ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||||
}
|
listContainer.setOrientation(LinearLayout.VERTICAL);
|
||||||
|
fillInSelectionList();
|
||||||
@Override
|
return new SimpleBottomSheetItem.Builder().setCustomView(listContainer).create();
|
||||||
protected void setupRightButton() {
|
|
||||||
super.setupRightButton();
|
|
||||||
applyButtonTitle = rightButton.findViewById(R.id.button_text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTitle(@NonNull String title) {
|
public void setTitle(@NonNull String title) {
|
||||||
|
@ -128,20 +133,23 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment
|
||||||
applyButtonTitle.setText(title);
|
applyButtonTitle.setText(title);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void setModes(@NonNull List<RadioItem> modes) {
|
||||||
protected void onRightBottomButtonClick() {
|
this.modes = modes;
|
||||||
if (applySelectionListener != null) {
|
if (radioGroup != null) {
|
||||||
applySelectionListener.onSelectionApplied(getSelection());
|
radioGroup.setItems(modes);
|
||||||
}
|
}
|
||||||
dismiss();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
public void setSelectedMode(@NonNull RadioItem mode) {
|
||||||
public abstract List<SelectableItem> getSelection();
|
radioGroup.setSelectedItem(mode);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
public void setItems(List<SelectableItem> allItems) {
|
||||||
protected int getRightBottomButtonTextId() {
|
if (!Algorithms.isEmpty(allItems)) {
|
||||||
return R.string.shared_string_apply;
|
this.allItems.clear();
|
||||||
|
this.allItems.addAll(allItems);
|
||||||
|
fillInSelectionList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUiInitializedListener(OnUiInitializedListener uiInitializedListener) {
|
public void setUiInitializedListener(OnUiInitializedListener uiInitializedListener) {
|
||||||
|
@ -152,17 +160,81 @@ public abstract class SelectionBottomSheet extends MenuBottomSheetDialogFragment
|
||||||
this.applySelectionListener = onApplySelectionListener;
|
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<SelectableItem> getSelectedItems();
|
||||||
|
|
||||||
|
protected abstract void updateItemView(SelectableItem item, View view);
|
||||||
|
|
||||||
|
protected abstract int getItemLayoutId();
|
||||||
|
|
||||||
|
protected abstract boolean shouldShowDivider();
|
||||||
|
|
||||||
protected void notifyUiInitialized() {
|
protected void notifyUiInitialized() {
|
||||||
if (uiInitializedListener != null) {
|
if (uiInitializedListener != null) {
|
||||||
uiInitializedListener.onUiInitialized();
|
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
|
@Override
|
||||||
protected boolean useVerticalButtons() {
|
protected boolean useVerticalButtons() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
if (requireActivity().isChangingConfigurations()) {
|
||||||
|
dismiss();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public interface OnUiInitializedListener {
|
public interface OnUiInitializedListener {
|
||||||
void onUiInitialized();
|
void onUiInitialized();
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,10 +8,10 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||||
import net.osmand.map.OsmandRegions;
|
import net.osmand.map.OsmandRegions;
|
||||||
import net.osmand.plus.OsmandApplication;
|
import net.osmand.plus.OsmandApplication;
|
||||||
import net.osmand.plus.R;
|
import net.osmand.plus.R;
|
||||||
import net.osmand.plus.base.SelectMultipleItemsBottomSheet;
|
import net.osmand.plus.base.MultipleSelectionBottomSheet;
|
||||||
import net.osmand.plus.base.SelectMultipleItemsBottomSheet.SelectionUpdateListener;
|
import net.osmand.plus.base.MultipleSelectionBottomSheet.SelectionUpdateListener;
|
||||||
import net.osmand.plus.base.SelectModeBottomSheet;
|
import net.osmand.plus.base.ModeSelectionBottomSheet;
|
||||||
import net.osmand.plus.base.SelectMultipleWithModeBottomSheet;
|
import net.osmand.plus.base.MultipleWithModeBottomSheet;
|
||||||
import net.osmand.plus.base.SelectionBottomSheet;
|
import net.osmand.plus.base.SelectionBottomSheet;
|
||||||
import net.osmand.plus.base.SelectionBottomSheet.OnApplySelectionListener;
|
import net.osmand.plus.base.SelectionBottomSheet.OnApplySelectionListener;
|
||||||
import net.osmand.plus.base.SelectionBottomSheet.OnUiInitializedListener;
|
import net.osmand.plus.base.SelectionBottomSheet.OnUiInitializedListener;
|
||||||
|
@ -96,8 +96,8 @@ public class SelectIndexesUiHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final SelectMultipleItemsBottomSheet dialog =
|
final MultipleSelectionBottomSheet dialog =
|
||||||
SelectMultipleItemsBottomSheet.showInstance(activity, allItems, selectedItems, true);
|
MultipleSelectionBottomSheet.showInstance(activity, allItems, selectedItems, true);
|
||||||
|
|
||||||
dialog.setUiInitializedListener(new OnUiInitializedListener() {
|
dialog.setUiInitializedListener(new OnUiInitializedListener() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -131,7 +131,7 @@ public class SelectIndexesUiHelper {
|
||||||
radioItems.add(meters);
|
radioItems.add(meters);
|
||||||
radioItems.add(feet);
|
radioItems.add(feet);
|
||||||
|
|
||||||
dialog = SelectModeBottomSheet.showInstance(activity,
|
dialog = ModeSelectionBottomSheet.showInstance(activity,
|
||||||
baseSRTM ? meterItem : feetItem, radioItems, true);
|
baseSRTM ? meterItem : feetItem, radioItems, true);
|
||||||
|
|
||||||
final RadioItem initRadio = baseSRTM ? meters : feet;
|
final RadioItem initRadio = baseSRTM ? meters : feet;
|
||||||
|
@ -139,7 +139,7 @@ public class SelectIndexesUiHelper {
|
||||||
dialog.setUiInitializedListener(new OnUiInitializedListener() {
|
dialog.setUiInitializedListener(new OnUiInitializedListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onUiInitialized() {
|
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.setTitle(app.getString(R.string.srtm_unit_format));
|
||||||
dialog.setPrimaryDescription(app.getString(R.string.srtm_download_single_help_message));
|
dialog.setPrimaryDescription(app.getString(R.string.srtm_download_single_help_message));
|
||||||
updateSize(dialog, false);
|
updateSize(dialog, false);
|
||||||
|
@ -169,7 +169,7 @@ public class SelectIndexesUiHelper {
|
||||||
radioItem.setOnClickListener(new OnRadioItemClickListener() {
|
radioItem.setOnClickListener(new OnRadioItemClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onRadioItemClick(RadioItem radioItem, View view) {
|
public boolean onRadioItemClick(RadioItem radioItem, View view) {
|
||||||
((SelectModeBottomSheet)dialog).setPreviewItem(selectableItem);
|
((ModeSelectionBottomSheet)dialog).setItem(selectableItem);
|
||||||
updateSize(dialog, false);
|
updateSize(dialog, false);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -210,7 +210,7 @@ public class SelectIndexesUiHelper {
|
||||||
radioItems.add(meters);
|
radioItems.add(meters);
|
||||||
radioItems.add(feet);
|
radioItems.add(feet);
|
||||||
|
|
||||||
final SelectMultipleWithModeBottomSheet dialog = SelectMultipleWithModeBottomSheet.showInstance(
|
final MultipleWithModeBottomSheet dialog = MultipleWithModeBottomSheet.showInstance(
|
||||||
activity, itemsList, selectedItems, radioItems, true);
|
activity, itemsList, selectedItems, radioItems, true);
|
||||||
|
|
||||||
meters.setOnClickListener(new OnRadioItemClickListener() {
|
meters.setOnClickListener(new OnRadioItemClickListener() {
|
||||||
|
@ -265,7 +265,7 @@ public class SelectIndexesUiHelper {
|
||||||
|
|
||||||
private void updateSize(SelectionBottomSheet dialog,
|
private void updateSize(SelectionBottomSheet dialog,
|
||||||
boolean updateDescription) {
|
boolean updateDescription) {
|
||||||
double sizeToDownload = getDownloadSizeInMb(dialog.getSelection());
|
double sizeToDownload = getDownloadSizeInMb(dialog.getSelectedItems());
|
||||||
String size = DownloadItem.getFormattedMb(app, sizeToDownload);
|
String size = DownloadItem.getFormattedMb(app, sizeToDownload);
|
||||||
if (updateDescription) {
|
if (updateDescription) {
|
||||||
String total = app.getString(R.string.shared_string_total);
|
String total = app.getString(R.string.shared_string_total);
|
||||||
|
|
Loading…
Reference in a new issue