Skip to content

Instantly share code, notes, and snippets.

@lawloretienne
Last active February 22, 2017 06:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lawloretienne/0e96def6cf2e0dba463e to your computer and use it in GitHub Desktop.
Save lawloretienne/0e96def6cf2e0dba463e to your computer and use it in GitHub Desktop.

Make two API calls and update in one event

Observable.combineLatest(
    Api.getService(Api.getEndpointUrl()).getUserProfile(SoundcloudConstants.USERNAME),
    Api.getService(Api.getEndpointUrl()).getFavoriteTracks(SoundcloudConstants.USERNAME),
    new Func2<UserProfile, List<Track>, Account>() {
        @Override
        public Account call(UserProfile userProfile, List<Track> tracks) {
            return new Account(userProfile, tracks);
        }
    })
    .subscribeOn(Schedulers.newThread())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Action1<Account>() {
        @Override
        public void call(Account account) {
            if (account != null) {
                persistAccount(account);
            }
        }
    }, new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {
            Timber.e(throwable, "Soundcloud error");

            if (throwable instanceof RetrofitError) {
                RetrofitError.Kind errorKind = ((RetrofitError) throwable).getKind();

                mErrorTextView.setText(getErrorMessage(errorKind));
                Timber.e(throwable, "Soundcloud error : errorMessage - " + getErrorMessage(errorKind));

                mProgressBar.setVisibility(View.GONE);
                if (mAccountLinearLayout.getVisibility() == View.GONE)
                    mErrorLinearLayout.setVisibility(View.VISIBLE);
            }
        }
    });

Make consecutive API calls and maintain the order of the responses

Api.getService(Api.getEndpointUrl()).getTopStoryIds()
    .concatMap(new Func1<List<Long>, Observable<?>>() {
        @Override
        public Observable<?> call(List<Long> storyIds) {
            mStoryIdCount = storyIds.size();
    
            return Observable.from(storyIds);
        }
    })
    .concatMap(new Func1<Object, Observable<TopStory>>() {
        @Override
        public Observable<TopStory> call(Object o) {
            Long storyId = (Long)o;
            return Api.getService(Api.getEndpointUrl()).getTopStory(storyId);
        }
    })
    .subscribeOn(Schedulers.newThread())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Action1<TopStory>() {
        @Override
        public void call(TopStory topStory) {
            if(!mIsRefreshing && topStory != null){
                Timber.d("getTopStory : success()");
                mProgressBar.setVisibility(View.GONE);
                mTopStoriesRecyclerViewAdapter.add(mTopStoriesRecyclerViewAdapter.getItemCount(), topStory);
    
                if(mTopStoriesRecyclerViewAdapter.getItemCount() == mStoryIdCount){
                    mSwipeRefreshLayout.setRefreshing(false);
                    mIsRefreshing = false;
                }
            }
        }
    }, new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {
            Timber.d("getTopStory : failure()");
        }
    });

Debounce an edittext for form validation

RxTextView.textChanges(mEmailEditText)
  .map(new Func1<CharSequence, CharSequence>() {
      @Override
      public CharSequence call(CharSequence charSequence) {
          disableError(mEmailInputLayout);
          // mEmailInputLayout.setErrorEnabled(false);
          mEmailInputLayout.setError(null);
          return charSequence;
      }
  })
  .debounce(400, TimeUnit.MILLISECONDS)
  .observeOn(AndroidSchedulers.mainThread()) // UI Thread
  .subscribe(new Subscriber<CharSequence>() {
      @Override
      public void onCompleted() {

      }

      @Override
      public void onError(Throwable e) {
        e.printStackTrace();
      }

      @Override
      public void onNext(CharSequence charSequence) {
          if(!TextUtils.isEmpty(charSequence)){
              boolean isEmailValid = validateEmail(charSequence.toString());
              if(!isEmailValid){
                  enableError(mEmailInputLayout);
                  // mEmailInputLayout.setErrorEnabled(true);
                  mEmailInputLayout.setError("Not a valid email address!");
              } else {
                  disableError(mEmailInputLayout);
                  // mEmailInputLayout.setErrorEnabled(false);
                  mEmailInputLayout.setError(null);
              }
          }
      }
  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment