Refactor edit poi

This commit is contained in:
Victor Shcherb 2015-11-12 10:30:43 +01:00
parent 862353b602
commit 053abea161
10 changed files with 94 additions and 75 deletions

View file

@ -28,7 +28,7 @@ public class Amenity extends MapObject {
private static final long serialVersionUID = 132083949926339552L; private static final long serialVersionUID = 132083949926339552L;
private String subType; private String subType;
private transient PoiCategory type; private PoiCategory type;
// duplicate for fast access // duplicate for fast access
private String openingHours; private String openingHours;
private Map<String, String> additionalInfo; private Map<String, String> additionalInfo;

View file

@ -18,7 +18,7 @@ import net.osmand.util.Algorithms;
import net.sf.junidecode.Junidecode; import net.sf.junidecode.Junidecode;
public abstract class MapObject implements Comparable<MapObject>, Serializable { public abstract class MapObject implements Comparable<MapObject> {
protected String name = null; protected String name = null;
protected String enName = null; protected String enName = null;
protected Map<String, String> names = null; protected Map<String, String> names = null;

View file

@ -14,7 +14,7 @@ public abstract class AbstractPoiType {
private boolean topVisible; private boolean topVisible;
private String lang; private String lang;
private AbstractPoiType baseLangType; private AbstractPoiType baseLangType;
private boolean notEditableOsm;
public AbstractPoiType(String keyName, MapPoiTypes registry) { public AbstractPoiType(String keyName, MapPoiTypes registry) {
this.keyName = keyName; this.keyName = keyName;
@ -76,6 +76,14 @@ public abstract class AbstractPoiType {
return poiAdditionals; return poiAdditionals;
} }
public boolean isNotEditableOsm() {
return notEditableOsm;
}
public void setNotEditableOsm(boolean notEditableOsm) {
this.notEditableOsm = notEditableOsm;
}
public abstract Map<PoiCategory, LinkedHashSet<String>> putTypes(Map<PoiCategory, LinkedHashSet<String>> acceptedTypes); public abstract Map<PoiCategory, LinkedHashSet<String>> putTypes(Map<PoiCategory, LinkedHashSet<String>> acceptedTypes);

View file

@ -274,6 +274,7 @@ public class MapPoiTypes {
if (name.equals("poi_category")) { if (name.equals("poi_category")) {
lastCategory = new PoiCategory(this, parser.getAttributeValue("", "name"), categories.size()); lastCategory = new PoiCategory(this, parser.getAttributeValue("", "name"), categories.size());
lastCategory.setTopVisible(Boolean.parseBoolean(parser.getAttributeValue("", "top"))); lastCategory.setTopVisible(Boolean.parseBoolean(parser.getAttributeValue("", "top")));
lastCategory.setNotEditableOsm("true".equals(parser.getAttributeValue("", "no_edit")));
categories.add(lastCategory); categories.add(lastCategory);
} else if (name.equals("poi_filter")) { } else if (name.equals("poi_filter")) {
PoiFilter tp = new PoiFilter(this, lastCategory, parser.getAttributeValue("", "name")); PoiFilter tp = new PoiFilter(this, lastCategory, parser.getAttributeValue("", "name"));

View file

@ -12,7 +12,7 @@ public class PoiType extends AbstractPoiType {
private String osmTag2; private String osmTag2;
private String osmValue; private String osmValue;
private String osmValue2; private String osmValue2;
private boolean notEditableOsm;
private String nameTag; private String nameTag;
private boolean text; private boolean text;
@ -145,13 +145,6 @@ public class PoiType extends AbstractPoiType {
this.relation = relation; this.relation = relation;
} }
public boolean isNotEditableOsm() {
return notEditableOsm;
}
public void setNotEditableOsm(boolean notEditableOsm) {
this.notEditableOsm = notEditableOsm;
}
@Override @Override
public String toString() { public String toString() {

View file

@ -110,14 +110,13 @@ public class AdvancedEditPoiFragment extends Fragment
nameTextView.setText(value); nameTextView.setText(value);
} }
if (Algorithms.objectEquals(anyTag, EditPoiData.POI_TYPE_TAG)) { if (Algorithms.objectEquals(anyTag, EditPoiData.POI_TYPE_TAG)) {
String subType = value == null ? "" : value.trim().toLowerCase(); PoiType pt = getData().getPoiTypeDefined();
if (allTranslatedSubTypes.get(subType) != null) { if (pt != null) {
PoiType pt = allTranslatedSubTypes.get(subType);
amenityTagTextView.setText(pt.getOsmTag()); amenityTagTextView.setText(pt.getOsmTag());
amenityTextView.setText(pt.getOsmValue()); amenityTextView.setText(pt.getOsmValue());
} else { } else {
amenityTagTextView.setText(getData().amenity.getType().getDefaultTag()); amenityTagTextView.setText(getData().getPoiCategory().getDefaultTag());
amenityTextView.setText(subType); amenityTextView.setText(getData().getPoiTypeString());
} }
} }
} }

View file

@ -2,10 +2,12 @@ package net.osmand.plus.osmedit;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.data.Amenity; import net.osmand.data.Amenity;
import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiType; import net.osmand.osm.PoiType;
import net.osmand.osm.edit.Node; import net.osmand.osm.edit.Node;
import net.osmand.osm.edit.OSMSettings; import net.osmand.osm.edit.OSMSettings;
import net.osmand.osm.edit.OSMSettings.OSMTagKey; import net.osmand.osm.edit.OSMSettings.OSMTagKey;
import net.osmand.plus.OsmandApplication;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -22,16 +24,42 @@ public class EditPoiData {
private LinkedHashMap<String, String > tagValues = new LinkedHashMap<String, String>(); private LinkedHashMap<String, String > tagValues = new LinkedHashMap<String, String>();
private boolean isInEdit = false; private boolean isInEdit = false;
private Node entity; private Node entity;
public final Amenity amenity;
public static final String POI_TYPE_TAG = "poi_type_tag"; public static final String POI_TYPE_TAG = "poi_type_tag";
private boolean hasChangesBeenMade = false; private boolean hasChangesBeenMade = false;
private Map<String, PoiType> allTranslatedSubTypes;
private PoiCategory category;
private String subtype;
public EditPoiData(Amenity amenity, Node node, Map<String, PoiType> allTranslatedSubTypes) { public EditPoiData(Node node, OsmandApplication app) {
this.amenity = amenity; allTranslatedSubTypes = app.getPoiTypes().getAllTranslatedNames(true);
category = app.getPoiTypes().getOtherPoiCategory();
this.subtype = "";
entity = node; entity = node;
initTags(node, allTranslatedSubTypes); initTags(node);
} }
public Map<String, PoiType> getAllTranslatedSubTypes() {
return allTranslatedSubTypes;
}
public void updateType(PoiCategory type) {
category = type;
subtype = "";
}
public PoiCategory getPoiCategory() {
return category;
}
public PoiType getPoiTypeDefined() {
return allTranslatedSubTypes.get(getPoiTypeString());
}
public String getPoiTypeString() {
return subtype;
}
public Node getEntity() { public Node getEntity() {
return entity; return entity;
@ -42,8 +70,10 @@ public class EditPoiData {
} }
public void updateTags(Map<String, String> mp) { public void updateTags(Map<String, String> mp) {
checkNotInEdit();
this.tagValues.clear(); this.tagValues.clear();
this.tagValues.putAll(mp); this.tagValues.putAll(mp);
retrieveType();
} }
private void tryAddTag(String key, String value) { private void tryAddTag(String key, String value) {
@ -52,18 +82,23 @@ public class EditPoiData {
} }
} }
private void initTags(Node node, Map<String, PoiType> allTranslatedSubTypes) { private void initTags(Node node) {
checkNotInEdit(); checkNotInEdit();
for (String s : node.getTagKeySet()) { for (String s : node.getTagKeySet()) {
tryAddTag(s, node.getTag(s)); tryAddTag(s, node.getTag(s));
} }
String subType = amenity.getSubType(); retrieveType();
String value =""; }
PoiType pt = amenity.getType().getPoiTypeByKeyName(subType);
if (pt != null) { private void retrieveType() {
value = pt.getTranslation(); String tp = tagValues.get(POI_TYPE_TAG);
if(tp != null) {
PoiType pt = allTranslatedSubTypes.get(tp);
if (pt != null) {
subtype = tp;
category = pt.getCategory();
}
} }
tagValues.put(POI_TYPE_TAG, value);
} }
@ -147,4 +182,6 @@ public class EditPoiData {
return hasChangesBeenMade; return hasChangesBeenMade;
} }
} }

View file

@ -44,11 +44,11 @@ import android.widget.ImageButton;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
import net.osmand.data.Amenity; import net.osmand.data.Amenity;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiType; import net.osmand.osm.PoiType;
import net.osmand.osm.edit.EntityInfo; import net.osmand.osm.edit.EntityInfo;
import net.osmand.osm.edit.Node; import net.osmand.osm.edit.Node;
@ -75,7 +75,6 @@ public class EditPoiDialogFragment extends DialogFragment {
private static final Log LOG = PlatformUtil.getLog(EditPoiDialogFragment.class); private static final Log LOG = PlatformUtil.getLog(EditPoiDialogFragment.class);
private static final String KEY_AMENITY_NODE = "key_amenity_node"; private static final String KEY_AMENITY_NODE = "key_amenity_node";
private static final String KEY_AMENITY = "key_amenity";
private static final String TAGS_LIST = "tags_list"; private static final String TAGS_LIST = "tags_list";
private static final String IS_ADDING_POI = "is_adding_poi"; private static final String IS_ADDING_POI = "is_adding_poi";
@ -99,10 +98,7 @@ public class EditPoiDialogFragment extends DialogFragment {
} }
Node node = (Node) getArguments().getSerializable(KEY_AMENITY_NODE); Node node = (Node) getArguments().getSerializable(KEY_AMENITY_NODE);
allTranslatedSubTypes = getMyApplication().getPoiTypes().getAllTranslatedNames(true); editPoiData = new EditPoiData(node, getMyApplication());
Amenity amenity = (Amenity) getArguments().getSerializable(KEY_AMENITY);
editPoiData = new EditPoiData(amenity, node, allTranslatedSubTypes);
} }
@Override @Override
@ -201,7 +197,7 @@ public class EditPoiDialogFragment extends DialogFragment {
poiTypeButton.setOnClickListener(new View.OnClickListener() { poiTypeButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
DialogFragment fragment = PoiTypeDialogFragment.createInstance(editPoiData.amenity); DialogFragment fragment = PoiTypeDialogFragment.createInstance();
fragment.show(getChildFragmentManager(), "PoiTypeDialogFragment"); fragment.show(getChildFragmentManager(), "PoiTypeDialogFragment");
} }
}); });
@ -249,7 +245,7 @@ public class EditPoiDialogFragment extends DialogFragment {
}); });
poiNameEditText.setOnEditorActionListener(mOnEditorActionListener); poiNameEditText.setOnEditorActionListener(mOnEditorActionListener);
poiTypeEditText.setOnEditorActionListener(mOnEditorActionListener); poiTypeEditText.setOnEditorActionListener(mOnEditorActionListener);
poiTypeEditText.setText(editPoiData.amenity.getSubType()); poiTypeEditText.setText(editPoiData.getPoiTypeString());
Button saveButton = (Button) view.findViewById(R.id.saveButton); Button saveButton = (Button) view.findViewById(R.id.saveButton);
saveButton.setText(mOpenstreetmapUtil instanceof OpenstreetmapRemoteUtil saveButton.setText(mOpenstreetmapUtil instanceof OpenstreetmapRemoteUtil
@ -297,7 +293,7 @@ public class EditPoiDialogFragment extends DialogFragment {
node.putTag(poiType.getOsmTag2(), poiType.getOsmValue2()); node.putTag(poiType.getOsmTag2(), poiType.getOsmValue2());
} }
} else { } else {
node.putTag(editPoiData.amenity.getType().getDefaultTag(), tag.getValue()); node.putTag(editPoiData.getPoiCategory().getDefaultTag(), tag.getValue());
} }
} else if (!Algorithms.isEmpty(tag.getKey()) && !Algorithms.isEmpty(tag.getValue())) { } else if (!Algorithms.isEmpty(tag.getKey()) && !Algorithms.isEmpty(tag.getValue())) {
@ -306,15 +302,15 @@ public class EditPoiDialogFragment extends DialogFragment {
} }
commitNode(action, node, mOpenstreetmapUtil.getEntityInfo(), commitNode(action, node, mOpenstreetmapUtil.getEntityInfo(),
"", "",
false,//closeChange.isSelected(), false,
new Runnable() { new Runnable() {
@Override @Override
public void run() { public void run() {
OsmEditingPlugin plugin = OsmandPlugin.getPlugin(OsmEditingPlugin.class); OsmEditingPlugin plugin = OsmandPlugin.getPlugin(OsmEditingPlugin.class);
if (plugin != null) { if (plugin != null && mOpenstreetmapUtil instanceof OpenstreetmapLocalUtil) {
List<OpenstreetmapPoint> points = plugin.getDBPOI().getOpenstreetmapPoints(); List<OpenstreetmapPoint> points = plugin.getDBPOI().getOpenstreetmapPoints();
if (getActivity() instanceof MapActivity && points.size() > 0) {
OsmPoint point = points.get(points.size() - 1); OsmPoint point = points.get(points.size() - 1);
if (getActivity() instanceof MapActivity) {
MapActivity mapActivity = (MapActivity) getActivity(); MapActivity mapActivity = (MapActivity) getActivity();
mapActivity.getContextMenu().showOrUpdate(new LatLon(point.getLatitude(), point.getLongitude()), mapActivity.getContextMenu().showOrUpdate(new LatLon(point.getLatitude(), point.getLongitude()),
plugin.getOsmEditsLayer(mapActivity).getObjectName(point), point); plugin.getOsmEditsLayer(mapActivity).getObjectName(point), point);
@ -408,9 +404,10 @@ public class EditPoiDialogFragment extends DialogFragment {
}.execute(); }.execute();
} }
public void updateType(Amenity amenity) { public void updateType(PoiCategory type) {
poiTypeEditText.setText(amenity.getSubType()); editPoiData.updateType(type);
poiTypeTextInputLayout.setHint(amenity.getType().getTranslation()); poiTypeEditText.setText(editPoiData.getPoiTypeString());
poiTypeTextInputLayout.setHint(editPoiData.getPoiCategory().getTranslation());
setAdapterForPoiTypeEditText(); setAdapterForPoiTypeEditText();
poiTypeEditText.setOnTouchListener(new View.OnTouchListener() { poiTypeEditText.setOnTouchListener(new View.OnTouchListener() {
@Override @Override
@ -421,9 +418,9 @@ public class EditPoiDialogFragment extends DialogFragment {
if (event.getX() >= (editText.getRight() if (event.getX() >= (editText.getRight()
- editText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width() - editText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width()
- editText.getPaddingRight())) { - editText.getPaddingRight())) {
if (editPoiData.amenity.getType() != null) { if (editPoiData.getPoiCategory() != null) {
DialogFragment dialogFragment = DialogFragment dialogFragment =
PoiSubTypeDialogFragment.createInstance(editPoiData.amenity); PoiSubTypeDialogFragment.createInstance(editPoiData.getPoiCategory());
dialogFragment.show(getChildFragmentManager(), "PoiSubTypeDialogFragment"); dialogFragment.show(getChildFragmentManager(), "PoiSubTypeDialogFragment");
} }
@ -497,20 +494,13 @@ public class EditPoiDialogFragment extends DialogFragment {
public static EditPoiDialogFragment createAddPoiInstance(double latitude, double longitude, public static EditPoiDialogFragment createAddPoiInstance(double latitude, double longitude,
OsmandApplication application) { OsmandApplication application) {
Node node = new Node(latitude, longitude, -1); Node node = new Node(latitude, longitude, -1);
Amenity amenity; return createInstance(node, true);
amenity = new Amenity();
amenity.setType(application.getPoiTypes().getOtherPoiCategory());
amenity.setSubType("");
amenity.setAdditionalInfo(OSMSettings.OSMTagKey.OPENING_HOURS.getValue(), "");
return createInstance(node, amenity, true);
} }
public static EditPoiDialogFragment createInstance(Node node, Amenity amenity, public static EditPoiDialogFragment createInstance(Node node, boolean isAddingPoi) {
boolean isAddingPoi) {
EditPoiDialogFragment editPoiDialogFragment = new EditPoiDialogFragment(); EditPoiDialogFragment editPoiDialogFragment = new EditPoiDialogFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putSerializable(KEY_AMENITY_NODE, node); args.putSerializable(KEY_AMENITY_NODE, node);
args.putSerializable(KEY_AMENITY, amenity);
args.putBoolean(IS_ADDING_POI, isAddingPoi); args.putBoolean(IS_ADDING_POI, isAddingPoi);
editPoiDialogFragment.setArguments(args); editPoiDialogFragment.setArguments(args);
return editPoiDialogFragment; return editPoiDialogFragment;
@ -537,7 +527,7 @@ public class EditPoiDialogFragment extends DialogFragment {
protected void onPostExecute(Node n) { protected void onPostExecute(Node n) {
if (n != null) { if (n != null) {
EditPoiDialogFragment fragment = EditPoiDialogFragment fragment =
EditPoiDialogFragment.createInstance(n, amenity, false); EditPoiDialogFragment.createInstance(n, false);
fragment.show(activity.getSupportFragmentManager(), TAG); fragment.show(activity.getSupportFragmentManager(), TAG);
} else { } else {
AccessibleToast.makeText(activity, AccessibleToast.makeText(activity,

View file

@ -6,9 +6,9 @@ import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import net.osmand.data.Amenity; import net.osmand.data.Amenity;
import net.osmand.osm.MapPoiTypes; import net.osmand.osm.MapPoiTypes;
import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiType; import net.osmand.osm.PoiType;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.osmedit.EditPoiDialogFragment; import net.osmand.plus.osmedit.EditPoiDialogFragment;
@ -17,15 +17,15 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
public class PoiSubTypeDialogFragment extends DialogFragment { public class PoiSubTypeDialogFragment extends DialogFragment {
private static final String KEY_AMENITY = "amenity"; private static final String KEY_POI_CATEGORY = "amenity";
@NonNull @NonNull
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
MapPoiTypes poiTypes = ((OsmandApplication) getActivity().getApplication()).getPoiTypes(); MapPoiTypes poiTypes = ((OsmandApplication) getActivity().getApplication()).getPoiTypes();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final Amenity a = (Amenity) getArguments().getSerializable(KEY_AMENITY); final PoiCategory a = poiTypes.getPoiCategoryByName((String) getArguments().getSerializable(KEY_POI_CATEGORY));
final Map<String, PoiType> allTranslatedNames = poiTypes.getAllTranslatedNames(a.getType(), true); final Map<String, PoiType> allTranslatedNames = poiTypes.getAllTranslatedNames(a, true);
Set<String> strings = allTranslatedNames.keySet(); Set<String> strings = allTranslatedNames.keySet();
final String[] subCats = strings.toArray(new String[strings.size()]); final String[] subCats = strings.toArray(new String[strings.size()]);
builder.setItems(subCats, new DialogInterface.OnClickListener() { builder.setItems(subCats, new DialogInterface.OnClickListener() {
@ -38,10 +38,10 @@ public class PoiSubTypeDialogFragment extends DialogFragment {
return builder.create(); return builder.create();
} }
public static PoiSubTypeDialogFragment createInstance(Amenity amenity) { public static PoiSubTypeDialogFragment createInstance(PoiCategory cat) {
PoiSubTypeDialogFragment fragment = new PoiSubTypeDialogFragment(); PoiSubTypeDialogFragment fragment = new PoiSubTypeDialogFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putSerializable(KEY_AMENITY, amenity); args.putSerializable(KEY_POI_CATEGORY, cat.getKeyName());
fragment.setArguments(args); fragment.setArguments(args);
return fragment; return fragment;
} }

View file

@ -17,21 +17,17 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class PoiTypeDialogFragment extends DialogFragment { public class PoiTypeDialogFragment extends DialogFragment {
private static final String KEY_AMENITY = "amenity";
@NonNull @NonNull
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
MapPoiTypes poiTypes = ((OsmandApplication) getActivity().getApplication()).getPoiTypes(); MapPoiTypes poiTypes = ((OsmandApplication) getActivity().getApplication()).getPoiTypes();
final Amenity amenity = (Amenity) getArguments().getSerializable(KEY_AMENITY);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final List<PoiCategory> categories = poiTypes.getCategories(false); final List<PoiCategory> categories = poiTypes.getCategories(false);
ArrayList<String> vals = new ArrayList<>(categories.size()); ArrayList<String> vals = new ArrayList<>(categories.size());
ArrayList<PoiCategory> toDelete = new ArrayList<>(); ArrayList<PoiCategory> toDelete = new ArrayList<>();
// TODO replace with constants
for (PoiCategory category : categories) { for (PoiCategory category : categories) {
if (category.getKeyName().equals("user_defined_other") if (!category.isNotEditableOsm()) {
|| category.getKeyName().equals("osmwiki")) {
toDelete.add(category); toDelete.add(category);
} else { } else {
vals.add(category.getTranslation()); vals.add(category.getTranslation());
@ -42,21 +38,16 @@ public class PoiTypeDialogFragment extends DialogFragment {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
PoiCategory aType = categories.get(which); PoiCategory aType = categories.get(which);
if (aType != amenity.getType()) { ((EditPoiDialogFragment) getParentFragment()).updateType(aType);
amenity.setType(aType);
amenity.setSubType(""); //$NON-NLS-1$
((EditPoiDialogFragment) getParentFragment()).updateType(amenity);
}
dismiss(); dismiss();
} }
}); });
return builder.create(); return builder.create();
} }
public static PoiTypeDialogFragment createInstance(Amenity amenity) { public static PoiTypeDialogFragment createInstance() {
PoiTypeDialogFragment poiTypeDialogFragment = new PoiTypeDialogFragment(); PoiTypeDialogFragment poiTypeDialogFragment = new PoiTypeDialogFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putSerializable(KEY_AMENITY, amenity);
poiTypeDialogFragment.setArguments(args); poiTypeDialogFragment.setArguments(args);
return poiTypeDialogFragment; return poiTypeDialogFragment;
} }