refactoring/switchable quick actions skeletons

This commit is contained in:
Rosty 2017-01-05 19:01:08 +02:00
parent 7fad600bc4
commit f2cd5d945a
4 changed files with 318 additions and 86 deletions

View file

@ -19,16 +19,18 @@
android:orientation="vertical">
<TextView
android:id="@+id/textDscrTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Map styles"
android:text="@string/quick_action_map_styles"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/default_sub_text_size"/>
<TextView
android:id="@+id/textDscrHint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/quic_action_map_style_doscrp"
android:text="@string/quick_action_map_style_discrp"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_sub_text_size" />
@ -46,7 +48,7 @@
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnAddStyle"
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_transparent"

View file

@ -2525,7 +2525,20 @@ If you need help with OsmAnd application, please contact our support team: suppo
<string name="quick_action_gpx_category_discr">You can select a category to save GPX waypoint point.</string>
<string name="quick_action_poi_list">POI list</string>
<string name="quick_action_sh_poi_discrp">You can add few POI categories to display on the map.</string>
<string name="quic_action_map_style_doscrp">Tap on the action button will toggle the map style, from the list order.</string>
<string name="quick_action_map_style_discrp">Tap on the action button will toggle the map style, from the list order.</string>
<string name="quick_action_map_style_action">Add map style</string>
<string name="quick_action_empty_param_error">Parameters should not be empty</string>
<string name="quick_action_map_styles">Map styles</string>
<string name="quick_action_map_overlay">Change map overlay</string>
<string name="quick_action_map_overlay_title">Map overlays</string>
<string name="quick_action_map_overlay_dscr">Tap on the action button will toggle the map overlay, from the list order.</string>
<string name="quick_action_map_overlay_action">Add Overlay</string>
<string name="quick_action_map_underlay">Change map underlay</string>
<string name="quick_action_map_underlay_title">Map underlays</string>
<string name="quick_action_map_underlay_dscr">Tap on the action button will toggle the map underlay, from the list order.</string>
<string name="quick_action_map_underlay_action">Add Underlay</string>
<string name="quick_action_map_source">Change map source</string>
<string name="quick_action_map_source_title">Map sources</string>
<string name="quick_action_map_source_dscr">Tap on the action button will toggle the map source, from the list order.</string>
<string name="quick_action_map_source_action">Add Source</string>
</resources>

View file

