Ville Saalo avatar Ville Saalo committed 61ab13d

On the repository page, print some info if the repository is a fork of some other repository.

Comments (0)

Files changed (4)

res/values/strings.xml

     
     <!-- RepositoriesActivity.java -->
     <string name="your_repositories">Your repositories</string>
+    <string name="fork_of">Fork of</string>
     
     <!-- SearchableActivity -->
     <string name="please_enter_more_text">Please enter at least 3 letters.</string>

src/com/saibotd/bitbeaker/Helper.java

 		return string.trim().equals("");
 	}
 	
+	public static boolean isJsonEmpty(final String string) {
+		return isEmpty(string) || string.trim().equals("null");
+	}
+	
 	public static JSONArray reverseJSONObjectArray(JSONArray array) throws JSONException{
 		JSONArray tmp = new JSONArray();
 		for(int i = array.length()-1; i >= 0; i--){
 		}
 		return tmp;
 	}
+	
+	/**
+	 * Turns the given URL into an HTML link
+	 * @param url An URL, 'http://www.example.com/', for example.
+	 * @return HTML link, e.g. '<a href="http://www.example.com/">http://www.example.com/</a>'.
+	 */
+	public static String getHtmlLink(String url) {
+		return getHtmlLink(url, url);
+	}
+	
+	/**
+	 * Turns the given URL and text into an HTML link
+	 * @param url An URL, 'http://www.example.com/', for example
+	 * @param text The link text, e.g. 'Click here!'
+	 * @return HTML link, e.g. '<a href="http://www.example.com/">Click here!</a>'.
+	 */
+	public static String getHtmlLink(String url, String text) {
+		return "<a href=\"" + url + "\">" + text + "</a>";
+	}
 }

src/com/saibotd/bitbeaker/RepositoryActivity.java

 import android.content.Intent;
 import android.os.Bundle;
 import android.text.Html;
+import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 			firstCall = false;
 			
 			//TextView name = (TextView) findViewById(R.id.header_title);
-			TextView description = (TextView) findViewById(R.id.repository_description);
+			TextView repositoryDescription = (TextView) findViewById(R.id.repository_description);
 			
 			ImageView icon = (ImageView) findViewById(R.id.icon);
 			
 			try {
-				JSONObject jsonObject = new JSONObject(result);
-				hasWiki = jsonObject.has("has_wiki") && jsonObject.getBoolean("has_wiki");
-				hasIssues = jsonObject.has("has_issues") && jsonObject.getBoolean("has_issues");
+				JSONObject repository = new JSONObject(result);
+				hasWiki = repository.has("has_wiki") && repository.getBoolean("has_wiki");
+				hasIssues = repository.has("has_issues") && repository.getBoolean("has_issues");
 				invalidateOptionsMenu();
-				setTitle(jsonObject.getString("name"));
-				String desc = "";
-				if(!Helper.isEmpty(jsonObject.getString("description"))) desc += jsonObject.getString("description");
-				if(!Helper.isEmpty(jsonObject.getString("website")) && !jsonObject.getString("website").trim().equals("null")) desc += "<br><a href='"+ jsonObject.getString("website") +"'>" + jsonObject.getString("website") + "</a>";
-				description.setText(Html.fromHtml(desc));
-				if (!"null".equals(jsonObject.getString("logo"))) {
-					executeAsyncImageLoader(icon, jsonObject.getString("logo"));
+				setTitle(repository.getString("name"));
+				StringBuilder desc = new StringBuilder();
+				if (Boolean.parseBoolean(repository.getString("is_fork"))) {
+					final JSONObject fork = repository.getJSONObject("fork_of");
+					final String owner = fork.getString("owner");
+					final String slug = fork.getString("slug");
+					final String ownerUrl = "https://www.bitbucket.org/" + owner + "/";
+					final String slugUrl = ownerUrl + slug + "/";
+					desc.append("(").append(this.getString(R.string.fork_of)).append(" ");
+					desc.append(owner);
+					desc.append(" / ");
+					desc.append(slug);
+					if (!Helper.isJsonEmpty(fork.getString("description"))) {
+						desc.append(": <i>" + fork.getString("description") + "</i>");
+					}
+					desc.append(")<br>");
+					Log.d("RepositoryActivity", "ownerUrl: '"+ownerUrl+"'");
+					Log.d("RepositoryActivity", "slugUrl : '"+slugUrl+"'");
+				}
+				if (!Helper.isJsonEmpty(repository.getString("description"))) {
+					desc.append(repository.getString("description"));
+				}
+				if (!Helper.isJsonEmpty(repository.getString("website"))) {
+					desc.append("<br>").append(Helper.getHtmlLink(repository.getString("website")));
+				}
+				Log.d("RepositoryActivity", "repoUrl : '"+repository.getString("website")+"'");
+				repositoryDescription.setText(Html.fromHtml(desc.toString()));
+				if (!Helper.isJsonEmpty(repository.getString("logo"))) {
+					executeAsyncImageLoader(icon, repository.getString("logo"));
 				}
 			} catch (JSONException e) {
 				// TODO Auto-generated catch block

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

 		assertFalse(Helper.isEmpty("       asd        "));
 	}
 	
+	public void test_isJsonEmpty_with_empty_results() {
+		assertTrue("Null should be evaluated as empty!", Helper.isJsonEmpty(null));
+		assertTrue("Empty string should be evaluated as empty!", Helper.isJsonEmpty(""));
+		assertTrue("String consisting of whitespace should be evaluated as empty!", Helper.isJsonEmpty("   "));
+		assertTrue("String consisting of whitespace should be evaluated as empty!", Helper.isJsonEmpty(" \n\t \r"));
+		assertTrue("The literal 'null' should be evaluated as empty!", Helper.isJsonEmpty("null"));
+		assertTrue("The literal 'null' should be evaluated as empty even with whitespace around it!", Helper.isJsonEmpty("    null\n"));
+	}
+	
+	public void test_isJsonEmpty_with_nonempty_results() {
+		assertFalse(Helper.isJsonEmpty("test"));
+		assertFalse(Helper.isJsonEmpty("µ"));
+		assertFalse(Helper.isJsonEmpty("asd null          "));
+		assertFalse(Helper.isJsonEmpty("          null asd"));
+		assertFalse(Helper.isJsonEmpty("       asd null   "));
+	}
+	
 	public void test_reverseJSONObjectArray() throws JSONException {
 		JSONArray test = new JSONArray();
 		test.put(new JSONObject().put("key1", "value1"));
 		assertFalse(Helper.isImage(""));
 		assertFalse(Helper.isImage(null));
 	}
+	
+	public void test_getPlainHtmlLink_with_URL_only() {
+		assertEquals("<a href=\"http://www.example.com/foobar\">http://www.example.com/foobar</a>", 
+					Helper.getHtmlLink("http://www.example.com/foobar"));
+	}
+	
+	public void test_getPlainHtmlLink_with_URL_and_text() {
+		assertEquals("<a href=\"https://www.example.com/foobar\">Click for foobar</a>", 
+					Helper.getHtmlLink("https://www.example.com/foobar", "Click for foobar"));
+	}
 }
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.