Skip to content

Commit

Permalink
Improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
EddieCurtis committed Mar 12, 2015
1 parent 793893c commit da488e4
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
import java.util.TreeSet;

import me.tripsit.mobile.R;
import me.tripsit.mobile.TripMobileActivity;
import me.tripsit.mobile.builders.LayoutBuilder;
import me.tripsit.mobile.common.ErrorHandlingActivity;
import me.tripsit.mobile.error.ErrorHandler;

public class Combinations extends ErrorHandlingActivity implements CombinationsCallback, ErrorHandler {
public class Combinations extends TripMobileActivity implements CombinationsCallback {

private enum CombinationSeverity {
SAFE_SYNERGY("Safe & Synergy", "%s and %s %s a safe combination, which provides synergistic effects. Be sure to lower the doses of each, as they will potentiate each other accordingly.", R.id.txt_safesynergy_header, R.id.txt_safesynergy_content),
Expand Down Expand Up @@ -72,6 +71,11 @@ private String getSingleCombinationText() {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(LayoutBuilder.buildLinearLayout(this, R.layout.activity_combinations, LayoutBuilder.buildParams()));
downloadCombinations();
}

@Override
public void downloadCombinations() {
new CombinationsAsyncTask(this, this).execute(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,67 @@
import java.util.Map;

import me.tripsit.mobile.comms.ContentRetriever;
import me.tripsit.mobile.error.ErrorHandler;

public class CombinationsAsyncTask extends AsyncTask<Activity, Void, Void> {

private static final String URL = "http://tripsit.me/combo.json";

private final CombinationsCallback callback;
private final ErrorHandler errorHandler;
private final Activity activity;
private final Map<String, Map<String, List<String>>> combinationsMap;

CombinationsAsyncTask(CombinationsCallback callback, ErrorHandler errorHandler) {
CombinationsAsyncTask(CombinationsCallback callback, Activity activity) {
this.callback = callback;
this.errorHandler = errorHandler;
this.activity = activity;
combinationsMap = new HashMap<String, Map<String, List<String>>>();
}

@Override
protected Void doInBackground(final Activity... activity) {
protected Void doInBackground(final Activity... context) {
try {
String response = new ContentRetriever(activity[0]).getResponseFromURL(URL);
String response = new ContentRetriever(context[0]).getResponseFromURL(URL);
JSONObject combinations = new JSONObject(response);
JSONArray drugNames = combinations.names();
for (int i = 0; i < drugNames.length(); i++) {
String drugName = drugNames.getString(i);
Map<String, List<String>> interactionsMap = buildInteractionsMap(combinations, drugName);
combinationsMap.put(drugName, interactionsMap);
}
} catch (JSONException e) {
errorHandler.handleGenericError(String.format("Failed to parse combinations text, please report this error: " + e.toString()));
} catch (IOException e) {
new AlertDialog.Builder(activity[0])
.setTitle("Operation failed")
.setMessage("Failed to combinations information. Please check your internet connection and try again.")
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
doInBackground(activity);
}
})
.setNegativeButton("Return to menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.finishActivity();
}
})
.show();
} catch (final JSONException e) {
context[0].runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(activity)
.setTitle("Operation failed")
.setMessage("Failed to parse combinations text, please report this error: " + e.getMessage())
.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.finishActivity();
}
})
.show();
}
});
} catch (final IOException e) {
context[0].runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(activity)
.setTitle("Operation failed")
.setMessage("Failed to download combinations information. Please check your internet connection and try again.")
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.downloadCombinations();
}
})
.setNegativeButton("Return to menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.finishActivity();
}
})
.show();
}
});
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public interface CombinationsCallback {

public void downloadCombinations();
public void updateCombinationsMap(Map<String, Map<String, List<String>>> combinations);
public void finishActivity();

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,65 @@
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import me.tripsit.mobile.comms.ContentRetriever;
import me.tripsit.mobile.error.ErrorHandler;

public class DrugInfoAsyncTask extends AsyncTask<Activity, Void, Void> {

private static final String DRUG_URL = "http://tripbot.tripsit.me/api/tripsit/getDrug?name=";

private final FactsheetsCallback callback;
private final ErrorHandler errorHandler;
private final Activity activity;
private final String drugName;

private Drug result = null;

DrugInfoAsyncTask(FactsheetsCallback callback, ErrorHandler errorHandler, String drugName) {
DrugInfoAsyncTask(FactsheetsCallback callback, Activity activity, String drugName) {
this.callback = callback;
this.errorHandler = errorHandler;
this.activity = activity;
this.drugName = drugName;
}

@Override
protected Void doInBackground(final Activity... activity) {
protected Void doInBackground(final Activity... activities) {
try {
String response = new ContentRetriever(activity[0]).getResponseFromURL(DRUG_URL + drugName);
String response = new ContentRetriever(activities[0]).getResponseFromURL(DRUG_URL + drugName);
result = new Drug(new JSONObject(response));
} catch (JSONException e) {
errorHandler.handleGenericError(String.format("Failed to parse text for '%s', please report this error: %s", drugName, e.toString()));
throw new IOException("test");
} catch (final JSONException e) {
activities[0].runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity.getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
} catch (IOException e) {
new AlertDialog.Builder(activity[0])
.setTitle("Operation failed")
.setMessage("Failed to download drug information. Please check your internet connection and try again.")
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
doInBackground(activity);
}
})
.setNegativeButton("Return to menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.finishActivity();
}
})
.show();
activities[0].runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(activity)
.setTitle("Operation failed")
.setMessage("Failed to download drug information. Please check your internet connection and try again.")
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.searchDrug(drugName);
}
})
.setNegativeButton("Return to menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.finishActivity();
}
})
.show();
}
});

}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -14,7 +15,6 @@
import java.util.TreeSet;

