Sort map styles

This commit is contained in:
Alex Sytnyk 2018-05-18 12:48:25 +03:00
parent c457eeea12
commit 22ea4131d8

View file

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.ColorInt; import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.widget.CompoundButtonCompat; import android.support.v4.widget.CompoundButtonCompat;
import android.support.v4.widget.NestedScrollView; import android.support.v4.widget.NestedScrollView;
@ -18,6 +19,8 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.Collator;
import net.osmand.OsmAndCollator;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R; import net.osmand.plus.R;
@ -33,7 +36,10 @@ import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.render.RenderingRulesStorage; import net.osmand.render.RenderingRulesStorage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class SelectMapStyleBottomSheetDialogFragment extends MenuBottomSheetDialogFragment { public class SelectMapStyleBottomSheetDialogFragment extends MenuBottomSheetDialogFragment {
@ -49,7 +55,7 @@ public class SelectMapStyleBottomSheetDialogFragment extends MenuBottomSheetDial
private View.OnClickListener onStyleClickListener; private View.OnClickListener onStyleClickListener;
private ColorStateList rbColorList; private ColorStateList rbColorList;
private List<String> styles; private TreeMap<String, String> stylesMap;
private String selectedStyle; private String selectedStyle;
@Override @Override
@ -59,13 +65,9 @@ public class SelectMapStyleBottomSheetDialogFragment extends MenuBottomSheetDial
return; return;
} }
RendererRegistry rendererRegistry = getMyApplication().getRendererRegistry(); stylesMap = generateStylesMap(context);
styles = new ArrayList<>(rendererRegistry.getRendererNames());
if (OsmandPlugin.getEnabledPlugin(NauticalMapsPlugin.class) == null) {
styles.remove(RendererRegistry.NAUTICAL_RENDER);
}
if (savedInstanceState == null) { if (savedInstanceState == null) {
selectedStyle = rendererRegistry.getCurrentSelectedRenderer().getName(); selectedStyle = getMyApplication().getRendererRegistry().getCurrentSelectedRenderer().getName();
} else { } else {
selectedStyle = savedInstanceState.getString(SELECTED_STYLE_KEY); selectedStyle = savedInstanceState.getString(SELECTED_STYLE_KEY);
} }
@ -99,7 +101,7 @@ public class SelectMapStyleBottomSheetDialogFragment extends MenuBottomSheetDial
stylesContainer.setLayoutParams((new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT))); stylesContainer.setLayoutParams((new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)));
stylesContainer.setOrientation(LinearLayout.VERTICAL); stylesContainer.setOrientation(LinearLayout.VERTICAL);
stylesContainer.setPadding(0, getResources().getDimensionPixelSize(R.dimen.bottom_sheet_content_padding_small), 0, 0); stylesContainer.setPadding(0, getResources().getDimensionPixelSize(R.dimen.bottom_sheet_content_padding_small), 0, 0);
for (int i = 0; i < styles.size(); i++) { for (int i = 0; i < stylesMap.size(); i++) {
LayoutInflater.from(new ContextThemeWrapper(context, themeRes)) LayoutInflater.from(new ContextThemeWrapper(context, themeRes))
.inflate(R.layout.bottom_sheet_item_with_radio_btn_left, stylesContainer, true); .inflate(R.layout.bottom_sheet_item_with_radio_btn_left, stylesContainer, true);
} }
@ -154,31 +156,61 @@ public class SelectMapStyleBottomSheetDialogFragment extends MenuBottomSheetDial
return null; return null;
} }
@NonNull
private TreeMap<String, String> generateStylesMap(Context context) {
final Collator collator = OsmAndCollator.primaryCollator();
TreeMap<String, String> res = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String string1, String string2) {
if (string1.equals(RendererRegistry.DEFAULT_RENDER)) {
return -1;
}
if (string2.equals(RendererRegistry.DEFAULT_RENDER)) {
return 1;
}
return collator.compare(string1, string2);
}
});
List<String> names = new ArrayList<>(getMyApplication().getRendererRegistry().getRendererNames());
if (OsmandPlugin.getEnabledPlugin(NauticalMapsPlugin.class) == null) {
names.remove(RendererRegistry.NAUTICAL_RENDER);
}
for (String name : names) {
String translation = RendererRegistry.getTranslatedRendererName(context, name);
if (translation == null) {
translation = name.replace('_', ' ').replace('-', ' ');
}
res.put(translation, name);
}
return res;
}
@SuppressWarnings("RedundantCast") @SuppressWarnings("RedundantCast")
private void populateStylesList() { private void populateStylesList() {
Context context = getContext(); Context context = getContext();
if (context == null) { if (context == null) {
return; return;
} }
for (int i = 0; i < styles.size(); i++) { int counter = 0;
String style = styles.get(i); for (Map.Entry<String, String> entry : stylesMap.entrySet()) {
boolean selected = style.equals(selectedStyle); String name = entry.getValue();
String title = RendererRegistry.getTranslatedRendererName(context, style); boolean selected = name.equals(selectedStyle);
if (title == null) {
title = style.replace('_', ' ').replace('-', ' ');
}
View view = stylesContainer.getChildAt(i); View view = stylesContainer.getChildAt(counter);
view.setTag(style); view.setTag(name);
view.setOnClickListener(getOnStyleClickListener()); view.setOnClickListener(getOnStyleClickListener());
TextView titleTv = (TextView) view.findViewById(R.id.title); TextView titleTv = (TextView) view.findViewById(R.id.title);
titleTv.setText(title); titleTv.setText(entry.getKey());
titleTv.setTextColor(getStyleTitleColor(selected)); titleTv.setTextColor(getStyleTitleColor(selected));
RadioButton rb = (RadioButton) view.findViewById(R.id.compound_button); RadioButton rb = (RadioButton) view.findViewById(R.id.compound_button);
rb.setChecked(selected); rb.setChecked(selected);
CompoundButtonCompat.setButtonTintList(rb, rbColorList); CompoundButtonCompat.setButtonTintList(rb, rbColorList);
counter++;
} }
} }