Skip to content

Commit 3d402c7

Browse files
committed
OSM User Name Dialog & Storing in sharedPreferences #111
1 parent d2966ec commit 3d402c7

5 files changed

Lines changed: 54 additions & 16 deletions

File tree

app/src/main/java/org/redcross/openmapkit/odkcollect/ODKCollectData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ public String getTagValueLabel(String key, String value) {
134134
return null;
135135
}
136136

137-
public void consumeOSMElement(OSMElement el) throws IOException {
137+
public void consumeOSMElement(OSMElement el, String osmUserName) throws IOException {
138138
osmClassName = el.getClass().getSimpleName();
139139
osmId = el.getId();
140-
editedXml = OSMXmlWriter.elementToString(el, "theoutpost", APP_NAME + " " + appVersion);
140+
editedXml = OSMXmlWriter.elementToString(el, osmUserName, APP_NAME + " " + appVersion);
141141
}
142142

143143
public void deleteOldOSMEdit() {

app/src/main/java/org/redcross/openmapkit/odkcollect/ODKCollectHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ public static ODKCollectData getODKCollectData() {
7070
* @param el
7171
* @return The full path of the saved OSM XML File
7272
*/
73-
public static String saveXmlInODKCollect(OSMElement el) {
73+
public static String saveXmlInODKCollect(OSMElement el, String osmUserName) {
7474
try {
75-
odkCollectData.consumeOSMElement(el);
75+
odkCollectData.consumeOSMElement(el, osmUserName);
7676
odkCollectData.deleteOldOSMEdit();
7777
odkCollectData.writeXmlToOdkCollectInstanceDir();
7878
return odkCollectData.getOSMFileFullPath();

app/src/main/java/org/redcross/openmapkit/odkcollect/ODKCollectTagActivity.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
6868
}
6969

7070
void saveToOdkCollect() {
71-
OSMElement osmElement = fragment.updateTagsInOSMElement();
72-
String osmXmlFileFullPath = ODKCollectHandler.saveXmlInODKCollect(osmElement);
73-
74-
Intent resultIntent = new Intent();
75-
resultIntent.putExtra("OSM_PATH", osmXmlFileFullPath);
76-
setResult(Activity.RESULT_OK, resultIntent);
71+
// OSMElement osmElement = fragment.updateTagsInOSMElement();
72+
// String osmXmlFileFullPath = ODKCollectHandler.saveXmlInODKCollect(osmElement);
73+
//
74+
// Intent resultIntent = new Intent();
75+
// resultIntent.putExtra("OSM_PATH", osmXmlFileFullPath);
76+
// setResult(Activity.RESULT_OK, resultIntent);
7777

7878
finish();
7979
}

app/src/main/java/org/redcross/openmapkit/tagswipe/TagEdit.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ public static int getIndexForTagKey(String key) {
9494
return 0;
9595
}
9696

97-
public static void saveToODKCollect() {
97+
public static void saveToODKCollect(String osmUserName) {
9898
updateTagsInOSMElement();
99-
ODKCollectHandler.saveXmlInODKCollect(osmElement);
99+
ODKCollectHandler.saveXmlInODKCollect(osmElement, osmUserName);
100100
}
101101

102102
private static void updateTagsInOSMElement() {

app/src/main/java/org/redcross/openmapkit/tagswipe/TagSwipeActivity.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
import android.app.Activity;
77
import android.app.ProgressDialog;
88
import android.content.Context;
9+
import android.content.DialogInterface;
10+
import android.content.SharedPreferences;
911
import android.content.res.Resources;
1012
import android.support.v7.app.ActionBarActivity;
1113
import android.support.v4.app.Fragment;
1214
import android.support.v4.app.FragmentManager;
1315
import android.support.v4.app.FragmentPagerAdapter;
1416
import android.os.Bundle;
1517
import android.support.v4.view.ViewPager;
18+
import android.support.v7.app.AlertDialog;
19+
import android.text.InputType;
1620
import android.view.Menu;
1721
import android.view.MenuItem;
1822
import android.view.inputmethod.InputMethodManager;
@@ -24,10 +28,12 @@
2428
public class TagSwipeActivity extends ActionBarActivity {
2529

2630
private List<TagEdit> tagEdits;
31+
private SharedPreferences userNamePref;
2732

2833

2934
private void setupModel() {
3035
tagEdits = TagEdit.buildTagEdits();
36+
userNamePref = getSharedPreferences("org.redcross.openmapkit.USER_NAME", Context.MODE_PRIVATE);
3137
}
3238

3339

@@ -93,17 +99,49 @@ public boolean onOptionsItemSelected(MenuItem item) {
9399
return super.onOptionsItemSelected(item);
94100
}
95101

102+
/**
103+
* We check to see if there is a saved user name. If there is not,
104+
* we present a dialog to ask for it. Otherwise, we just use what
105+
* is saved for writing OSM XML and saving to ODK Collect.
106+
*/
96107
public void saveToODKCollect() {
97-
TagEdit.saveToODKCollect();
98-
setResult(Activity.RESULT_OK);
99-
finish();
108+
String userName = userNamePref.getString("userName", null);
109+
if (userName == null) {
110+
askForOSMUsername();
111+
} else {
112+
TagEdit.saveToODKCollect(userName);
113+
setResult(Activity.RESULT_OK);
114+
finish();
115+
}
100116
}
101117

102118
public void cancel() {
103119
setResult(Activity.RESULT_CANCELED);
104120
finish();
105121
}
106-
122+
123+
private void askForOSMUsername() {
124+
AlertDialog.Builder builder = new AlertDialog.Builder(this);
125+
builder.setTitle("OpenStreetMap User Name");
126+
builder.setMessage("Please enter your OpenStreetMap user name.");
127+
final EditText input = new EditText(this);
128+
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
129+
builder.setView(input);
130+
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
131+
@Override
132+
public void onClick(DialogInterface dialog, int which) {
133+
String userName = input.getText().toString();
134+
SharedPreferences.Editor editor = userNamePref.edit();
135+
editor.putString("userName", userName);
136+
editor.apply();
137+
TagEdit.saveToODKCollect(userName);
138+
setResult(Activity.RESULT_OK);
139+
finish();
140+
}
141+
});
142+
builder.show();
143+
}
144+
107145
/**
108146
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
109147
* one of the sections/tabs/pages.

0 commit comments

Comments
 (0)