import me.tripsit.mobile.comms.ContentRetriever;
import me.tripsit.mobile.error.ErrorHandler;

public class DrugNamesAsyncTask extends AsyncTask<Activity, Void, Void> {

Expand All @@ -23,13 +23,13 @@ public class DrugNamesAsyncTask extends AsyncTask<Activity, Void, Void> {
private static final String SPLIT = "\",\""; // Split by ","

private final FactsheetsCallback callback;
private final ErrorHandler errorHandler;
private final Activity activity;

private Set<String> drugNames = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);

DrugNamesAsyncTask(FactsheetsCallback callback, ErrorHandler errorHandler) {
DrugNamesAsyncTask(FactsheetsCallback callback, Activity activity) {
this.callback = callback;
this.errorHandler = errorHandler;
this.activity = activity;
}

@Override
Expand All @@ -47,22 +47,32 @@ protected Void doInBackground(final Activity... context) {
}
}
} catch (JSONException e) {
errorHandler.handleGenericError("Could not retrieve drug names from tripbot interface. Search may still work but autocomplete suggestions will not be present.");
context[0].runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity.getBaseContext(), "Could not retrieve drug names from tripbot interface. Search may still work but autocomplete suggestions will not be present.", Toast.LENGTH_LONG).show();
}
});
} catch (IOException e) {
new AlertDialog.Builder(context[0])
.setTitle("Operation failed")
.setMessage("Failed to download drug information. Please check your internet connection and try again.")
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
doInBackground(context);
}
})
.setNegativeButton("Return to menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.finishActivity();
}
})
.show();
context[0].runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(activity)
.setTitle("Operation failed")
.setMessage("Failed to download drug information. Please check your internet connection and try again.")
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.searchDrugNames();
}
})
.setNegativeButton("Return to menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.finishActivity();
}
})
.show();
}
});
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,21 @@
import java.util.Set;

import me.tripsit.mobile.R;
import me.tripsit.mobile.TripMobileActivity;
import me.tripsit.mobile.builders.LayoutBuilder;
import me.tripsit.mobile.common.ErrorHandlingActivity;
import me.tripsit.mobile.error.ErrorHandler;
import me.tripsit.mobile.utils.CollectionUtils;

/**
* The factsheets activity is used to retrieve data about particular drugs from the tripbot API
* @author Eddie Curtis
*/
public class Factsheets extends ErrorHandlingActivity implements FactsheetsCallback, ErrorHandler {
public class Factsheets extends TripMobileActivity implements FactsheetsCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(LayoutBuilder.buildLinearLayout(this, R.layout.activity_factsheets, LayoutBuilder.buildParams()));
downloadFactsheetInfo();
searchDrugNames();
setDrugNameSearchListeners((AutoCompleteTextView) findViewById(R.id.drugNameSearch));
}

Expand All @@ -52,28 +51,25 @@ public void onClick(View v) {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
populateFormWithDrug((String)parent.getItemAtPosition(position));
searchDrug((String)parent.getItemAtPosition(position));
}
});
}

public void clickSearch(View v) {
AutoCompleteTextView searchBox = (AutoCompleteTextView) findViewById(R.id.drugNameSearch);
Editable drugName = searchBox.getText();
populateFormWithDrug(drugName.toString());
searchDrug(drugName.toString());
}

private void populateFormWithDrug(String drugName) {
@Override
public void searchDrug(String drugName) {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.drugNameSearch);
textView.clearFocus();
new DrugInfoAsyncTask(this, this, drugName).execute(this);
}

private void downloadFactsheetInfo() {
new DrugNamesAsyncTask(this, this).execute(this);
}
}

@Override
public void finishActivity() {
Expand All @@ -97,6 +93,11 @@ public void onDrugSearchComplete(Drug drug) {
}
}

@Override
public void searchDrugNames() {
new DrugNamesAsyncTask(this, this).execute(this);
}

private void updateDrugView(Drug drug) {
TextView drugName = (TextView) findViewById(R.id.txt_drugName);
String updatedDrugName = manipulateDrugName(drug.getName(), drug.getAliases());
Expand Down
Loading

0 comments on commit da488e4

Please sign in to comment.