diff --git a/MapboxAndroidSDK/src/main/java/com/spatialdev/osm/model/OSMDataSet.java b/MapboxAndroidSDK/src/main/java/com/spatialdev/osm/model/OSMDataSet.java index 44ced19..b4db239 100644 --- a/MapboxAndroidSDK/src/main/java/com/spatialdev/osm/model/OSMDataSet.java +++ b/MapboxAndroidSDK/src/main/java/com/spatialdev/osm/model/OSMDataSet.java @@ -54,6 +54,15 @@ public class OSMDataSet { */ private List openWays = new ArrayList<>(); + private static Set tagValues = new HashSet<>(); + + public static void addTagValue(String tagValue) { + tagValues.add(tagValue); + } + + public static Set tagValues() { + return tagValues; + } public OSMDataSet() {} diff --git a/MapboxAndroidSDK/src/main/java/com/spatialdev/osm/model/OSMXmlParser.java b/MapboxAndroidSDK/src/main/java/com/spatialdev/osm/model/OSMXmlParser.java index deb15bf..c00486c 100644 --- a/MapboxAndroidSDK/src/main/java/com/spatialdev/osm/model/OSMXmlParser.java +++ b/MapboxAndroidSDK/src/main/java/com/spatialdev/osm/model/OSMXmlParser.java @@ -211,6 +211,7 @@ private void readTags(OSMElement el) throws XmlPullParserException, IOException String k = parser.getAttributeValue(ns, "k"); String v = parser.getAttributeValue(ns, "v"); el.addParsedTag(k, v); + OSMDataSet.addTagValue(v); // we do this twice, because these are singular nodes that // function as start and end tags parser.nextTag(); diff --git a/app/app.iml b/app/app.iml index 5902d5c..21910d6 100644 --- a/app/app.iml +++ b/app/app.iml @@ -100,8 +100,8 @@ - + diff --git a/app/build.gradle b/app/build.gradle index 677f0fa..ed8953b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,8 +12,8 @@ android { applicationId "org.redcross.openmapkit" minSdkVersion 16 targetSdkVersion 21 - versionCode 20 - versionName "0.20" + versionCode 21 + versionName "0.21" } buildTypes { release { diff --git a/app/src/main/assets/constraints/buildings.json b/app/src/main/assets/constraints/buildings.json new file mode 100644 index 0000000..15bf0bd --- /dev/null +++ b/app/src/main/assets/constraints/buildings.json @@ -0,0 +1,25 @@ +{ + "addr:city": { + "default": "Sacramento", + "required": true, + "custom_value": true + }, + "addr:housenumber": { + "numeric": true + }, + "addr:state": { + "default": "CA", + "required": true + }, + "addr:street": { + "required": true + }, + "building": { + "required": true + }, + "name": { + "hide_if": { + "building": "house" + } + } +} diff --git a/app/src/main/assets/constraints/default.json b/app/src/main/assets/constraints/default.json index 3713a1b..0170d5f 100644 --- a/app/src/main/assets/constraints/default.json +++ b/app/src/main/assets/constraints/default.json @@ -1,3 +1,66 @@ { - "test": "value1" + "addr:city": { + "default": "Sacramento" + }, + "addr:housenumber": { + "numeric": true + }, + "addr:state": { + "default": "CA" + }, + "addr:postcode": { + "numeric": true + }, + "amenity": { + "hide_if": { + "building": "residential", + "shop": true, + "office": true + }, + "custom_value": true + }, + "building": { + "default": "yes" + }, + "building:levels": { + "numeric": true, + "custom_value": true + }, + "cuisine": { + "show_if": { + "amenity": true + } + }, + "network": { + "select_multiple": true + }, + "office": { + "hide_if": { + "amenity": true, + "shop": true + } + }, + "opening_hours": { + "select_multiple": true + }, + "operator": { + "select_multiple": true, + "custom_value": true + }, + "religion": { + "show_if": { + "amenity": "place_of_worship" + } + }, + "shop": { + "custom_value": true, + "hide_if": { + "building": "residential", + "amenity": true, + "office": true + } + }, + "source": { + "implicit": "survey" + } } diff --git a/app/src/main/java/org/redcross/openmapkit/Basemap.java b/app/src/main/java/org/redcross/openmapkit/Basemap.java index f66a327..4637c80 100644 --- a/app/src/main/java/org/redcross/openmapkit/Basemap.java +++ b/app/src/main/java/org/redcross/openmapkit/Basemap.java @@ -87,7 +87,7 @@ public void presentBasemapsOptions() { } if (basemaps.size() == 0) { - Toast prompt = Toast.makeText(context, "Device is offline. Please add .mbtiles file to " + ExternalStorage.getMBTilesDir() + " or check out a deployment.", Toast.LENGTH_LONG); + Toast prompt = Toast.makeText(context, "No basemap downloaded yet. You may 1) download a deployment, 2) connect to the internet, or 3) add .mbtiles file to " + ExternalStorage.getMBTilesDir() + ".", Toast.LENGTH_LONG); prompt.show(); return; } @@ -189,7 +189,7 @@ private void selectMBTilesBasemap(String mbtilesPath) { if(!mbtilesFile.exists()) { AlertDialog.Builder builder = new AlertDialog.Builder(mapActivity); builder.setTitle("Offline Basemap Not Found"); - builder.setMessage("Please add MBTiles to " + ExternalStorage.getMBTilesDir()); + builder.setMessage("Please check out a deployment or add MBTiles files to " + ExternalStorage.getMBTilesDir()); builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //placeholder diff --git a/app/src/main/java/org/redcross/openmapkit/Constraints.java b/app/src/main/java/org/redcross/openmapkit/Constraints.java new file mode 100644 index 0000000..017400e --- /dev/null +++ b/app/src/main/java/org/redcross/openmapkit/Constraints.java @@ -0,0 +1,438 @@ +package org.redcross.openmapkit; + +import com.spatialdev.osm.model.OSMElement; + +import org.apache.commons.io.FileUtils; +import org.json.JSONException; +import org.json.JSONObject; +import org.redcross.openmapkit.odkcollect.ODKCollectHandler; +import org.redcross.openmapkit.odkcollect.tag.ODKTag; +import org.redcross.openmapkit.tagswipe.TagEdit; + +import java.io.File; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +public class Constraints { + + private static Constraints instance; + + private JSONObject defaultConstraintsJson; + private JSONObject formConstraintsJson; + + /** + * Contains a map of constraint keys with the key value pairs of the + * OSM Element conditions in which a tag view should be hidden. + * + * The key of the outer map is to the tag in question + */ + private Map> hideMap = new HashMap<>(); + + /** + * This map is similar, but the key to the outer map is of tag keys + * that will cause the inner Set of tag keys to be hidden. + */ + private Map>> causeHideMap = new HashMap<>(); + + /** + * Contains a map of constraint keys with the key value pairs of the + * OSM Element conditions in which a tag view should be shown. + * + * The key of the outer map is to the tag in question. + */ + private Map> showMap = new HashMap<>(); + + /** + * The key of the outer map is to the tag key that causes the inner + * set of tag keys to be shown. + */ + private Map>> causeShowMap = new HashMap<>(); + + private boolean active = true; + + public class TagAction { + public Set hide = new HashSet<>(); + public Set show = new HashSet<>(); + } + + public static Constraints initialize() { + instance = new Constraints(); + return instance; + } + + public static Constraints singleton() { + return instance; + } + + /** + * If there is no default.json constraints file, we have nothing to work with, + * so the constraint functionality is disabled. + * + * @return boolean if the constraint is active + */ + public boolean isActive() { + return active; + } + + public boolean tagIsNumeric(String tagKey) { + return cascadeBooleanTagConstraint(tagKey, "numeric", false); + } + + public boolean tagAllowsCustomValue(String tagKey) { + return cascadeBooleanTagConstraint(tagKey, "custom_value", false); + } + + public boolean tagIsRequired(String tagKey) { + return cascadeBooleanTagConstraint(tagKey, "required", false); + } + + public boolean tagIsSelectMultiple(String tagKey) { + return cascadeBooleanTagConstraint(tagKey, "select_multiple", false); + } + + public Set requiredTagsNotMet(OSMElement osmElement) { + Set missingTags = new HashSet<>(); + + if (ODKCollectHandler.isODKCollectMode()) { + Map tags = osmElement.getTags(); + Collection odkTags = ODKCollectHandler.getODKCollectData().getRequiredTags(); + for (ODKTag odkTag : odkTags) { + String odkTagKey = odkTag.getKey(); + if (cascadeBooleanTagConstraint(odkTagKey, "required", false)) { + String osmTagVal = tags.get(odkTagKey); + if (osmTagVal == null || osmTagVal.length() < 1) { + missingTags.add(odkTagKey); + } + } + } + } + + return missingTags; + } + + public String tagDefaultValue(String tagKey) { + return cascadeStringTagConstraint(tagKey, "default", null); + } + + public boolean tagShouldBeShown(String tagKey, OSMElement osmElement) { + if (!isActive()) return true; + + // Check showMap + Map showMapMap = showMap.get(tagKey); + if (showMapMap != null && showMapMap.size() > 0) { + Set showMapMapKeys = showMapMap.keySet(); + for (String key : showMapMapKeys) { + String val = showMapMap.get(key); + // wildcard + if (val.equals("true")) { + if (osmElement.getTags().keySet().contains(key)) { + // OSM Element has a tag key that is required (declared with true) + // by the show_if condition for the given tag_key. + return true; + } + } else { + String osmElementTagVal = osmElement.getTags().get(key); + if (osmElementTagVal != null && osmElementTagVal.equals(val)) { + // OSM Element has a tag key and value that match the + // show_if condition for the given tagKey. + return true; + } + } + } + // OSM Element has no tags that match the show_if condition for the given tagKey. + return false; + } + + // Check hideMap + Map hideMapMap = hideMap.get(tagKey); + if (hideMapMap != null && hideMapMap.size() > 0) { + Set hideMapMapKeys = hideMapMap.keySet(); + for (String key : hideMapMapKeys) { + String val = hideMapMap.get(key); + // wildcard + if (val.equals("true")) { + if (osmElement.getTags().keySet().contains(key)) { + return false; + } + } else { + String osmElementTagVal = osmElement.getTags().get(key); + if (osmElementTagVal != null && osmElementTagVal.equals(val)) { + return false; + } + } + } + } + + // If the tag isn't mentioned in the showMap or the hideMap, then we should show it! + return true; + } + + /** + * If the tag has an implicit value, it returns it. + * Otherwise, it returns null. + * + * This is how to test if the tag should be considered implicit. + * + * @param tagKey + * @return implicit value or null + */ + public String implicitVal(String tagKey) { + return cascadeStringTagConstraint(tagKey, "implicit", null); + } + + public TagAction tagAddedOrEdited(String key, String val) { + TagAction tagAction = new TagAction(); + if (!isActive()) return tagAction; + + tagAction.hide = findTagsToBeHiddenFromUpdate(key, val); + tagAction.show = findTagsToBeShownFromUpdate(key, val); + + return tagAction; + } + + public TagAction tagDeleted(String key) { + TagAction tagAction = new TagAction(); + if (!isActive()) return tagAction; + + tagAction.hide = findTagsToBeHiddenFromDelete(key); + tagAction.show = findTagsToBeShownFromDelete(key); + + return tagAction; + } + + private Constraints() { + loadConstraintsJson(); + buildSkipLogic(); + } + + private void loadConstraintsJson() { + try { + File defaultConstraintsFile = ExternalStorage.fetchConstraintsFile("default"); + String defaultConstraintsStr = FileUtils.readFileToString(defaultConstraintsFile); + defaultConstraintsJson = new JSONObject(defaultConstraintsStr); + } + // If there is no default.json constraints file, + // we just turn off Constraints functionality. + catch (Exception e) { + active = false; + } + + if (!ODKCollectHandler.isODKCollectMode()) return; + + String formFileName = ODKCollectHandler.getODKCollectData().getFormFileName(); + try { + File formConstraintsFile = ExternalStorage.fetchConstraintsFile(formFileName); + String formConstraintsStr = FileUtils.readFileToString(formConstraintsFile); + formConstraintsJson = new JSONObject(formConstraintsStr); + } catch (Exception e) { + // do nothing + // We typically do not need a constraints specific to a given form, + // so this is normal. + } + } + + private boolean cascadeBooleanTagConstraint(String tagKey, String tagConstraint, boolean defaultVal) { + boolean val = defaultVal; + if (!isActive()) return val; + + try { + JSONObject tagConstraints = defaultConstraintsJson.getJSONObject(tagKey); + val = tagConstraints.getBoolean(tagConstraint); + } catch (JSONException e) { + // do nothing + } + + if (formConstraintsJson != null) { + try { + JSONObject tagConstraints = formConstraintsJson.getJSONObject(tagKey); + val = tagConstraints.getBoolean(tagConstraint); + } catch (JSONException e) { + // do nothing + } + } + + return val; + } + + private String cascadeStringTagConstraint(String tagKey, String tagConstraint, String defaultVal) { + String val = defaultVal; + if (!isActive()) return val; + + try { + JSONObject tagConstraints = defaultConstraintsJson.getJSONObject(tagKey); + val = tagConstraints.getString(tagConstraint); + } catch (JSONException e) { + // do nothing + } + + if (formConstraintsJson != null) { + try { + JSONObject tagConstraints = formConstraintsJson.getJSONObject(tagKey); + val = tagConstraints.getString(tagConstraint); + } catch (JSONException e) { + // do nothing + } + } + + return val; + } + + private void buildSkipLogic() { + if (defaultConstraintsJson != null) { + buildHideMaps(defaultConstraintsJson); + buildShowMaps(defaultConstraintsJson); + } + if (formConstraintsJson != null) { + buildHideMaps(formConstraintsJson); + buildShowMaps(formConstraintsJson); + } + } + + private void buildHideMaps(JSONObject constraintsJson) { + Iterator tagKeys = constraintsJson.keys(); + // iterate through main tag keys + while (tagKeys.hasNext()) { + String tag = tagKeys.next(); + try { + JSONObject constraints = constraintsJson.getJSONObject(tag); + Map hideMapMap = hideMap.get(tag); + if (hideMapMap == null) { + hideMapMap = new HashMap<>(); + hideMap.put(tag, hideMapMap); + } + JSONObject hideIf = constraints.getJSONObject("hide_if"); + Iterator hideIfKeys = hideIf.keys(); + while (hideIfKeys.hasNext()) { + String hideIfKey = hideIfKeys.next(); + String hideIfVal = hideIf.optString(hideIfKey); + Map> causeHideMapMap = causeHideMap.get(hideIfKey); + if (causeHideMapMap == null) { + causeHideMapMap = new HashMap<>(); + causeHideMapMap.put(hideIfVal, new HashSet()); + causeHideMap.put(hideIfKey, causeHideMapMap); + } else if (causeHideMapMap.get(hideIfVal) == null) { + causeHideMapMap.put(hideIfVal, new HashSet()); + } + hideMapMap.put(hideIfKey, hideIfVal); + causeHideMapMap.get(hideIfVal).add(tag); + } + } catch (JSONException e) { + // do nothing + } + } + } + + private void buildShowMaps(JSONObject constraintsJson) { + Iterator tagKeys = constraintsJson.keys(); + // iterate through main tag keys + while (tagKeys.hasNext()) { + String tag = tagKeys.next(); + try { + JSONObject constraints = constraintsJson.getJSONObject(tag); + Map showMapMap = showMap.get(tag); + if (showMapMap == null) { + showMapMap = new HashMap<>(); + showMap.put(tag, showMapMap); + } + JSONObject showIf = constraints.getJSONObject("show_if"); + Iterator showIfKeys = showIf.keys(); + while (showIfKeys.hasNext()) { + String showIfKey = showIfKeys.next(); + String showIfVal = showIf.optString(showIfKey); + Map> causeShowMapMap = causeShowMap.get(showIfKey); + if (causeShowMapMap == null) { + causeShowMapMap = new HashMap<>(); + causeShowMapMap.put(showIfVal, new HashSet()); + causeShowMap.put(showIfKey, causeShowMapMap); + } else if (causeShowMapMap.get(showIfVal) == null) { + causeShowMapMap.put(showIfVal, new HashSet()); + } + showMapMap.put(showIfKey, showIfVal); + causeShowMapMap.get(showIfVal).add(tag); + } + } catch (JSONException e) { + // do nothing + } + } + } + + private Set findTagsToBeHiddenFromUpdate(String key, String val) { + Set tags = new HashSet<>(); + + Map> causeHideMapMap = causeHideMap.get(key); + if (causeHideMapMap != null) { + Set set = causeHideMapMap.get("true"); + if (set != null) { + tags.addAll(set); + } + set = causeHideMap.get(key).get(val); + if (set != null) { + tags.addAll(set); + } + } + + Map> causeShowMapMap = causeShowMap.get(key); + if (causeShowMapMap != null) { + Set keys = causeShowMapMap.keySet(); + for (String k : keys) { + // We should still show the keys that should be shown by + // the key wildcard and the specific new value selected. + if (k.equals("true") || k.equals(val)) continue; + Set tagsToHide = causeShowMapMap.get(k); + tags.addAll(tagsToHide); + } + } + return tags; + } + + private Set findTagsToBeShownFromUpdate(String key, String val) { + Set tags = new HashSet<>(); + + Map> causeShowMapMap = causeShowMap.get(key); + if (causeShowMapMap != null) { + Set set = causeShowMapMap.get("true"); + if (set != null) { + tags.addAll(set); + } + set = causeShowMap.get(key).get(val); + if (set != null) { + tags.addAll(set); + } + } + + return tags; + } + + private Set findTagsToBeHiddenFromDelete(String key) { + Set tags = new HashSet<>(); + + Map> allTags = causeShowMap.get(key); + if (allTags != null && allTags.size() > 0) { + Set vals = allTags.keySet(); + for (String v : vals) { + tags.addAll(allTags.get(v)); + } + } + + return tags; + } + + private Set findTagsToBeShownFromDelete(String key) { + Set tags = new HashSet<>(); + + Map> allTags = causeHideMap.get(key); + if (allTags != null && allTags.size() > 0) { + Set vals = allTags.keySet(); + for (String v : vals) { + tags.addAll(allTags.get(v)); + } + } + + return tags; + } + +} diff --git a/app/src/main/java/org/redcross/openmapkit/ExternalStorage.java b/app/src/main/java/org/redcross/openmapkit/ExternalStorage.java index d9ad680..4d61345 100644 --- a/app/src/main/java/org/redcross/openmapkit/ExternalStorage.java +++ b/app/src/main/java/org/redcross/openmapkit/ExternalStorage.java @@ -6,6 +6,7 @@ import android.util.Log; import org.apache.commons.io.FilenameUtils; +import org.json.JSONObject; import java.io.BufferedReader; import java.io.File; @@ -359,7 +360,12 @@ private static void copyAssetsFileToExternalStorage(Context context, String file } - + public static File fetchConstraintsFile(String formName) { + File storageDir = Environment.getExternalStorageDirectory(); + File appDir = new File(storageDir, APP_DIR); + File constraintsDir = new File(appDir, CONSTRAINTS_DIR); + return new File(constraintsDir, formName + ".json"); + } diff --git a/app/src/main/java/org/redcross/openmapkit/MapActivity.java b/app/src/main/java/org/redcross/openmapkit/MapActivity.java index ee2b413..f04c554 100644 --- a/app/src/main/java/org/redcross/openmapkit/MapActivity.java +++ b/app/src/main/java/org/redcross/openmapkit/MapActivity.java @@ -125,6 +125,10 @@ protected void onCreate(Bundle savedInstanceState) { // This will determine if we are in ODK Collect Mode or not. ODKCollectHandler.registerIntent(getIntent()); + // Initialize the constraints singleton. + // Loads up all the constraints JSON configs. + Constraints.initialize(); + //set layout setContentView(R.layout.activity_map); @@ -572,8 +576,18 @@ public void onClick(DialogInterface dialog, int id) { }); builder.show(); } else { - Toast prompt = Toast.makeText(getApplicationContext(), "Please add .osm files to " + ExternalStorage.getOSMDir(), Toast.LENGTH_LONG); - prompt.show(); + Snackbar.make(findViewById(R.id.mapActivity), + "You have not yet downloaded any OSM data. Please check out a deployment or add .osm files to external storage.", + Snackbar.LENGTH_INDEFINITE) + .setAction("OK", new View.OnClickListener() { + // undo action + @Override + public void onClick(View v) { + + } + }) + .setActionTextColor(Color.rgb(126,188,111)) + .show(); } } diff --git a/app/src/main/java/org/redcross/openmapkit/TagListAdapter.java b/app/src/main/java/org/redcross/openmapkit/TagListAdapter.java index 2d5a815..11e26bf 100644 --- a/app/src/main/java/org/redcross/openmapkit/TagListAdapter.java +++ b/app/src/main/java/org/redcross/openmapkit/TagListAdapter.java @@ -39,7 +39,9 @@ public TagListAdapter(Activity activity, OSMElement osmElement) { for (ODKTag odkTag : requiredTags) { String key = odkTag.getKey(); String val = tags.get(key); - tagMap.put(key, val); + if (Constraints.singleton().tagShouldBeShown(key, osmElement)) { + tagMap.put(key, val); + } readOnlyTags.remove(key); } Set readOnlyKeys = readOnlyTags.keySet(); diff --git a/app/src/main/java/org/redcross/openmapkit/odkcollect/ODKCollectData.java b/app/src/main/java/org/redcross/openmapkit/odkcollect/ODKCollectData.java index c896f64..11e83d5 100644 --- a/app/src/main/java/org/redcross/openmapkit/odkcollect/ODKCollectData.java +++ b/app/src/main/java/org/redcross/openmapkit/odkcollect/ODKCollectData.java @@ -30,6 +30,7 @@ public class ODKCollectData { public static final String APP_NAME = "OpenMapKit Android"; private String formId; + private String formFileName; private String instanceId; private String instanceDir; private String previousOSMEditFileName; @@ -47,6 +48,7 @@ public ODKCollectData ( String formId, String previousOSMEditFileName, LinkedHashMap requiredTags ) { this.formId = formId; + this.formFileName = formFileName; this.instanceId = instanceId; this.instanceDir = instanceDir; this.previousOSMEditFileName = previousOSMEditFileName; @@ -91,6 +93,10 @@ public String getFormId() { return formId; } + public String getFormFileName() { + return formFileName; + } + public String getInstanceId() { return instanceId; } diff --git a/app/src/main/java/org/redcross/openmapkit/tagswipe/SelectMultipleTagValueFragment.java b/app/src/main/java/org/redcross/openmapkit/tagswipe/SelectMultipleTagValueFragment.java index b37b56d..6a15eae 100644 --- a/app/src/main/java/org/redcross/openmapkit/tagswipe/SelectMultipleTagValueFragment.java +++ b/app/src/main/java/org/redcross/openmapkit/tagswipe/SelectMultipleTagValueFragment.java @@ -2,6 +2,7 @@ import android.app.Activity; import android.content.Context; +import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; @@ -11,12 +12,17 @@ import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; +import android.widget.ArrayAdapter; +import android.widget.AutoCompleteTextView; import android.widget.CheckBox; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; +import com.spatialdev.osm.model.OSMDataSet; + import org.apache.commons.lang3.StringUtils; +import org.redcross.openmapkit.Constraints; import org.redcross.openmapkit.R; import org.redcross.openmapkit.odkcollect.tag.ODKTag; import org.redcross.openmapkit.odkcollect.tag.ODKTagItem; @@ -62,6 +68,10 @@ private void setupWidgets() { String keyLabel = tagEdit.getTagKeyLabel(); String key = tagEdit.getTagKey(); + if (Constraints.singleton().tagIsRequired(key)) { + rootView.findViewById(R.id.requiredTextView).setVisibility(View.VISIBLE); + } + if (keyLabel != null) { tagKeyLabelTextView.setText(keyLabel); tagKeyTextView.setText(key); @@ -117,7 +127,7 @@ private void setupCheckBoxes() { } final CheckBox editTextCheckBox = new CheckBox(activity); - final EditText editText = new EditText(activity); + final AutoCompleteTextView editText = new AutoCompleteTextView(activity); editText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); if (prevTagValsNotInChoices.size() > 0) { String joinedNotInChoices = StringUtils.join(prevTagValsNotInChoices, ";"); @@ -142,6 +152,12 @@ public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { public void afterTextChanged(Editable editable) { } }); + + // Numeric Input Constraint + if (Constraints.singleton().tagIsNumeric(tagEdit.getTagKey())) { + editText.setRawInputType(Configuration.KEYBOARD_QWERTY); + } + editTextCheckBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -156,14 +172,20 @@ public void onClick(View view) { }); tagEdit.setupEditCheckbox(editTextCheckBox, editText); - LinearLayout customLinearLayout = new LinearLayout(activity); - customLinearLayout.setOrientation(LinearLayout.HORIZONTAL); - customLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); - customLinearLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); - customLinearLayout.setFocusableInTouchMode(true); - customLinearLayout.addView(editTextCheckBox); - customLinearLayout.addView(editText); - checkboxLinearLayout.addView(customLinearLayout); + if (Constraints.singleton().tagAllowsCustomValue(tagEdit.getTagKey())) { + + // Only setup this more expensive model for AutoComplete when we know we need it. + setupAutoComplete(editText); + + LinearLayout customLinearLayout = new LinearLayout(activity); + customLinearLayout.setOrientation(LinearLayout.HORIZONTAL); + customLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + customLinearLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); + customLinearLayout.setFocusableInTouchMode(true); + customLinearLayout.addView(editTextCheckBox); + customLinearLayout.addView(editText); + checkboxLinearLayout.addView(customLinearLayout); + } } @@ -214,6 +236,15 @@ public void onDetach() { mListener = null; } + private void setupAutoComplete(AutoCompleteTextView autoCompleteTextView) { + Set tagValues = OSMDataSet.tagValues(); + String[] tagValuesArr = tagValues.toArray(new String[tagValues.size()]); + ArrayAdapter adapter = new ArrayAdapter<>(this.getActivity(), + android.R.layout.simple_dropdown_item_1line, tagValuesArr); + autoCompleteTextView.setAdapter(adapter); + autoCompleteTextView.setThreshold(1); + } + /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated diff --git a/app/src/main/java/org/redcross/openmapkit/tagswipe/SelectOneTagValueFragment.java b/app/src/main/java/org/redcross/openmapkit/tagswipe/SelectOneTagValueFragment.java index 2cc7920..d82011f 100644 --- a/app/src/main/java/org/redcross/openmapkit/tagswipe/SelectOneTagValueFragment.java +++ b/app/src/main/java/org/redcross/openmapkit/tagswipe/SelectOneTagValueFragment.java @@ -2,6 +2,7 @@ import android.app.Activity; import android.content.Context; +import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; @@ -11,17 +12,23 @@ import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; +import android.widget.ArrayAdapter; +import android.widget.AutoCompleteTextView; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; +import com.spatialdev.osm.model.OSMDataSet; + +import org.redcross.openmapkit.Constraints; import org.redcross.openmapkit.R; import org.redcross.openmapkit.odkcollect.tag.ODKTag; import org.redcross.openmapkit.odkcollect.tag.ODKTagItem; import java.util.Collection; +import java.util.Set; /** * A simple {@link Fragment} subclass. @@ -56,6 +63,10 @@ private void setupWidgets() { String keyLabel = tagEdit.getTagKeyLabel(); String key = tagEdit.getTagKey(); + if (Constraints.singleton().tagIsRequired(key)) { + rootView.findViewById(R.id.requiredTextView).setVisibility(View.VISIBLE); + } + if (keyLabel != null) { tagKeyLabelTextView.setText(keyLabel); tagKeyTextView.setText(key); @@ -85,7 +96,7 @@ private void setupRadioButtons() { /** * Special EditText that is next to the customButton */ - final EditText customEditText = new EditText(activity); + final AutoCompleteTextView customEditText = new AutoCompleteTextView(activity); customEditText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); customEditText.addTextChangedListener(new TextWatcher() { @Override @@ -107,6 +118,12 @@ public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { public void afterTextChanged(Editable editable) { } }); + + // Numeric Input Constraint + if (Constraints.singleton().tagIsNumeric(tagEdit.getTagKey())) { + customEditText.setRawInputType(Configuration.KEYBOARD_QWERTY); + } + customButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -152,7 +169,7 @@ public void onClick(View v) { } tagValueRadioGroup.addView(button); if (prevTagVal != null && value.equals(prevTagVal)) { - button.toggle(); + button.toggleOn(); } int id = button.getId(); odkTag.putButtonIdToTagItemHash(id, item); @@ -167,14 +184,20 @@ public void onClick(View v) { * Adding customButton and customEditText to a horizontal linear layout * and puts that as the last item in the tag value radio group. */ - LinearLayout customLinearLayout = new LinearLayout(activity); - customLinearLayout.setOrientation(LinearLayout.HORIZONTAL); - customLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); - customLinearLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); - customLinearLayout.setFocusableInTouchMode(true); - customLinearLayout.addView(customButton); - customLinearLayout.addView(customEditText); - tagValueRadioGroup.addView(customLinearLayout); + if (Constraints.singleton().tagAllowsCustomValue(tagEdit.getTagKey())) { + + // Only setup this more expensive model for AutoComplete when we know we need it. + setupAutoComplete(customEditText); + + LinearLayout customLinearLayout = new LinearLayout(activity); + customLinearLayout.setOrientation(LinearLayout.HORIZONTAL); + customLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + customLinearLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); + customLinearLayout.setFocusableInTouchMode(true); + customLinearLayout.addView(customButton); + customLinearLayout.addView(customEditText); + tagValueRadioGroup.addView(customLinearLayout); + } } @@ -230,6 +253,15 @@ public void onDetach() { mListener = null; } + private void setupAutoComplete(AutoCompleteTextView autoCompleteTextView) { + Set tagValues = OSMDataSet.tagValues(); + String[] tagValuesArr = tagValues.toArray(new String[tagValues.size()]); + ArrayAdapter adapter = new ArrayAdapter<>(this.getActivity(), + android.R.layout.simple_dropdown_item_1line, tagValuesArr); + autoCompleteTextView.setAdapter(adapter); + autoCompleteTextView.setThreshold(1); + } + /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated @@ -286,5 +318,20 @@ else if (customButton != null){ } } } + + public void toggleOn() { + setChecked(true); + // if custom button + if(!(getParent() instanceof RadioGroup)) { + radioGroup.clearCheck(); + } + // not custom button + else if (customButton != null){ + customButton.setChecked(false); + if (customEditText != null) { + customEditText.clearFocus(); + } + } + } } } diff --git a/app/src/main/java/org/redcross/openmapkit/tagswipe/StringTagValueFragment.java b/app/src/main/java/org/redcross/openmapkit/tagswipe/StringTagValueFragment.java index 159129e..2d43267 100644 --- a/app/src/main/java/org/redcross/openmapkit/tagswipe/StringTagValueFragment.java +++ b/app/src/main/java/org/redcross/openmapkit/tagswipe/StringTagValueFragment.java @@ -1,6 +1,7 @@ package org.redcross.openmapkit.tagswipe; import android.content.Context; +import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; @@ -8,11 +9,18 @@ import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; +import android.widget.ArrayAdapter; +import android.widget.AutoCompleteTextView; import android.widget.EditText; import android.widget.TextView; +import com.spatialdev.osm.model.OSMDataSet; + +import org.redcross.openmapkit.Constraints; import org.redcross.openmapkit.R; +import java.util.Set; + public class StringTagValueFragment extends Fragment { @@ -23,7 +31,7 @@ public class StringTagValueFragment extends Fragment { private TextView tagKeyLabelTextView; private TextView tagKeyTextView; - private EditText tagValueEditText; + private AutoCompleteTextView tagValueEditText; private OnFragmentInteractionListener mListener; @@ -39,11 +47,17 @@ public static StringTagValueFragment newInstance(int idx) { private void setupWidgets() { tagKeyLabelTextView = (TextView)rootView.findViewById(R.id.tagKeyLabelTextView); tagKeyTextView = (TextView)rootView.findViewById(R.id.tagKeyTextView); - tagValueEditText = (EditText)rootView.findViewById(R.id.tagValueEditText); + tagValueEditText = (AutoCompleteTextView)rootView.findViewById(R.id.tagValueEditText); + + setupAutoComplete(); String keyLabel = tagEdit.getTagKeyLabel(); String key = tagEdit.getTagKey(); String val = tagEdit.getTagVal(); + + if (Constraints.singleton().tagIsRequired(key)) { + rootView.findViewById(R.id.requiredTextView).setVisibility(View.VISIBLE); + } if (keyLabel != null) { tagKeyLabelTextView.setText(keyLabel); @@ -55,6 +69,24 @@ private void setupWidgets() { tagValueEditText.setText(val); tagEdit.setEditText(tagValueEditText); + + if (Constraints.singleton().tagIsNumeric(key)) { + /** + * You could use Configuration.KEYBOARD_12KEY but this does not allow + * switching back to the normal alphabet keyboard. That needs to be + * an option. + */ + tagValueEditText.setRawInputType(Configuration.KEYBOARD_QWERTY); + } + } + + private void setupAutoComplete() { + Set tagValues = OSMDataSet.tagValues(); + String[] tagValuesArr = tagValues.toArray(new String[tagValues.size()]); + ArrayAdapter adapter = new ArrayAdapter<>(this.getActivity(), + android.R.layout.simple_dropdown_item_1line, tagValuesArr); + tagValueEditText.setAdapter(adapter); + tagValueEditText.setThreshold(1); } public StringTagValueFragment() { diff --git a/app/src/main/java/org/redcross/openmapkit/tagswipe/TagEdit.java b/app/src/main/java/org/redcross/openmapkit/tagswipe/TagEdit.java index e178cc1..6dc03c5 100644 --- a/app/src/main/java/org/redcross/openmapkit/tagswipe/TagEdit.java +++ b/app/src/main/java/org/redcross/openmapkit/tagswipe/TagEdit.java @@ -9,6 +9,7 @@ import com.spatialdev.osm.model.OSMElement; +import org.redcross.openmapkit.Constraints; import org.redcross.openmapkit.odkcollect.ODKCollectData; import org.redcross.openmapkit.odkcollect.ODKCollectHandler; import org.redcross.openmapkit.odkcollect.tag.ODKTag; @@ -31,15 +32,16 @@ public class TagEdit { private static LinkedHashMap tagEditHash; + private static LinkedHashMap tagEditHiddenHash; private static List tagEdits; private static OSMElement osmElement; + private static TagSwipeActivity tagSwipeActivity; - private String tagKey; + private final String tagKey; // a given TagEdit always associates to an immutable key private String tagVal; private ODKTag odkTag; private boolean readOnly; private boolean checkBoxMode = false; - private int idx = -1; private EditText editText; private RadioGroup radioGroup; @@ -48,12 +50,18 @@ public class TagEdit { */ private CheckBox editTextCheckBox; private EditText checkBoxEditText; - + + /** + * Factory Method that gives collection of instances. + * + * @return + */ public static List buildTagEdits() { - int idx = 0; tagEditHash = new LinkedHashMap<>(); + tagEditHiddenHash = new LinkedHashMap<>(); tagEdits = new ArrayList<>(); osmElement = OSMElement.getSelectedElements().getFirst(); + Map tags = osmElement.getTags(); // Tag Edits for ODK Collect Mode @@ -63,14 +71,22 @@ public static List buildTagEdits() { Collection requiredTags = odkCollectData.getRequiredTags(); for (ODKTag odkTag : requiredTags) { String tagKey = odkTag.getKey(); - TagEdit tagEdit = new TagEdit(tagKey, tags.get(tagKey), odkTag, false, idx++); - tagEditHash.put(tagKey, tagEdit); - tagEdits.add(tagEdit); + TagEdit tagEdit = new TagEdit(tagKey, tagValueOrDefaultValue(tags, tagKey), odkTag, false); + String implicitVal = Constraints.singleton().implicitVal(tagKey); + if (implicitVal != null) { + tagEditHiddenHash.put(tagKey, tagEdit); + osmElement.addOrEditTag(tagKey, implicitVal); + } else if (Constraints.singleton().tagShouldBeShown(tagKey, osmElement)) { + tagEditHash.put(tagKey, tagEdit); + tagEdits.add(tagEdit); + } else { + tagEditHiddenHash.put(tagKey, tagEdit); + } readOnlyTags.remove(tagKey); } Set readOnlyKeys = readOnlyTags.keySet(); for (String readOnlyKey : readOnlyKeys) { - TagEdit tagEdit = new TagEdit(readOnlyKey, readOnlyTags.get(readOnlyKey), true, idx++); + TagEdit tagEdit = new TagEdit(readOnlyKey, readOnlyTags.get(readOnlyKey), true); tagEditHash.put(readOnlyKey, tagEdit); tagEdits.add(tagEdit); } @@ -80,7 +96,7 @@ public static List buildTagEdits() { else { Set keys = tags.keySet(); for (String key : keys) { - TagEdit tagEdit = new TagEdit(key, tags.get(key), false, idx++); + TagEdit tagEdit = new TagEdit(key, tags.get(key), false); tagEditHash.put(key, tagEdit); tagEdits.add(tagEdit); } @@ -88,6 +104,22 @@ public static List buildTagEdits() { return tagEdits; } + + private static String tagValueOrDefaultValue(Map tags, String tagKey) { + String tagVal = tags.get(tagKey); + if (tagVal == null) { + // null if there is no default + tagVal = Constraints.singleton().tagDefaultValue(tagKey); + if (tagVal != null) { + osmElement.addOrEditTag(tagKey, tagVal); + } + } + return tagVal; + } + + public static void setTagSwipeActivity(TagSwipeActivity tagSwipeActivity) { + TagEdit.tagSwipeActivity = tagSwipeActivity; + } public static TagEdit getTag(int idx) { return tagEdits.get(idx); @@ -97,18 +129,56 @@ public static TagEdit getTag(String key) { return tagEditHash.get(key); } + public static Set hiddenTagKeys() { + return tagEditHash.keySet(); + } + + public static Set shownTagKeys() { + return tagEditHiddenHash.keySet(); + } + public static int getIndexForTagKey(String key) { TagEdit tagEdit = tagEditHash.get(key); if (tagEdit != null) { - return tagEdit.getIndex(); + return tagEdits.indexOf(tagEdit); } // If its not there, just go to the first TagEdit return 0; } - public static void saveToODKCollect(String osmUserName) { + public static boolean saveToODKCollect(String osmUserName) { updateTagsInOSMElement(); - ODKCollectHandler.saveXmlInODKCollect(osmElement, osmUserName); + + Set missingTags = Constraints.singleton().requiredTagsNotMet(osmElement); + if (missingTags.size() > 0) { + tagSwipeActivity.notifyMissingTags(missingTags); + return false; + } else { + ODKCollectHandler.saveXmlInODKCollect(osmElement, osmUserName); + return true; + } + } + + private static void removeTag(String key, String activeTagKey) { + if (tagEditHash.get(key) == null) return; + int idx = getIndexForTagKey(key); + TagEdit tagEdit = tagEditHash.remove(key); + tagEditHiddenHash.put(key, tagEdit); + tagEdits.remove(idx); + if (tagSwipeActivity != null) { + tagSwipeActivity.updateUI(activeTagKey); + } + } + + private static void addTag(String key, String activeTagKey) { + if (tagEditHiddenHash.get(key) == null) return; + int idx = getIndexForTagKey(activeTagKey) + 1; + TagEdit tagEdit = tagEditHiddenHash.remove(key); + tagEditHash.put(key, tagEdit); + tagEdits.add(idx, tagEdit); + if (tagSwipeActivity != null) { + tagSwipeActivity.updateUI(activeTagKey); + } } private static void updateTagsInOSMElement() { @@ -117,19 +187,17 @@ private static void updateTagsInOSMElement() { } } - private TagEdit(String tagKey, String tagVal, ODKTag odkTag, boolean readOnly, int idx) { + private TagEdit(String tagKey, String tagVal, ODKTag odkTag, boolean readOnly) { this.tagKey = tagKey; this.tagVal = tagVal; this.odkTag = odkTag; this.readOnly = readOnly; - this.idx = idx; } - private TagEdit(String tagKey, String tagVal, boolean readOnly, int idx) { + private TagEdit(String tagKey, String tagVal, boolean readOnly) { this.tagKey = tagKey; this.tagVal = tagVal; this.readOnly = readOnly; - this.idx = idx; } /** @@ -166,35 +234,67 @@ public void updateTagInOSMElement() { } else { tagVal = odkTag.getSemiColonDelimitedTagValues(null); } - osmElement.addOrEditTag(tagKey, tagVal); + addOrEditTag(tagKey, tagVal); } else { - osmElement.deleteTag(tagKey); + deleteTag(tagKey); } return; } // radio buttons if (radioGroup != null && odkTag != null) { View v = radioGroup.getChildAt(radioGroup.getChildCount() - 1); + int checkedId = radioGroup.getCheckedRadioButtonId(); + // has custom value input if (v instanceof LinearLayout) { LinearLayout customLL = (LinearLayout)v; RadioButton customRadio = (RadioButton)customLL.getChildAt(0); - int checkedId = radioGroup.getCheckedRadioButtonId(); if (customRadio.isChecked()) { EditText et = (EditText)customLL.getChildAt(1); tagVal = et.getText().toString(); - osmElement.addOrEditTag(tagKey, tagVal); + addOrEditTag(tagKey, tagVal); } else if (checkedId != -1) { tagVal = odkTag.getTagItemValueFromButtonId(checkedId); - osmElement.addOrEditTag(tagKey, tagVal); + addOrEditTag(tagKey, tagVal); } else { - osmElement.deleteTag(tagKey); + deleteTag(tagKey); + } + } + // no custom value input + else { + if (checkedId != -1) { + tagVal = odkTag.getTagItemValueFromButtonId(checkedId); + addOrEditTag(tagKey, tagVal); + } else { + deleteTag(tagKey); } } } // edit text else if (editText != null) { tagVal = editText.getText().toString(); - osmElement.addOrEditTag(tagKey, tagVal); + addOrEditTag(tagKey, tagVal); + } + } + + private void addOrEditTag(String tagKey, String tagVal) { + osmElement.addOrEditTag(tagKey, tagVal); + Constraints.TagAction tagAction = Constraints.singleton().tagAddedOrEdited(tagKey, tagVal); + executeTagAction(tagAction); + } + + private void deleteTag(String tagKey) { + osmElement.deleteTag(tagKey); + tagVal = null; + Constraints.TagAction tagAction = Constraints.singleton().tagDeleted(tagKey); + executeTagAction(tagAction); + } + + private void executeTagAction(Constraints.TagAction tagAction) { + for (String tag : tagAction.hide) { + removeTag(tag, tagKey); + } + for (String tag : tagAction.show) { + addTag(tag, tagKey); } } @@ -244,16 +344,17 @@ public boolean isReadOnly() { } public boolean isSelectOne() { - if ( !readOnly && + return !readOnly && odkTag != null && - odkTag.getItems().size() > 0 ) { - return true; - } - return false; + odkTag.getItems().size() > 0 && + !Constraints.singleton().tagIsSelectMultiple(odkTag.getKey()); } - - public int getIndex() { - return idx; + + public boolean isSelectMultiple() { + return !readOnly && + odkTag != null && + odkTag.getItems().size() > 0 && + Constraints.singleton().tagIsSelectMultiple(odkTag.getKey()); } } diff --git a/app/src/main/java/org/redcross/openmapkit/tagswipe/TagSwipeActivity.java b/app/src/main/java/org/redcross/openmapkit/tagswipe/TagSwipeActivity.java index 62d43e0..dd64cc3 100644 --- a/app/src/main/java/org/redcross/openmapkit/tagswipe/TagSwipeActivity.java +++ b/app/src/main/java/org/redcross/openmapkit/tagswipe/TagSwipeActivity.java @@ -1,24 +1,28 @@ package org.redcross.openmapkit.tagswipe; import java.util.List; -import java.util.Locale; +import java.util.Set; import android.app.Activity; -import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; +import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; +import android.graphics.Color; +import android.support.design.widget.Snackbar; +import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.view.PagerAdapter; import android.support.v7.app.ActionBarActivity; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v7.app.AlertDialog; import android.text.InputType; import android.view.Menu; import android.view.MenuItem; +import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; @@ -33,18 +37,11 @@ public class TagSwipeActivity extends ActionBarActivity { private void setupModel() { tagEdits = TagEdit.buildTagEdits(); + TagEdit.setTagSwipeActivity(this); userNamePref = getSharedPreferences("org.redcross.openmapkit.USER_NAME", Context.MODE_PRIVATE); } - - /** - * The {@link android.support.v4.view.PagerAdapter} that will provide - * fragments for each of the sections. We use a - * {@link FragmentPagerAdapter} derivative, which will keep every - * loaded fragment in memory. If this becomes too memory intensive, it - * may be best to switch to a - * {@link android.support.v4.app.FragmentStatePagerAdapter}. - */ + SectionsPagerAdapter mSectionsPagerAdapter; /** @@ -64,7 +61,7 @@ protected void onCreate(Bundle savedInstanceState) { mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. - mViewPager = (ViewPager) findViewById(R.id.pager); + mViewPager = (ViewPager) findViewById(R.id.tagSwipeActivity); mViewPager.setAdapter(mSectionsPagerAdapter); pageToCorrectTag(); @@ -109,9 +106,10 @@ public void saveToODKCollect() { if (userName == null) { askForOSMUsername(); } else { - TagEdit.saveToODKCollect(userName); - setResult(Activity.RESULT_OK); - finish(); + if (TagEdit.saveToODKCollect(userName)) { + setResult(Activity.RESULT_OK); + finish(); + } } } @@ -134,19 +132,62 @@ public void onClick(DialogInterface dialog, int which) { SharedPreferences.Editor editor = userNamePref.edit(); editor.putString("userName", userName); editor.apply(); - TagEdit.saveToODKCollect(userName); - setResult(Activity.RESULT_OK); - finish(); + if (TagEdit.saveToODKCollect(userName)) { + setResult(Activity.RESULT_OK); + finish(); + } } }); builder.show(); } + public void updateUI(String activeTagKey) { + mSectionsPagerAdapter.notifyDataSetChanged(); + int idx = TagEdit.getIndexForTagKey(activeTagKey); + mViewPager.setCurrentItem(idx); + } + /** - * A {@link FragmentPagerAdapter} that returns a fragment corresponding to - * one of the sections/tabs/pages. + * Only call if you have more than one missing tag. + * + * @param missingTags - tags that are required that are missing. */ - public class SectionsPagerAdapter extends FragmentPagerAdapter { + public void notifyMissingTags(final Set missingTags) { + Snackbar.make(findViewById(R.id.tagSwipeActivity), + "There are " + missingTags.size() + " required tags that you need to complete: " + missingTagsText(missingTags), + Snackbar.LENGTH_LONG) + .setAction("OK", new View.OnClickListener() { + // undo action + @Override + public void onClick(View v) { + try { + String missingTag = missingTags.iterator().next(); + int idx = TagEdit.getIndexForTagKey(missingTag); + mViewPager.setCurrentItem(idx); + } catch (Exception e) { + // do nothing + } + } + }) + .setActionTextColor(Color.rgb(126, 188, 111)) + .show(); + } + + private String missingTagsText(Set missingTags) { + String str = ""; + boolean first = true; + for (String tag: missingTags) { + if (first) { + str += tag; + } else { + str += ", " + tag; + } + first = false; + } + return str; + } + + public class SectionsPagerAdapter extends FragmentStatePagerAdapter { private Fragment fragment; @@ -179,7 +220,10 @@ public Fragment getItem(int position) { return fragment; } else if (tagEdit.isSelectOne()) { fragment = SelectOneTagValueFragment.newInstance(position); -// fragment = SelectMultipleTagValueFragment.newInstance(position); + return fragment; + } + else if (tagEdit.isSelectMultiple()) { + fragment = SelectMultipleTagValueFragment.newInstance(position); return fragment; } else { fragment = StringTagValueFragment.newInstance(position); @@ -200,6 +244,14 @@ public int getCount() { return tagEdits.size() + 1; } + //this is called when notifyDataSetChanged() is called + @Override + public int getItemPosition(Object object) { + // refresh all fragments when data set changed + return PagerAdapter.POSITION_NONE; + } + + @Override public CharSequence getPageTitle(int position) { if (position < tagEdits.size()) { diff --git a/app/src/main/res/layout/activity_tag_swipe.xml b/app/src/main/res/layout/activity_tag_swipe.xml index 4e23078..eae0c47 100644 --- a/app/src/main/res/layout/activity_tag_swipe.xml +++ b/app/src/main/res/layout/activity_tag_swipe.xml @@ -1,5 +1,5 @@ diff --git a/app/src/main/res/layout/fragment_select_multiple_tag_value.xml b/app/src/main/res/layout/fragment_select_multiple_tag_value.xml index dfaadd1..dca8db6 100644 --- a/app/src/main/res/layout/fragment_select_multiple_tag_value.xml +++ b/app/src/main/res/layout/fragment_select_multiple_tag_value.xml @@ -9,8 +9,19 @@ android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="20dp" - android:paddingTop="20dp" - android:paddingRight="20dp"> + android:paddingTop="2dp" + android:paddingRight="6dp"> + + + android:paddingRight="6dp" + android:paddingTop="2dp"> + + + android:paddingLeft="20dp" + android:paddingRight="6dp" + android:paddingTop="2dp"> + + - diff --git a/app/src/main/res/layout/taglistviewitem.xml b/app/src/main/res/layout/taglistviewitem.xml index 8fb9cd0..3823be6 100644 --- a/app/src/main/res/layout/taglistviewitem.xml +++ b/app/src/main/res/layout/taglistviewitem.xml @@ -1,6 +1,6 @@ - @@ -8,20 +8,19 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textViewTagKey" - android:layout_marginTop="15dp" + android:layout_marginTop="4dp" android:layout_marginLeft="20dp" - android:layout_marginBottom="15dp" - android:textSize="18sp" - android:layout_alignParentLeft="true"/> + android:textSize="16sp" + android:textStyle="bold" + android:textColor="@color/osm_dark_green" /> + android:layout_marginTop="0dp" + android:layout_marginLeft="20dp" + android:layout_marginBottom="6dp" + android:textSize="20sp" /> - \ No newline at end of file +