Permalink
Browse files

first round of refactoring

  • Loading branch information...
1 parent 297172c commit 973d9a5f1b975128a852bbc9be92d92d7cf18967 @mschoch committed Aug 22, 2011
View
191 src/com/couchbase/grocerysync/AndroidGrocerySyncActivity.java
@@ -17,7 +17,6 @@
import android.app.Activity;
import android.app.AlertDialog;
-import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
@@ -36,22 +35,17 @@
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
-import android.widget.ProgressBar;
-import android.widget.TextView;
-import android.widget.Toast;
import com.couchbase.libcouch.CouchbaseEmbeddedServer;
import com.couchbase.libcouch.ICouchClient;
-public class AndroidGrocerySyncActivity extends Activity {
+public class AndroidGrocerySyncActivity extends Activity implements OnItemClickListener, OnItemLongClickListener, OnKeyListener {
//constants
public static final String DATABASE_NAME = "grocery-sync";
//splash screen
- protected Dialog splashDialog;
- protected ProgressBar splashProgressBar;
- protected TextView splashProgressMessage;
+ protected SplashScreenDialog splashDialog;
//main screen
protected EditText addItemEditText;
@@ -70,39 +64,16 @@ public void onCreate(Bundle savedInstanceState) {
startCouch();
setContentView(R.layout.main);
-
-
+
//connect items from layout
addItemEditText = (EditText)findViewById(R.id.addItemEditText);
itemListView = (ListView)findViewById(R.id.itemListView);
//connect listeners
- addItemEditText.setOnKeyListener(new OnKeyListener() {
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- if ((event.getAction() == KeyEvent.ACTION_DOWN)
- && (keyCode == KeyEvent.KEYCODE_ENTER)) {
-
- String inputText = addItemEditText.getText().toString();
- if(!inputText.equals("")) {
-
- createGroceryItem(inputText);
-
- Toast.makeText(AndroidGrocerySyncActivity.this,
- inputText, Toast.LENGTH_SHORT)
- .show();
-
- }
- addItemEditText.setText("");
- return true;
- }
- return false;
- }
- });
+ addItemEditText.setOnKeyListener(this);
}
-
-
protected void onDestroy() {
super.onDestroy();
try {
@@ -114,67 +85,24 @@ protected void onDestroy() {
protected ICouchClient couchCallbackHandler = new ICouchClient.Stub() {
public void installing(int completed, int total) throws RemoteException {
- if(completed < (total - 1)) {
- AndroidGrocerySyncActivity.this.updateSplashScreenProgressBar(completed, total);
- AndroidGrocerySyncActivity.this.updateSplashScreenProgressMessage(getString(R.string.installing_message));
- }
- else {
- AndroidGrocerySyncActivity.this.updateSplashScreenProgressBar(completed, total);
- AndroidGrocerySyncActivity.this.updateSplashScreenProgressMessage(getString(R.string.startup_message));
- }
+ AndroidGrocerySyncActivity.this.splashDialog.updateSplashScreenProgress(completed, total);
}
public void exit(String error) throws RemoteException {
- // TODO Auto-generated method stub
- }
+ }
public void couchStarted(String host, int port) throws RemoteException {
AndroidGrocerySyncActivity.this.removeSplashScreen();
HttpClient httpClient = new StdHttpClient.Builder().host(host).port(port).build();
CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
couchDbConnector = dbInstance.createConnector(DATABASE_NAME, true);
- itemListView.setAdapter(new CouchListAdapter(AndroidGrocerySyncActivity.this, couchDbConnector));
- itemListView.setOnItemClickListener(new OnItemClickListener() {
-
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- JsonNode document = (JsonNode)parent.getItemAtPosition(position);
- toggleItemChecked(document);
- }
-
- });
- itemListView.setOnItemLongClickListener(new OnItemLongClickListener() {
-
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
-
- final JsonNode document = (JsonNode)parent.getItemAtPosition(position);
- JsonNode textNode = document.get("text");
-
- AlertDialog.Builder builder = new AlertDialog.Builder(AndroidGrocerySyncActivity.this);
- AlertDialog alert = builder.setTitle("Delete Item?")
- .setMessage("Are you sure you want to delete " + textNode.getValueAsText() + "?")
- .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- deleteGroceryItem(document);
- }
- })
- .setNegativeButton("No", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- // Handle Cancel
- }
- })
- .create();
-
- alert.show();
-
- return true;
- }
-
- });
+ //attach list adapter to the list and handle clicks
+ itemListView.setAdapter(new CouchListAdapter(AndroidGrocerySyncActivity.this, couchDbConnector));
+ itemListView.setOnItemClickListener(AndroidGrocerySyncActivity.this);
+ itemListView.setOnItemLongClickListener(AndroidGrocerySyncActivity.this);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
@@ -202,59 +130,89 @@ protected void startCouch() {
couchServiceConnection = couch.startCouchbase();
}
+
/**
- * Removes the Dialog that displays the splash screen
+ * Handle typing item text
*/
- protected void removeSplashScreen() {
- if (splashDialog != null) {
- splashDialog.dismiss();
- splashDialog = null;
- splashProgressBar = null;
- splashProgressMessage = null;
- }
+ public boolean onKey(View v, int keyCode, KeyEvent event) {
+ if ((event.getAction() == KeyEvent.ACTION_DOWN)
+ && (keyCode == KeyEvent.KEYCODE_ENTER)) {
+
+ String inputText = addItemEditText.getText().toString();
+ if(!inputText.equals("")) {
+ createGroceryItem(inputText);
+ }
+ addItemEditText.setText("");
+ return true;
+ }
+ return false;
}
/**
- * Update the Splash Screen Progress Bar
- */
- protected void updateSplashScreenProgressBar(int progress, int max) {
- if(splashProgressBar != null) {
- splashProgressBar.setProgress(progress);
- splashProgressBar.setMax(max);
- }
+ * Handle click on item in list
+ */
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+ JsonNode document = (JsonNode)parent.getItemAtPosition(position);
+ toggleItemChecked(document);
}
/**
- * Update the Splash Screen Progress Message
+ * Handle long-click on item in list
*/
- protected void updateSplashScreenProgressMessage(String message) {
- if(splashProgressMessage != null) {
- splashProgressMessage.setText(message);
- }
+ public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
+
+ final JsonNode document = (JsonNode)parent.getItemAtPosition(position);
+ JsonNode textNode = document.get("text");
+
+ AlertDialog.Builder builder = new AlertDialog.Builder(AndroidGrocerySyncActivity.this);
+ AlertDialog alert = builder.setTitle("Delete Item?")
+ .setMessage("Are you sure you want to delete " + textNode.getValueAsText() + "?")
+ .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int id) {
+ deleteGroceryItem(document);
+ }
+ })
+ .setNegativeButton("No", new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int id) {
+ // Handle Cancel
+ }
+ })
+ .create();
+
+ alert.show();
+
+ return true;
+ }
+
+ /**
+ * Removes the Dialog that displays the splash screen
+ */
+ protected void removeSplashScreen() {
+ if (splashDialog != null) {
+ splashDialog.dismiss();
+ splashDialog = null;
+ }
}
/**
* Shows the splash screen over the full Activity
*/
protected void showSplashScreen() {
- splashDialog = new Dialog(this, R.style.SplashScreenStyle);
- splashDialog.setContentView(R.layout.splashscreen);
- splashDialog.setCancelable(false);
+ splashDialog = new SplashScreenDialog(this);
splashDialog.show();
-
- splashProgressBar = (ProgressBar)splashDialog.findViewById(R.id.splashProgressBar);
- splashProgressBar.setProgress(0);
- splashProgressBar.setMax(100);
-
- splashProgressMessage = (TextView)splashDialog.findViewById(R.id.splashProgressMessage);
- splashProgressMessage.setText(getString(R.string.startup_message));
}
+ /**
+ * Add settings item to the menu
+ */
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 0, 0, "Settings");
return super.onCreateOptionsMenu(menu);
}
+ /**
+ * Launch the settings activity
+ */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
@@ -278,7 +236,8 @@ public void createGroceryItem(String name) {
newItem.put("check", Boolean.FALSE.toString());
newItem.put("created_at", currentTimeString);
- couchDbConnector.create(newItem);
+ CouchDocumentAsyncTask createTask = new CouchDocumentAsyncTask(couchDbConnector, CouchDocumentAsyncTask.OPERATION_CREATE);
+ createTask.execute(newItem);
}
public void toggleItemChecked(JsonNode document) {
@@ -292,11 +251,13 @@ public void toggleItemChecked(JsonNode document) {
documentObject.put("check", true);
}
- couchDbConnector.update(document);
+ CouchDocumentAsyncTask updateTask = new CouchDocumentAsyncTask(couchDbConnector, CouchDocumentAsyncTask.OPERATION_UPDATE);
+ updateTask.execute(document);
}
public void deleteGroceryItem(JsonNode document) {
- couchDbConnector.delete(document);
+ CouchDocumentAsyncTask deleteTask = new CouchDocumentAsyncTask(couchDbConnector, CouchDocumentAsyncTask.OPERATION_DELETE);
+ deleteTask.execute(document);
}
}
View
53 src/com/couchbase/grocerysync/CouchDocumentAsyncTask.java
@@ -0,0 +1,53 @@
+package com.couchbase.grocerysync;
+
+import org.ektorp.CouchDbConnector;
+
+import android.os.AsyncTask;
+
+public class CouchDocumentAsyncTask extends AsyncTask<Object, Void, Void> {
+
+ public static final int OPERATION_CREATE = 0;
+ public static final int OPERATION_UPDATE = 1;
+ public static final int OPERATION_DELETE = 2;
+
+ private CouchDbConnector couchDbConnector;
+ private int operation;
+
+ public CouchDocumentAsyncTask(CouchDbConnector couchDbConnector, int operation) {
+ this.couchDbConnector = couchDbConnector;
+ this.operation = operation;
+ }
+
+ @Override
+ protected Void doInBackground(Object... params) {
+
+ for (Object document : params) {
+ switch(operation) {
+ case OPERATION_CREATE:
+ doCreateInBackground(document);
+ break;
+ case OPERATION_UPDATE:
+ doUpdateInBackground(document);
+ break;
+ case OPERATION_DELETE:
+ doDeleteInBackground(document);
+ break;
+ }
+ }
+
+ return null;
+ }
+
+ private void doDeleteInBackground(Object document) {
+ couchDbConnector.delete(document);
+ }
+
+ private void doUpdateInBackground(Object document) {
+ couchDbConnector.update(document);
+ }
+
+ private void doCreateInBackground(Object document) {
+ couchDbConnector.create(document);
+ }
+
+}
View
60 src/com/couchbase/grocerysync/SplashScreenDialog.java
@@ -0,0 +1,60 @@
+package com.couchbase.grocerysync;
+
+import android.app.Dialog;
+import android.content.Context;
+import android.widget.ProgressBar;
+import android.widget.TextView;
+
+public class SplashScreenDialog extends Dialog {
+
+ protected ProgressBar splashProgressBar;
+ protected TextView splashProgressMessage;
+
+ public SplashScreenDialog(Context context) {
+ super(context, R.style.SplashScreenStyle);
+
+ setContentView(R.layout.splashscreen);
+ setCancelable(false);
+
+ splashProgressBar = (ProgressBar)findViewById(R.id.splashProgressBar);
+ splashProgressBar.setProgress(0);
+ splashProgressBar.setMax(100);
+
+ splashProgressMessage = (TextView)findViewById(R.id.splashProgressMessage);
+ splashProgressMessage.setText(context.getString(R.string.startup_message));
+ }
+
+ /**
+ * Logic to control progress and message together
+ */
+ public void updateSplashScreenProgress(int completed, int total) {
+ if(completed < (total - 1)) {
+ updateSplashScreenProgressBar(completed, total);
+ updateSplashScreenProgressMessage(getContext().getString(R.string.installing_message));
+ }
+ else {
+ updateSplashScreenProgressBar(completed, total);
+ updateSplashScreenProgressMessage(getContext().getString(R.string.startup_message));
+ }
+ }
+
+ /**
+ * Update the Splash Screen Progress Bar
+ */
+ private void updateSplashScreenProgressBar(int progress, int max) {
+ if(splashProgressBar != null) {
+ splashProgressBar.setProgress(progress);
+ splashProgressBar.setMax(max);
+ }
+ }
+
+ /**
+ * Update the Splash Screen Progress Message
+ */
+ private void updateSplashScreenProgressMessage(String message) {
+ if(splashProgressMessage != null) {
+ splashProgressMessage.setText(message);
+ }
+ }
+
+}

0 comments on commit 973d9a5

Please sign in to comment.