How to automatically show a progress bar on every API call for refitting?

How to achieve a progress bar for each call to the Retrofit 2.0 API without having to create a progress bar in each action, show it and reject it. The onResponse onFailure should show every time the API is deleted, and it should reject when we get a response to onResponse or onFailure .

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 be copied everywhere when I call the API call. I do not want to duplicate the code.

+5
source share
5 answers

As suggested by @Sourabh, I ended up working with the database and named a simple method during every API call. In BaseActivity ,

 public void showDialog() { if(mProgressDialog != null && !mProgressDialog.isShowing()) mProgressDialog.show(); } public void hideDialog() { if(mProgressDialog != null && mProgressDialog.isShowing()) mProgressDialog.dismiss(); } 

In your child activity, you can directly call showDialog() and hideDialog() to display and reject the dialog box.

+2
source

Another inner class in your Activity can save you.

 class MyCallBack implements CallBacks<T>{ @Override public void onResponse(Call<T> call, Response<T> response) { if (mProgressDialog.isShowing()) mProgressDialog.dismiss(); } @Override public void onFailure(Call<SignInResponse> call, Throwable t) { if (mProgressDialog.isShowing()) mProgressDialog.dismiss(); } } 

When you submit a request:

 retrofitService.login(new SignInRequest(email, password), new MyCallback<SignInResponse>() { @Override public void onResponse(Call<SignInResponse> call, Response<SignInResponse> response) { super(call, response); //do more on response } @Override public void onFailure(Call<SignInResponse> call, Throwable t) { super(call, error); /* Do more on failure. For example: give a reason why the request failed*/ } }); 
+7
source

Based on the answers of Shubham and Peters, I wrote this class:

 class CustomCallBack<T> implements Callback<T> { private ProgressDialog mProgressDialog; Context context; CustomCallBack(Context context) { this.context = context; mProgressDialog = new ProgressDialog(context); ((Activity) context).getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); mProgressDialog.setIndeterminate(true); mProgressDialog.setMessage("Loading..."); mProgressDialog.setCanceledOnTouchOutside(false); mProgressDialog.show(); } @Override public void onResponse(Call<T> call, Response<T> response) { if (mProgressDialog.isShowing()) { mProgressDialog.dismiss(); ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); } } @Override public void onFailure(Call<T> call, Throwable t) { if (mProgressDialog.isShowing()) { mProgressDialog.dismiss(); ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); } } } 

Hope this helps you.

+3
source

Shameless advertisement

I created the RxLoading library for this, it can do it and much more,

you can just do something like this:

 networkCall().compose(RxLoading.<>create(loadingLayout)).subscribe(...); 

it consists of 2 classes, a custom view (loadLayout) and RxLoading, which is a GitHub page .

+1
source

Example from the tutorial:

  protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); requestWindowFeature(Window.FEATURE_PROGRESS); ArrayAdapter<Question> arrayAdapter = new ArrayAdapter<Question>(this, android.R.layout.simple_list_item_1, android.R.id.text1, new ArrayList<Question>()); setListAdapter(arrayAdapter); setProgressBarIndeterminateVisibility(true); setProgressBarVisibility(true); } 

Study Guide Link: http://www.vogella.com/tutorials/Retrofit/article.html

0
source

All Articles