@ -3,8 +3,10 @@ package net.osmand.plus.quickaction;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.support.annotation.StringRes;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
@ -34,7 +36,6 @@ import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.osm.AbstractPoiType;
import net.osmand.osm.MapPoiTypes;
import net.osmand.osm.PoiFilter;
import net.osmand.osm.PoiType;
import net.osmand.osm.edit.Node;
import net.osmand.osm.edit.OSMSettings;
@ -1566,14 +1567,12 @@ public class QuickActionFactory {
}
}
public static class MapStyleAction extends QuickAction {
public static class MapStyleAction extends SwitchableAction {
public static final int TYPE = 14;
private static String KEY_STYLES = "styles";
private transient EditText title;
protected MapStyleAction() {
id = System.currentTimeMillis();
type = TYPE;
@ -1636,49 +1635,30 @@ public class QuickActionFactory {
return filtered;
}
@Override
public void setAutoGeneratedTitle(EditText title) {
this.title = title;
}
private String getTitle(List<String> filters) {
if (filters.isEmpty()) return "";
return filters.size() > 1
? filters.get(0) + " +" + (filters.size() - 1)
: filters.get(0);
}
@Override
public void drawUI(ViewGroup parent, final MapActivity activity) {
protected int getAddBtnText() {
return R.string.quick_action_map_style_action;
}
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.quick_action_map_style, parent, false);
@Override
protected int getDiscrHint() {
return R.string.quick_action_map_style_discrp;
}
final RecyclerView list = (RecyclerView) view.findViewById(R.id.list);
@Override
protected int getDiscrTitle() {
return R.string.quick_action_map_styles;
}
final QuickActionItemTouchHelperCallback touchHelperCallback = new QuickActionItemTouchHelperCallback();
final ItemTouchHelper touchHelper = new ItemTouchHelper(touchHelperCallback);
@Override
protected String getListKey() {
return KEY_STYLES;
}
final Adapter adapter = new Adapter(new QuickActionListFragment.OnStartDragListener() {
@Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
touchHelper.startDrag(viewHolder);
}
});
touchHelperCallback.setItemMoveCallback(adapter);
touchHelper.attachToRecyclerView(list);
if (!getParams().isEmpty()){
adapter.addItems(loadMapStyles());
}
list.setAdapter(adapter);
view.findViewById(R.id.btnAddStyle).setOnClickListener(new View.OnClickListener() {
@Override
protected View.OnClickListener getOnAddBtnClickListener(final MapActivity activity, final Adapter adapter) {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -1712,15 +1692,7 @@ public class QuickActionFactory {
if (loaded != null) {
String oldTitle = getTitle(adapter.itemsList);
adapter.addItem(renderer);
if (oldTitle.equals(title.getText().toString()) || title.getText().toString().equals(getName(activity))) {
String newTitle = getTitle(adapter.itemsList);
title.setText(newTitle);
}
adapter.addItem(renderer, activity);
}
dialogInterface.dismiss();
@ -1730,12 +1702,240 @@ public class QuickActionFactory {
bld.setNegativeButton(R.string.shared_string_dismiss, null);
bld.show();
}
};
}
}
public static class MapOverlayAction extends SwitchableAction {
public static final int TYPE = 15;
private static String KEY_OVERLAYS = "overlays";
protected MapOverlayAction() {
id = System.currentTimeMillis();
type = TYPE;
nameRes = R.string.quick_action_map_overlay;
iconRes = R.drawable.ic_layer_top_dark;
}
public MapOverlayAction(QuickAction quickAction) {
super(quickAction);
}
@Override
public void execute(MapActivity activity) {
}
@Override
protected int getAddBtnText() {
return R.string.quick_action_map_overlay_action;
}
@Override
protected int getDiscrHint() {
return R.string.quick_action_map_overlay_dscr;
}
@Override
protected int getDiscrTitle() {
return R.string.quick_action_map_overlay_title;
}
@Override
protected String getListKey() {
return KEY_OVERLAYS;
}
@Override
protected View.OnClickListener getOnAddBtnClickListener(final MapActivity activity, final Adapter adapter) {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
}
};
}
}
public static class MapUnderlayAction extends SwitchableAction {
public static final int TYPE = 16;
private static String KEY_UNDERLAYS = "underlays";
protected MapUnderlayAction() {
id = System.currentTimeMillis();
type = TYPE;
nameRes = R.string.quick_action_map_underlay;
iconRes = R.drawable.ic_layer_bottom_dark;
}
public MapUnderlayAction(QuickAction quickAction) {
super(quickAction);
}
@Override
public void execute(MapActivity activity) {
}
@Override
protected int getAddBtnText() {
return R.string.quick_action_map_underlay_action;
}
@Override
protected int getDiscrHint() {
return R.string.quick_action_map_underlay_dscr;
}
@Override
protected int getDiscrTitle() {
return R.string.quick_action_map_underlay_title;
}
@Override
protected String getListKey() {
return KEY_UNDERLAYS;
}
@Override
protected View.OnClickListener getOnAddBtnClickListener(final MapActivity activity, final Adapter adapter) {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
}
};
}
}
public static class MapSourceAction extends SwitchableAction {
public static final int TYPE = 17;
private static String KEY_SOURCE = "source";
protected MapSourceAction() {
id = System.currentTimeMillis();
type = TYPE;
nameRes = R.string.quick_action_map_source;
iconRes = R.drawable.ic_map;
}
public MapSourceAction(QuickAction quickAction) {
super(quickAction);
}
@Override
public void execute(MapActivity activity) {
}
@Override
protected int getAddBtnText() {
return R.string.quick_action_map_source_action;
}
@Override
protected int getDiscrHint() {
return R.string.quick_action_map_source_dscr;
}
@Override
protected int getDiscrTitle() {
return R.string.quick_action_map_source_title;
}
@Override
protected String getListKey() {
return KEY_SOURCE;
}
@Override
protected View.OnClickListener getOnAddBtnClickListener(final MapActivity activity, final Adapter adapter) {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
}
};
}
}
protected static abstract class SwitchableAction extends QuickAction{
private transient EditText title;
public SwitchableAction() {
}
public SwitchableAction(QuickAction quickAction) {
super(quickAction);
}
@Override
public void setAutoGeneratedTitle(EditText title) {
this.title = title;
}
private String getTitle(List<String> filters) {
if (filters.isEmpty()) return "";
return filters.size() > 1
? filters.get(0) + " +" + (filters.size() - 1)
: filters.get(0);
}
@Override
public void drawUI(ViewGroup parent, final MapActivity activity) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.quick_action_switchable_action, parent, false);
final RecyclerView list = (RecyclerView) view.findViewById(R.id.list);
final QuickActionItemTouchHelperCallback touchHelperCallback = new QuickActionItemTouchHelperCallback();
final ItemTouchHelper touchHelper = new ItemTouchHelper(touchHelperCallback);
final Adapter adapter = new Adapter(new QuickActionListFragment.OnStartDragListener() {
@Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
touchHelper.startDrag(viewHolder);
}
});
touchHelperCallback.setItemMoveCallback(adapter);
touchHelper.attachToRecyclerView(list);
if (!getParams().isEmpty()){
adapter.addItems(loadMapStyles());
}
list.setAdapter(adapter);
TextView dscrTitle = (TextView) view.findViewById(R.id.textDscrTitle);
TextView dscrHint = (TextView) view.findViewById(R.id.textDscrHint);
Button addBtn = (Button) view.findViewById(R.id.btnAdd);
dscrTitle.setText(getDiscrTitle());
dscrHint.setText(getDiscrHint());
addBtn.setText(getAddBtnText());
addBtn.setOnClickListener(getOnAddBtnClickListener(activity, adapter));
parent.addView(view);
}
public class Adapter extends RecyclerView.Adapter<Adapter.ItemHolder> implements QuickActionItemTouchHelperCallback.OnItemMoveCallback {
@Override
public boolean fillParams(View root, MapActivity activity) {
return !getParams().isEmpty() && (getParams().get(getListKey()) != null || !getParams().get(getListKey()).isEmpty());
}
protected class Adapter extends RecyclerView.Adapter<Adapter.ItemHolder> implements QuickActionItemTouchHelperCallback.OnItemMoveCallback {
private List<String> itemsList = new ArrayList<>();
private final QuickActionListFragment.OnStartDragListener onStartDragListener;
@ -1748,40 +1948,41 @@ public class QuickActionFactory {
@Override
public Adapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new ItemHolder(inflater.inflate(R.layout.quick_action_map_style_item, parent, false));
return new Adapter.ItemHolder(inflater.inflate(R.layout.quick_action_switchable_item, parent, false));
}
@Override
public void onBindViewHolder(final Adapter.ItemHolder holder, final int position) {
final String item = itemsList.get(position);
holder.title.setText(item);
holder.title.setText(item);
holder.handleView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) ==
MotionEvent.ACTION_DOWN) {
onStartDragListener.onStartDrag(holder);
}
return false;
holder.handleView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) ==
MotionEvent.ACTION_DOWN) {
onStartDragListener.onStartDrag(holder);
}
});
holder.closeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
return false;
}
});
String oldTitle = getTitle(itemsList);
holder.closeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteItem(position);
String oldTitle = getTitle(itemsList);
if (oldTitle.equals(title.getText().toString()) || title.getText().toString().equals(getName(holder.handleView.getContext()))) {
deleteItem(position);
String newTitle = getTitle(itemsList);
title.setText(newTitle);
}
if (oldTitle.equals(title.getText().toString()) || title.getText().toString().equals(getName(holder.handleView.getContext()))) {
String newTitle = getTitle(itemsList);
title.setText(newTitle);
}
});
}
});
}
@Override
@ -1811,15 +2012,23 @@ public class QuickActionFactory {
}
}
public void addItem(String item) {
public void addItem(String item, Context context) {
if (!itemsList.contains(item)) {
String oldTitle = getTitle(itemsList);
int oldSize = itemsList.size();
itemsList.add(item);
saveMapStyles(itemsList);
notifyItemRangeInserted(oldSize, itemsList.size() - oldSize);
if (oldTitle.equals(title.getText().toString()) || title.getText().toString().equals(getName(context))) {
String newTitle = getTitle(itemsList);
title.setText(newTitle);
}
}
}
@ -1829,21 +2038,28 @@ public class QuickActionFactory {
int selectedPosition = viewHolder.getAdapterPosition();
int targetPosition = target.getAdapterPosition();
if (selectedPosition < 0 || targetPosition < 0)
if (selectedPosition < 0 || targetPosition < 0) {
return false;
}
String oldTitle = getTitle(itemsList);
Collections.swap(itemsList, selectedPosition, targetPosition);
if (selectedPosition - targetPosition < -1) {
notifyItemMoved(selectedPosition, targetPosition);
notifyItemMoved(targetPosition - 1, selectedPosition);
} else if (selectedPosition - targetPosition > 1) {
notifyItemMoved(selectedPosition, targetPosition);
notifyItemMoved(targetPosition + 1, selectedPosition);
} else {
notifyItemMoved(selectedPosition, targetPosition);
}
notifyItemChanged(selectedPosition);
notifyItemChanged(targetPosition);
@ -1876,15 +2092,15 @@ public class QuickActionFactory {
}
}
public void saveMapStyles(List<String> styles) {
getParams().put(KEY_STYLES, TextUtils.join(",", styles));
protected void saveMapStyles(List<String> styles) {
getParams().put(getListKey(), TextUtils.join(",", styles));
}
private List<String> loadMapStyles() {
protected List<String> loadMapStyles() {
List<String> styles = new ArrayList<>();
String filtersId = getParams().get(KEY_STYLES);
String filtersId = getParams().get(getListKey());
if (filtersId != null && !filtersId.trim().isEmpty()) {
Collections.addAll(styles, filtersId.split(","));
@ -1893,9 +2109,10 @@ public class QuickActionFactory {
return styles;
}
@Override
public boolean fillParams(View root, MapActivity activity) {
return !getParams().isEmpty() && (getParams().get(KEY_STYLES) != null || !getParams().get(KEY_STYLES).isEmpty());
}
protected abstract @StringRes int getAddBtnText();
protected abstract @StringRes int getDiscrHint();
protected abstract @StringRes int getDiscrTitle();
protected abstract String getListKey();
protected abstract View.OnClickListener getOnAddBtnClickListener(MapActivity activity, final Adapter adapter);
}
}