Ville Saalo avatar Ville Saalo committed 81d68be

Refactoring things and stuff, namely making the creation of URL parameters more elegant in the UpdateIssueActivity.

Comments (0)

Files changed (4)

src/com/saibotd/bitbeaker/Bitbeaker.java

 	public void onCreate(){
 		super.onCreate();
 		Helper.setContext(getApplicationContext());
-		Helper.initTranslatedApiStrings();
 		settings = getSharedPreferences("bitbeaker", 0);
 		kv = new LinkedHashMap<String,String>();
 	}

src/com/saibotd/bitbeaker/Helper.java

 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Map;
 
 import org.json.JSONArray;
 import org.json.JSONException;
 
 	public static void setContext(Context context){
 		Helper.context = context;
+		initTranslatedApiStrings();
 	}
 	
 	public static TwoWayMap<String,String> translatedApiStrings;
 	/**
 	 * Context MUST be set with setContext(Context) before you can call this!
 	 */
-	public static void initTranslatedApiStrings() {
+	private static void initTranslatedApiStrings() {
 		translatedApiStrings = new TwoWayMap<String,String>("", "ERROR: value not found");
 		translatedApiStrings.put("new", context.getString(R.string.api_status_new));
 		translatedApiStrings.put("open", context.getString(R.string.api_status_open));
 	public static String getHtmlLink(String url) {
 		return "<a href=\"" + url + "\">" + url + "</a>";
 	}
+
+	public static String convertMapToUrlParams(Map<String, String> postParams) {
+		if (postParams == null || postParams.isEmpty()) {
+			return "";
+		}
+		
+		StringBuilder result = new StringBuilder("?");
+		for (Map.Entry<String,String> param : postParams.entrySet()) {
+			result.append(param.getKey()).append("=").append(param.getValue()).append("&");			
+		}
+		return result.substring(0, result.length() - 1);
+	}
 }

src/com/saibotd/bitbeaker/UpdateIssueActivity.java

 package com.saibotd.bitbeaker;
 
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
 import org.json.JSONException;
 import org.json.JSONObject;
 
 				+ slug      + "/"
 				+ "issues/" 
 				+ issue_id  + "/";
-				String postParams = "";
 				
+			Map<String,String> postParams = new LinkedHashMap<String,String>();
+			
 			// Only add parameters that have changed.
 			if (!initialTitle.equals(titleEditor.getText().toString())) {
-
-				if (0 < postParams.length()) {
-					postParams += "&title=";
-				} else {
-					postParams = "title=";
-				}
-				postParams += Helper.encode(titleEditor.getText().toString());
-
+				postParams.put("title", Helper.encode(titleEditor.getText().toString()));
 			}
 
 			if (!initialContent.equals(contentEditor.getText().toString())) {
-
-				if (0 < postParams.length()) {
-					postParams += "&content=";
-				} else {
-					postParams = "content=";
-				}
-				postParams += Helper.encode(contentEditor.getText().toString());
+				postParams.put("content",contentEditor.getText().toString());
 			}
 
-			if (!initialType.equalsIgnoreCase(Helper.translatedApiStrings.getByValue(typeSpinner.getSelectedItem().toString()))) {
-
-				if (0 < postParams.length()) {
-					postParams += "&kind=";
-				} else {
-					postParams = "kind=";
-				}
-				postParams += Helper.translatedApiStrings.getByValue(typeSpinner.getSelectedItem().toString());
-
+			final String newType = Helper.translatedApiStrings.getByValue(typeSpinner.getSelectedItem().toString());
+			if (!initialType.equalsIgnoreCase(newType)) {
+				postParams.put("kind", newType);
 			}
 			
-			if (!initialPriority.equalsIgnoreCase(Helper.translatedApiStrings.getByValue(prioritySpinner.getSelectedItem().toString()))) {
-
-				if (0 < postParams.length()) {
-					postParams += "&priority=";
-				} else {
-					postParams = "priority=";
-				}
-				postParams += Helper.translatedApiStrings.getByValue(prioritySpinner.getSelectedItem().toString());
-
+			final String newPriority = Helper.translatedApiStrings.getByValue(prioritySpinner.getSelectedItem().toString());
+			if (!initialPriority.equalsIgnoreCase(newPriority)) {
+				postParams.put("priority", newPriority);
 			}
 			
-			if (!initialStatus.equalsIgnoreCase(Helper.translatedApiStrings.getByValue(statusSpinner.getSelectedItem().toString()))) {
-
-				if (0 < postParams.length()) {
-					postParams += "&status=";
-				} else {
-					postParams = "status=";
-				}
-				postParams += Helper.translatedApiStrings.getByValue(statusSpinner.getSelectedItem().toString());
-
+			final String newStatus = Helper.translatedApiStrings.getByValue(statusSpinner.getSelectedItem().toString());
+			if (!initialStatus.equalsIgnoreCase(newStatus)) {
+				postParams.put("status", newStatus);
 			}
 			
 			bitbeaker.delKV(updateURI);
-			executeAsyncLoader(updateURI, postParams, null, "PUT");
+			executeAsyncLoader(updateURI, Helper.convertMapToUrlParams(postParams), null, "PUT");
 			return true;
 
 		}

src/com/saibotd/bitbeaker/tests/HelperTest.java

 package com.saibotd.bitbeaker.tests;
 
+import java.util.LinkedHashMap;
+import java.util.Map;
+
 import junit.framework.TestCase;
 
 import org.json.JSONArray;
 		assertEquals("<a href=\"http://www.example.com/foobar\">http://www.example.com/foobar</a>", 
 					Helper.getHtmlLink("http://www.example.com/foobar"));
 	}
+
+	public void test_convertMapToUrlParams_with_null_param() {
+		assertEquals("", Helper.convertMapToUrlParams(null));
+	}
+	
+	public void test_convertMapToUrlParams_with_zero_params() {
+		Map<String, String> test = new LinkedHashMap<String, String>();
+		assertEquals("", Helper.convertMapToUrlParams(test));
+	}
+	
+	public void test_convertMapToUrlParams_with_one_param() {
+		Map<String, String> test = new LinkedHashMap<String, String>();
+		test.put("myparameter", "myvalue");
+		assertEquals("?myparameter=myvalue", Helper.convertMapToUrlParams(test));
+	}
+	
+	public void test_convertMapToUrlParams_with_three_params() {
+		Map<String, String> test = new LinkedHashMap<String, String>();
+		test.put("key1", "val1");
+		test.put("foo", "bar");
+		test.put("asdasd", "qwerty");
+		assertEquals("?key1=val1&foo=bar&asdasd=qwerty", Helper.convertMapToUrlParams(test));
+	}
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.