private void bind(Photos photos) { mSwipeRefreshLayout.setRefreshing(false); mPhotos = photos; mPhotosAdapter = new PhotosAdapter(mPhotos.getPhotos()); mRecyclerView.setAdapter(mPhotosAdapter); Observable.merge( mPhotosAdapter .getThumbnailClickedSubject() .filter(PhotosValidator::isSourceValid) .map(Photo::getSourceUrl), mPhotosAdapter .getItemClickedSubject() .filter(photo -> StringUtils.isValidUrl(photo.getPermalink())) .map(Photo::getPermalink)) .compose(bindUntilEvent(ActivityEvent.DESTROY)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(OnlyNextObserver.forAction(this::view)); }
private void initData() { // Initial load mPhotoService .getHotPhotos(null) .doOnNext(__ -> runOnUiThread(() -> mSwipeRefreshLayout.setRefreshing(true))) .compose(bindToLifecycle()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(this::bind, this::onErrorDownloading); // Observable for loading more pages mFetchAfterSubject .filter(after -> !StringUtils.isNullOrEmpty(after)) .distinctUntilChanged() .switchMap(mPhotoService::getHotPhotos) .doOnNext(__ -> runOnUiThread(() -> mSwipeRefreshLayout.setRefreshing(true))) .compose(bindUntilEvent(ActivityEvent.DESTROY)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(this::merge, this::onErrorDownloading); }