Search in sources :

Example 16 with Subscriber

use of rx.Subscriber in project Rutgers-Course-Tracker by tevjef.

the class SubjectPresenterImpl method loadSubjects.

@Override
public void loadSubjects(boolean pullToRefresh) {
    if (getView() != null)
        getView().showLoading(pullToRefresh);
    cancePreviousSubscription();
    Subscriber<List<Subject>> mSubscriber = new Subscriber<List<Subject>>() {

        @Override
        public void onCompleted() {
            if (getView() != null)
                getView().showLoading(false);
        }

        @Override
        public void onError(Throwable e) {
            //Lets the view decide what to display depending on what type of exception it is.
            if (getView() != null)
                getView().showError(e);
            //Removes the animated loading drawable
            if (getView() != null) {
                getView().showLoading(false);
            }
        }

        @Override
        public void onNext(List<Subject> subjectList) {
            if (getView() != null) {
                getView().setData(subjectList);
                if (subjectList.size() == 0)
                    getView().showError(new RutgersDataIOException());
                if (subjectList.size() > 0)
                    getView().showLayout(View.LayoutType.LIST);
            }
        }
    };
    mSubscription = mRetroRutgers.getSubjects(mRequest).doOnSubscribe(new Action0() {

        @Override
        public void call() {
            isLoading = true;
        }
    }).doOnTerminate(new Action0() {

        @Override
        public void call() {
            isLoading = false;
        }
    }).subscribeOn(mBackgroundThread).observeOn(mMainThread).subscribe(mSubscriber);
}
Also used : RutgersDataIOException(com.tevinjeffrey.rutgersct.rutgersapi.exceptions.RutgersDataIOException) Action0(rx.functions.Action0) Subscriber(rx.Subscriber) List(java.util.List) Subject(com.tevinjeffrey.rutgersct.rutgersapi.model.Subject)

Example 17 with Subscriber

use of rx.Subscriber in project Rutgers-Course-Tracker by tevjef.

the class CoursePresenterImpl method loadCourses.

@Override
public void loadCourses(boolean pullToRefresh) {
    if (getView() != null)
        getView().showLoading(pullToRefresh);
    cancePreviousSubscription();
    Subscriber<List<Course>> mSubscriber = new Subscriber<List<Course>>() {

        @Override
        public void onCompleted() {
            if (getView() != null)
                getView().showLoading(false);
        }

        @Override
        public void onError(Throwable e) {
            //Removes the animated loading drawable
            if (getView() != null)
                getView().showLoading(false);
            //Lets the view decide what to display depending on what type of exception it is.
            if (getView() != null)
                getView().showError(e);
        }

        @Override
        public void onNext(List<Course> courseList) {
            if (getView() != null)
                getView().setData(courseList);
            if (courseList.size() > 0) {
                if (getView() != null)
                    getView().showLayout(View.LayoutType.LIST);
            }
        }
    };
    mSubscription = mRetroRutgers.getCourses(mRequest).doOnSubscribe(new Action0() {

        @Override
        public void call() {
            isLoading = true;
        }
    }).doOnTerminate(new Action0() {

        @Override
        public void call() {
            isLoading = false;
        }
    }).subscribeOn(mBackgroundThread).observeOn(mMainThread).subscribe(mSubscriber);
}
Also used : Action0(rx.functions.Action0) Subscriber(rx.Subscriber) List(java.util.List) Course(com.tevinjeffrey.rutgersct.rutgersapi.model.Course)

Example 18 with Subscriber

use of rx.Subscriber in project ListenerMusicPlayer by hefuyicoder.

the class PanelSlideListener method setBlurredAlbumArt.

private void setBlurredAlbumArt() {
    Observable.create(new Observable.OnSubscribe<Drawable>() {

        @Override
        public void call(Subscriber<? super Drawable> subscriber) {
            Bitmap bitmap = ((BitmapDrawable) albumImage.getDrawable()).getBitmap();
            Drawable drawable = ImageUtil.createBlurredImageFromBitmap(bitmap, mContext, 3);
            subscriber.onNext(drawable);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Drawable>() {

        @Override
        public void call(Drawable drawable) {
            CoordinatorLayout.LayoutParams imageLayout = (CoordinatorLayout.LayoutParams) albumImage.getLayoutParams();
            imageLayout.height = FrameLayout.LayoutParams.MATCH_PARENT;
            imageLayout.width = FrameLayout.LayoutParams.MATCH_PARENT;
            albumImage.setLayoutParams(imageLayout);
            albumImage.setImageDrawable(drawable);
            if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                ColorDrawable colorDrawable = new ColorDrawable(nowPlayingCardColor);
                colorDrawable.setAlpha(200);
                albumImage.setForeground(colorDrawable);
            }
        }
    });
}
Also used : CoordinatorLayout(android.support.design.widget.CoordinatorLayout) Bitmap(android.graphics.Bitmap) ColorDrawable(android.graphics.drawable.ColorDrawable) Subscriber(rx.Subscriber) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable)

Example 19 with Subscriber

use of rx.Subscriber in project PhotoNoter by yydcdut.

the class RxCategory method saveCategory.

/**
     * 添加Category,这个category的check是true
     * 要做的事情:
     * 1、判断是否有这个字段了
     * 2、将其他的Category都取消check
     *
     * @param label
     * @param photosNumber
     * @param sort
     * @param isCheck
     * @return
     */
