Commits

Juha Kuitunen  committed b258fa6

added support for switching branches

  • Participants
  • Parent commits 4a9b952

Comments (0)

Files changed (2)

File src/com/saibotd/bitbeaker/SourceActivity.java

 import com.actionbarsherlock.view.Menu;
 
 public class SourceActivity extends MyActivity {
-    
+
 	private String slug;
-	private String scm;	
+	private String branch;
 	private String owner;
 	private String file;
 	private String code;
-	
+
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.source);
         Bundle b = getIntent().getExtras();
+        branch = b.getString("branch");
         slug = b.getString("slug");
-        scm = b.getString("scm");
         owner = b.getString("owner");
         file = b.getString("file");
-        AsyncLoader asyncLoader = new AsyncLoader();
-        if(scm.equals("git")) asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+slug+"/raw/master" + encode(file));
-        else asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/"+ owner +"/"+slug+"/raw/tip" + encode(file));
+        new AsyncLoader().execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/" + slug + "/raw/" + branch + encode(file));
         ActionBar actionBar = getSupportActionBar();
-        setTitle(slug);
+        setTitle(slug + " ("+branch+")");
         actionBar.setSubtitle(file);
     }
-    
-    @Override
-   	public boolean onCreateOptionsMenu(Menu menu) {
-   		return false;
-   	}
-	
+
+	@Override
+	public boolean onCreateOptionsMenu(Menu menu) {
+		return false;
+	}
+
 	@SuppressLint("SetJavaScriptEnabled")
 	public void AsyncLoaderDone(String result) {
 		super.AsyncLoaderDone(result);
 		webView.setWebChromeClient(new WebChromeClient());
 		webView.loadUrl("file:///android_asset/source.html");
 	}
-	
+
 	final class MyWebChromeClient extends WebChromeClient {
-        @Override
-        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
-            Log.d("JAVASCRIPT", message);
-            result.confirm();
-            return true;
-        }
-    }
-	
+		@Override
+		public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
+			Log.d("JAVASCRIPT", message);
+			result.confirm();
+			return true;
+		}
+	}
+
 	private class JavaScriptInterface{
 		@SuppressWarnings("unused")
 		public String getCode()  
-	    {  
-	         return code;
-	    }
+		{
+			return code;
+		}
 	}
-	
+
 }

File src/com/saibotd/bitbeaker/SourceBrowserActivity.java

 import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.TextView;
-import android.widget.Toast;
 
 import com.actionbarsherlock.app.ActionBar;
 import com.actionbarsherlock.view.Menu;
 	private String scm;
 	private String owner;
 	private String subdir;
+	private String branch;
 	private String[] branches;
 	private SubMenu branchesMenu;
 	private Menu mainMenu;
-	private static final int BRANCHES_GROUP_ID = 1;// some non-zero id
+	private static final int BRANCHES_GROUP_ID = 1;// some unique non-zero id for submenu
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         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();
 
-        AsyncLoader asyncLoader = new AsyncLoader();
-        if(scm.equals("git")) asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/"+slug+"/src/master" + encode(subdir));
-        else asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/"+slug+"/src/tip" + encode(subdir));
+        getData();
     }
 
 	private void listBranches() {
 		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) {
 	@Override
 	public boolean onOptionsItemSelected(MenuItem item) {
 		if (item.getGroupId() == BRANCHES_GROUP_ID) {
-			Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
+			branch = item.getTitle().toString();
+			getData();
 			return true;
 		}
 		else return super.onOptionsItemSelected(item);
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
+		setTitle(slug + " ("+branch+")");
 		listView.setAdapter(new SourceBrowserAdapter(this, jsonArray));
 	}
     
     					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);
     					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);