How to achieve showing a progress bar on each Retrofit 2.0
API call without having to make a progress bar in each activity, showing it and dismissing. Progress bar should show whenever an API is hit and it should dismiss when we get a response on onResponse
or onFailure
is called.
I tried this :
ProgressDialog mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setMessage("Loading...");
mProgressDialog.show();
retrofitService.login(new SignInRequest(email, password),
new Callback<SignInResponse>() {
@Override
public void onResponse(Call<SignInResponse> call, Response<SignInResponse> response) {
if (mProgressDialog.isShowing())
mProgressDialog.dismiss();
}
@Override
public void onFailure(Call<SignInResponse> call, Throwable t) {
if (mProgressDialog.isShowing())
mProgressDialog.dismiss();
}
});
But this code will have to copy pasted everywhere whenever I make an API call. I don;t want duplicate code.