Search in sources :

Example 1 with RxException

use of com.yydcdut.note.model.rx.exception.RxException 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 2 with RxException

use of com.yydcdut.note.model.rx.exception.RxException 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)

Example 3 with RxException

use of com.yydcdut.note.model.rx.exception.RxException in project PhotoNoter by yydcdut.

the class RxUser method getQQ.

public Observable<IUser> getQQ() {
    return Observable.create(new Observable.OnSubscribe<IUser>() {

        @Override
        public void call(Subscriber<? super IUser> subscriber) {
            if (mQQUser == null) {
                String name = mSharedPreferences.getString(Q_NAME, NAME_DEFAULT);
                String netImagePath = mSharedPreferences.getString(Q_NET_IMAGE_PATH, Q_NET_IMAGE_PATH_DEFAULT);
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(netImagePath)) {
                    subscriber.onError(new RxException("没有登录!!!"));
                    return;
                } else {
                    mQQUser = new QQUser(name, netImagePath);
                }
            }
            subscriber.onNext(mQQUser);
            subscriber.onCompleted();
        }
    }).subscribeOn(Schedulers.io());
}
Also used : Subscriber(rx.Subscriber) IUser(com.yydcdut.note.entity.user.IUser) RxException(com.yydcdut.note.model.rx.exception.RxException) QQUser(com.yydcdut.note.entity.user.QQUser)

Example 4 with RxException

use of com.yydcdut.note.model.rx.exception.RxException in project PhotoNoter by yydcdut.

the class RxUser method getEvernote.

public Observable<IUser> getEvernote() {
    return Observable.create(new Observable.OnSubscribe<IUser>() {

        @Override
        public void call(Subscriber<? super IUser> subscriber) {
            if (mEvernoteUser == null) {
                String name = mSharedPreferences.getString(EVERNOTE_NAME, NAME_DEFAULT);
                if (TextUtils.isEmpty(name)) {
                    subscriber.onError(new RxException("没有登录!!!"));
                } else {
                    mEvernoteUser = new EvernoteUser(name);
                }
            }
            subscriber.onNext(mEvernoteUser);
            subscriber.onCompleted();
        }
    }).subscribeOn(Schedulers.io());
}
Also used : Subscriber(rx.Subscriber) EvernoteUser(com.yydcdut.note.entity.user.EvernoteUser) IUser(com.yydcdut.note.entity.user.IUser) RxException(com.yydcdut.note.model.rx.exception.RxException)

Example 5 with RxException

use of com.yydcdut.note.model.rx.exception.RxException in project PhotoNoter by yydcdut.

the class SplashPresenterImpl method checkDisks.

private void checkDisks() {
    if (!mLocalStorageUtils.isFirstTime()) {
        initFiles();
        Observable.create(new Observable.OnSubscribe<File[]>() {

            @Override
            public void call(Subscriber<? super File[]> subscriber) {
                File f = new File(FilePathUtils.getPath());
                if (f.exists()) {
                    subscriber.onNext(f.listFiles());
                    subscriber.onCompleted();
                } else {
                    subscriber.onError(new RxException(""));
                }
            }
        }).flatMap(fileList -> Observable.from(fileList)).subscribeOn(Schedulers.io()).observeOn(Schedulers.computation()).filter(file1 -> !file1.isDirectory()).filter(file -> file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".png") || file.getName().toLowerCase().endsWith(".jpeg")).count().subscribe((fileNumber -> {
            mRxSandBox.getNumber().subscribe((integer -> {
                if (fileNumber != integer) {
                    Intent checkIntent = new Intent(mContext, CheckService.class);
                    mContext.startService(checkIntent);
                }
            }), (throwable -> YLog.e(throwable)));
        }), (throwable -> YLog.e(throwable)));
    }
}
Also used : Context(android.content.Context) Subscriber(rx.Subscriber) LocalStorageUtils(com.yydcdut.note.utils.LocalStorageUtils) Intent(android.content.Intent) ISplashPresenter(com.yydcdut.note.presenters.home.ISplashPresenter) File(java.io.File) Observable(rx.Observable) Inject(javax.inject.Inject) RxException(com.yydcdut.note.model.rx.exception.RxException) Message(android.os.Message) Permission(com.yydcdut.note.utils.permission.Permission) RxSandBox(com.yydcdut.note.model.rx.RxSandBox) Handler(android.os.Handler) ContextLife(com.yydcdut.note.injector.ContextLife) ISplashView(com.yydcdut.note.views.home.ISplashView) FilePathUtils(com.yydcdut.note.utils.FilePathUtils) Schedulers(rx.schedulers.Schedulers) CheckService(com.yydcdut.note.service.CheckService) AspectPermission(com.yydcdut.note.aspect.permission.AspectPermission) Activity(android.app.Activity) PermissionUtils(com.yydcdut.note.utils.PermissionUtils) YLog(com.yydcdut.note.utils.YLog) IView(com.yydcdut.note.views.IView) Subscriber(rx.Subscriber) RxException(com.yydcdut.note.model.rx.exception.RxException) Intent(android.content.Intent) File(java.io.File)

Aggregations

RxException (com.yydcdut.note.model.rx.exception.RxException)6 Subscriber (rx.Subscriber)6 Context (android.content.Context)3 IUser (com.yydcdut.note.entity.user.IUser)3 ContextLife (com.yydcdut.note.injector.ContextLife)3 Inject (javax.inject.Inject)3 Observable (rx.Observable)3 Schedulers (rx.schedulers.Schedulers)3 Category (com.yydcdut.note.entity.Category)2 EvernoteUser (com.yydcdut.note.entity.user.EvernoteUser)2 QQUser (com.yydcdut.note.entity.user.QQUser)2 CategoryDB (com.yydcdut.note.model.dao.CategoryDB)2 List (java.util.List)2 Singleton (javax.inject.Singleton)2 Activity (android.app.Activity)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Handler (android.os.Handler)1 Message (android.os.Message)1 EDAMSystemException (com.evernote.edam.error.EDAMSystemException)1