public Observable<List<Category>> saveCategory(String label, int photosNumber, int sort, boolean isCheck) {
    return Observable.create(new Observable.OnSubscribe<Long>() {

        private int mInTimes = 0;

        @Override
        public void call(Subscriber<? super Long> subscriber) {
            boolean exist = checkLabelExist(label);
            if (exist && mInTimes == 0) {
                //在没有mInTimes的时候,这里也会执行,不知道为啥.......
                subscriber.onError(new RxException("这个Label已经有了"));
            } else {
                mInTimes++;
                long id = mCategoryDB.save(label, photosNumber, sort, /* isCheck */
                true);
                if (mCache.size() != 0) {
                    subscriber.onNext(id);
                    subscriber.onCompleted();
                } else {
                    //如果mCache中没有数据,直接跳到lift中
                    subscriber.onCompleted();
                }
            }
        }
    }).subscribeOn(Schedulers.io()).map(//重新获取cache数据
    aLong -> mCache).flatMap(//转换成一个个的
    categories1 -> Observable.from(categories1)).filter(//过滤出check为true的
    category -> category.isCheck()).lift(new Observable.Operator<List<Category>, Category>() {

        @Override
        public Subscriber<? super Category> call(Subscriber<? super List<Category>> subscriber) {
            return new Subscriber<Category>() {

                @Override
                public void onCompleted() {
                    mCache.clear();
                    mCache.addAll(mCategoryDB.findAll());
                    subscriber.onNext(mCache);
                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onNext(Category category3) {
                    //如果有check为true的话,进入到这里,如果没有的话直接进入到onCompleted
                    category3.setCheck(false);
                    mCategoryDB.update(category3);
                }
            };
        }
    });
}
Also used : Inject(javax.inject.Inject) Context(android.content.Context) RxException(com.yydcdut.note.model.rx.exception.RxException) List(java.util.List) CategoryDB(com.yydcdut.note.model.dao.CategoryDB) Subscriber(rx.Subscriber) ContextLife(com.yydcdut.note.injector.ContextLife) Category(com.yydcdut.note.entity.Category) Schedulers(rx.schedulers.Schedulers) Singleton(javax.inject.Singleton) Observable(rx.Observable) Category(com.yydcdut.note.entity.Category) Observable(rx.Observable) Subscriber(rx.Subscriber) RxException(com.yydcdut.note.model.rx.exception.RxException) List(java.util.List)

Example 20 with Subscriber

use of rx.Subscriber in project PhotoNoter by yydcdut.

the class RxCategory method setCategoryMenuPosition.

/**
     * 设置check哪个category
     *
     * @param _id
     * @return
     */
public Observable<List<Category>> setCategoryMenuPosition(int _id) {
    return Observable.from(mCache).subscribeOn(Schedulers.io()).filter(//过滤出check为true的
    category -> category.isCheck()).map(category3 -> {
        category3.setCheck(false);
        mCategoryDB.update(category3);
        return mCache;
    }).flatMap(//转换成一个个Category来处理
    categories -> Observable.from(categories)).filter(//过滤出与ID相同的Category
    category1 -> category1.getId() == _id).lift(new Observable.Operator<Category, Category>() {

        @Override
        public Subscriber<? super Category> call(Subscriber<? super Category> subscriber) {
            return new Subscriber<Category>() {

                /* 因为我想经过filter之后,如果没有数据就返回onError,所以设置这个参数 */
                private int mInTimes = 0;

                @Override
                public void onCompleted() {
                    if (mInTimes == 0) {
                        subscriber.onError(new RxException("找不到这个ID的Category"));
                    }
                }

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

                @Override
                public void onNext(Category category) {
                    mInTimes++;
                    subscriber.onNext(category);
                }
            };
        }
    }).map(category2 -> {
        category2.setCheck(true);
        int row = mCategoryDB.update(category2);
        return row > 0 ? mCache : mCategoryDB.findAll();
    });
}
Also used : Inject(javax.inject.Inject) Context(android.content.Context) RxException(com.yydcdut.note.model.rx.exception.RxException) List(java.util.List) CategoryDB(com.yydcdut.note.model.dao.CategoryDB) Subscriber(rx.Subscriber) ContextLife(com.yydcdut.note.injector.ContextLife) Category(com.yydcdut.note.entity.Category) Schedulers(rx.schedulers.Schedulers) Singleton(javax.inject.Singleton) Observable(rx.Observable) Category(com.yydcdut.note.entity.Category) Subscriber(rx.Subscriber) RxException(com.yydcdut.note.model.rx.exception.RxException)

Aggregations

Subscriber (rx.Subscriber)48 List (java.util.List)16 Observable (rx.Observable)15 IOException (java.io.IOException)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Action1 (rx.functions.Action1)10 Schedulers (rx.schedulers.Schedulers)10 Context (android.content.Context)8 ArrayList (java.util.ArrayList)8 OnSubscribe (rx.Observable.OnSubscribe)8 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)7 RxException (com.yydcdut.note.model.rx.exception.RxException)7 Notification (rx.Notification)7 TestSubscriber (rx.observers.TestSubscriber)7 Cursor (android.database.Cursor)5 ContextLife (com.yydcdut.note.injector.ContextLife)5 Map (java.util.Map)5 Inject (javax.inject.Inject)5 Subscription (rx.Subscription)5 Action0 (rx.functions.Action0)5