Commits

Tobias Duehr committed 9bd77c7

Branches are now fetched in RepositoryActivity,
no more hard coded "master" or "tip", should fix #25.

AsyncLoader has a third optional parameter for a custom error message.
This was implemented for a potential error if a folder does not exist after
switching the branch.

fork_28x32.png has 80% opacity now.

The owner and the slug appear now in the subtitle of RepositoryActivity.

Removed WikiParser.jar - it's not needed since a looong time.

Comments (0)

Files changed (5)

lib/WikiParser.jar

Binary file removed.

res/drawable-hdpi/fork_28x32.png

Old
Old image
New
New image

src/com/saibotd/bitbeaker/MyActivity.java

 
 	protected class AsyncLoader extends AsyncTask<String, Integer, String> {
 
+		private String errorMSG = "There was a problem retrieving and/or processing data from Bitbucket.org. Checking your data connectivity and trying again might help.";
+		
 		@Override
 		protected String doInBackground(String... params) {
 			String postParams = null;
-			if(params.length == 2) postParams = params[1];
+			if(params.length >= 2) postParams = params[1];
+			if(params.length == 3) errorMSG = params[2];
 			return getAuthHTTPS(params[0], postParams);
 		}
 		
 				try {
 					alertDialog = new AlertDialog.Builder(MyActivity.this).create();
 					alertDialog.setTitle("Oops.");
-					alertDialog.setMessage("There was a problem retrieving and/or processing data from Bitbucket.org. Checking your data connectivity and trying again might help.");
+					alertDialog.setMessage(errorMSG);
 					alertDialog.show();
 				} catch (Exception e) {}
 				result = "";

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;
 	private String slug;
 	private String owner;
 	private String scm;
+	private String[] branches = new String[0];
 	private Boolean firstCall;
 	private Boolean hasIssues = false;
 	private Boolean hasWiki = false;
         scm = b.getString("scm");
         owner = b.getString("owner");
         firstCall = true;
-        Log.d("SLUG", slug);
-        Log.d("SCM", scm);
-        Log.d("OWNER", owner);
         setTitle(slug);
         AsyncLoader asyncLoader = new AsyncLoader();
         asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+ slug +"/");
+        listBranches();
     }
     
