How can I use a modified library with progressbar?

I am using a modified library. How can I use progressbar before calling the callback method?

public void getMovies() { MovieClient.getWeather().movies(MovieClient.getRandomMovie(), new Callback<MovieRequest>() { @Override public void success(MovieRequest movieRequest, Response response) { Picasso.with(getApplicationContext()).load(movieRequest.getPoster()).into(image); } @Override public void failure(RetrofitError error) { Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); } }); } 
+6
source share
2 answers

You can use it as follows:

 public void getMovies() { ProgressDialog mProgressDialog = new ProgressDialog(this); mProgressDialog.setIndeterminate(true); mProgressDialog.setMessage("Loading..."); mProgressDialog.show(); MovieClient.getWeather().movies(MovieClient.getRandomMovie(), new Callback<MovieRequest>() { @Override public void success(MovieRequest movieRequest, Response response) { Picasso.with(getApplicationContext()).load(movieRequest.getPoster()).into(image); if (mProgressDialog.isShowing()) mProgressDialog.dismiss(); } @Override public void failure(RetrofitError error) { if (mProgressDialog.isShowing()) mProgressDialog.dismiss(); Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); } }); } 
+20
source

You can check this method. Idea: create a wrapper, observable, then add 2 actions "start" and "finish".

 public static <T> Observable<T> bindLoadingAction(final Runnable start, final Runnable finish, final Observable<T> source) { final Observable<T> o = source.observeOn(AndroidSchedulers.mainThread()); return o.lift(new Observable.Operator<T, T>() { @Override public Subscriber<? super T> call(final Subscriber<? super T> child) { return new Subscriber<T>() { @Override public void onStart() { super.onStart(); if (start != null) { new Handler(Looper.getMainLooper()).post(start::run); } child.onStart(); } @Override public void onCompleted() { if (finish != null) { finish.run(); } child.onCompleted(); } @Override public void onError(Throwable e) { if (finish != null) { finish.run(); } child.onError(e); } @Override public void onNext(T t) { if (finish != null) { finish.run(); } child.onNext(t); } }; } }); } 

Using:

 bindLoadingAction(this::showLoadingView, this::hideLoadingView, YourRetrofitObservable); 

(I used the Java 8 syntax. This :: showLoadingView means a reference to the showLoadingView method in the current class.)

+2
source

Source: https://habr.com/ru/post/981363/


All Articles