Skip to content

Commit

Permalink
[CHORE] Handle Network Connectivity Edge Case.
Browse files Browse the repository at this point in the history
[+] Added an AlertDialog that fires up when there isn't a network connection.
[+] It provides you with an option to retry or exit the app.
  • Loading branch information
TheDancerCodes committed Aug 28, 2017
1 parent 38bb889 commit 28d9bb8
Showing 1 changed file with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import android.app.ProgressDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
Expand Down Expand Up @@ -58,25 +62,6 @@ public void onRefresh() {
Toast.makeText(MainActivity.this, "Users Refreshed", Toast.LENGTH_SHORT).show();
}
});

// // Listview to the display the Github users
// listView = (ListView) findViewById(R.id.paginator_list);
//
// // Build Retrofit Objects
// final Retrofit.Builder builder = new Retrofit.Builder()
// .baseUrl("https://api.github.com/search/")
// .addConverterFactory(GsonConverterFactory.create());
//
// Retrofit retrofit = builder.build();
//
// // Simple REST adapter which points the GitHub API endpoint.
// GithubClient client = retrofit.create(GithubClient.class);
//
// // Fetch a list of the Github User.
// Call<List<GithubUsers>> call = client.listOfJavaDevs("language:java location:lagos");
//
// // Execute the call asynchronously. Get a positive or negative callback.
// call.enqueue();
}

public Activity getActivity() {
Expand Down Expand Up @@ -114,6 +99,7 @@ private void initViews() {

//Loading User Data
loadJSON();

}

// Handling Loading of JSON Data
Expand Down Expand Up @@ -148,7 +134,35 @@ public void onResponse(Call<GithubUsersResponse> call, Response<GithubUsersRespo
@Override
public void onFailure(Call<GithubUsersResponse> call, Throwable t) {
Log.d("Error", t.getMessage());
Toast.makeText(MainActivity.this, "Error Fetching User Data :(", Toast.LENGTH_SHORT).show();

// Code to check for network connectivity
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(false);
builder.setTitle("No Internet");
builder.setMessage("Internet is required. Please Retry.");

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});

builder.setPositiveButton("Retry", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
loadJSON();
}
});
AlertDialog dialog = builder.create(); // calling builder.create after adding buttons
dialog.show();
Snackbar.make(findViewById(android.R.id.content), "Network Unavailable!", Snackbar.LENGTH_LONG)
.setActionTextColor(Color.RED)
.show();

}
});
}catch(Exception e){
Expand Down

0 comments on commit 28d9bb8

Please sign in to comment.