+    private void listBranches() {
+		new AsyncBranchesLoader().execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/" + slug + "/branches");
+	}
+    
+    protected class AsyncBranchesLoader extends AsyncLoader {
+		@Override
+		protected void onPostExecute(String result) {
+			JSONObject json;
+			try {
+				json = new JSONObject(result);
+				JSONArray names = json.names();
+				branches = new String[names.length()];
+				for (int i=0; i<names.length(); i++)
+					branches[i] = names.getString(i);
+				invalidateOptionsMenu();
+			} catch (JSONException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+		}
+	}
+    
     public boolean onCreateOptionsMenu(Menu menu) {
         MenuInflater inflater = getSupportMenuInflater();
         inflater.inflate(R.menu.menu_repository, menu);
     
     @Override
     public boolean onPrepareOptionsMenu (Menu menu) {
+    	if (branches.length > 0) menu.getItem(0).setVisible(true);
+        else menu.getItem(0).setVisible(false);
         if (hasIssues) menu.getItem(1).setVisible(true);
         else menu.getItem(1).setVisible(false);
         if (hasWiki) menu.getItem(2).setVisible(true);
 		b.putString("slug", slug);
 		b.putString("owner", owner);
 		b.putString("scm", scm);
+		b.putStringArray("branches", branches);
 		b.putString("subdir", "/");
 	    switch (item.getItemId()) {
 		    case R.id.menu_source:
 				if(jsonObject.has("has_issues") && jsonObject.getBoolean("has_issues")) hasIssues = true;
 				invalidateOptionsMenu();
 				setTitle(jsonObject.getString("name"));
-				String desc = "<b>" + jsonObject.getString("owner") + " / " + jsonObject.getString("slug") + "</b>";
-				if(!jsonObject.getString("description").trim().equals("")) desc += "<br>" + jsonObject.getString("description");
+				getSupportActionBar().setSubtitle(jsonObject.getString("owner") + " / " + jsonObject.getString("slug"));
+				String desc = "";
+				if(!jsonObject.getString("description").trim().equals("")) desc += jsonObject.getString("description");
 				if(!jsonObject.getString("website").trim().equals("") && !jsonObject.getString("website").trim().equals("null")) desc += "<br><a href='"+ jsonObject.getString("website") +"'>" + jsonObject.getString("website") + "</a>";
 				description.setText(Html.fromHtml(desc));
 				if(jsonObject.getString("logo") != "null"){

src/com/saibotd/bitbeaker/SourceBrowserActivity.java

         scm = b.getString("scm");
         owner = b.getString("owner");
         subdir = b.getString("subdir");
+        branches = b.getStringArray("branches");
+        branch = branches[0];
         setContentView(R.layout.repositories);
         ActionBar actionBar = getSupportActionBar();
-        setTitle(slug);
+        setTitle(slug + " ("+branch+")");
         actionBar.setSubtitle(subdir);
 
-        branches = new String[1];
-        if (scm.equals("git")) branches[0] = "master";
-        else branches[0] = "default";
-        if (b.containsKey("branch")) branch = b.getString("branch");
-        else branch = branches[0];
-        listBranches();
-
         getData();
     }
 
-	private void listBranches() {
-		bitbeaker.delKV("https://api.bitbucket.org/1.0/repositories/" + owner + "/" + slug + "/branches");
-		new AsyncBranchesLoader().execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/" + slug + "/branches");
-	}
-
 	private void getData() {
-		new AsyncLoader().execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/" + slug + "/src/" + branch + encode(subdir));
-	}
-
-	protected class AsyncBranchesLoader extends AsyncLoader {
-		@Override
-		protected void onPostExecute(String result) {
-			AsyncBranchesLoaderDone(result);
-		}
-	}
-
-	protected void AsyncBranchesLoaderDone(String result) {
-		super.AsyncLoaderDone(result);
-		JSONObject json;
-		try {
-			json = new JSONObject(result);
-			JSONArray names = json.names();
-			branches = new String[names.length()];
-			for (int i=0; i<names.length(); i++)
-				branches[i] = names.getString(i);
-			setBranchesMenuItems(mainMenu);
-		} catch (JSONException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
+		new AsyncLoader().execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/" + slug + "/src/" + branch + encode(subdir),
+				null,
+				"The folder \"" +subdir+ "\" does not exist in the \""+branch+"\" branch, or there was a problem retrieving and/or processing data from Bitbucket.org"
+		);
 	}
 
 	@Override
 	public boolean onOptionsItemSelected(MenuItem item) {
 		if (item.getGroupId() == BRANCHES_GROUP_ID) {
 			branch = item.getTitle().toString();
+	        setTitle(slug + " ("+branch+")");
 			getData();
 			return true;
 		}
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
-		setTitle(slug + " ("+branch+")");
 		listView.setAdapter(new SourceBrowserAdapter(this, jsonArray));
 	}
     
     	
     	private OnClickListener clickListener = new OnClickListener() {
     		public void onClick(View v) {
-    			try {
+    			try {    				
     				String rec = getItem(v.getId()).getString("name");
+    				Bundle b = new Bundle();
+					b.putString("slug", slug);
+					b.putString("scm", scm);
+					b.putString("owner", owner);
+					b.putString("branch", branch);
+					b.putStringArray("branches", branches);
     				if(rec.substring(0, 4).equals("dir_")){
-    					Bundle b = new Bundle();
-    					b.putString("slug", slug);
-    					b.putString("scm", scm);
-    					b.putString("owner", owner);
     					b.putString("subdir", subdir + rec.substring(4) + "/");
-    					b.putString("branch", branch);
     					Intent intent = new Intent(v.getContext(), SourceBrowserActivity.class);
     					intent.putExtras(b);
     					v.getContext().startActivity(intent);
     				} else {
-    					Bundle b = new Bundle();
-    					b.putString("slug", slug);
-    					b.putString("scm", scm);
-    					b.putString("owner", owner);
     					b.putString("file", subdir + rec.substring(4));
-    					b.putString("branch", branch);
     					Intent intent = new Intent(v.getContext(), SourceActivity.class);
     					intent.putExtras(b);
     					v.getContext().startActivity(intent);