[#33] - Add validations and small refactoring for handleError method

This commit is contained in:
Franze Jr 2015-11-03 22:19:11 -03:00
parent 4df6a448dd
commit 25c9d69d02
3 changed files with 31 additions and 22 deletions

View file

@ -113,6 +113,7 @@
<!-- Network Errors -->
<string name="network_error_title">Connection Error</string>
<string name="network_error_no_network_body">You are not connected to the internet.</string>
<string name="internal_error_api">Some problem in our API.</string>
<string name="network_up">Your internet connection just got back!</string>
<string name="checklist.title.add">Add checklist</string>

View file

@ -181,29 +181,39 @@ public class APIHelper implements ErrorHandler, Profiler {
@Override
public Throwable handleError(RetrofitError cause) {
final Activity activity = (Activity) this.mContext;
//It also handles timeouts
if (cause.getKind().equals(RetrofitError.Kind.NETWORK)) {
final Activity activity = (Activity) this.mContext;
activity.runOnUiThread(new Runnable() {
public void run() {
new AlertDialog.Builder(activity)
.setTitle(R.string.network_error_title)
.setMessage(R.string.network_error_no_network_body)
.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
});
}
//It also handles timeouts
showConnectionProblemDialog(activity, R.string.network_error_no_network_body);
}else{
/*
* CONVERSION An exception was thrown while (de)serializing a body.
* HTTP A non-200 HTTP status code was received from the server e.g. 502, 503, etc...
* UNEXPECTED An internal error occurred while attempting to execute a request.
*/
showConnectionProblemDialog(activity,R.string.internal_error_api);
}
return cause;
}
private void showConnectionProblemDialog(final Activity activity, final int resourceMessageString){
activity.runOnUiThread(new Runnable() {
public void run() {
new AlertDialog.Builder(activity)
.setTitle(R.string.network_error_title)
.setMessage(resourceMessageString)
.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
});
}
@Override
public Object beforeCall() {
return null;

View file

@ -16,13 +16,11 @@ import com.habitrpg.android.habitica.R;
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(hasInternetConnection(context))
{
public void onReceive(final Context context, Intent intent) {
if (hasInternetConnection(context)) {
Toast.makeText(context, R.string.network_up, Toast.LENGTH_LONG).show();
}else{
} else {
Toast.makeText(context, R.string.network_error_no_network_body, Toast.LENGTH_LONG).show();
}
}