Search in sources :

Example 1 with Category

use of com.yydcdut.note.entity.Category 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 2 with Category

use of com.yydcdut.note.entity.Category 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(subscriber -> 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) Observable(rx.Observable) Category(com.yydcdut.note.entity.Category) Subscriber(rx.Subscriber) RxException(com.yydcdut.note.model.rx.exception.RxException)

Example 3 with Category

use of com.yydcdut.note.entity.Category in project PhotoNoter by yydcdut.

the class AlbumPresenterImpl method createCategory.

@Override
public void createCategory(String newCategoryLabel) {
    mRxCategory.getAllCategories().observeOn(AndroidSchedulers.mainThread()).subscribe(categories -> {
        int totalNumber = categories.size();
        if (!TextUtils.isEmpty(newCategoryLabel)) {
            mRxCategory.saveCategory(newCategoryLabel, 0, totalNumber, true).observeOn(AndroidSchedulers.mainThread()).subscribe(categories1 -> {
                boolean success = false;
                for (Category category : categories1) {
                    if (category.getLabel().equals(newCategoryLabel)) {
                        mAlbumView.changeActivityListMenuCategoryChecked(category);
                        EventBus.getDefault().post(new CategoryCreateEvent());
                        success = true;
                        break;
                    }
                }
                if (!success) {
                    mAlbumView.showToast(mContext.getResources().getString(R.string.toast_fail));
                }
            }, (throwable -> YLog.e(throwable)));
        } else {
            mAlbumView.showToast(mContext.getResources().getString(R.string.toast_fail));
        }
    }, (throwable -> YLog.e(throwable)));
}
Also used : Context(android.content.Context) RxPhotoNote(com.yydcdut.note.model.rx.RxPhotoNote) Uri(android.net.Uri) PhotoNoteDeleteEvent(com.yydcdut.note.bus.PhotoNoteDeleteEvent) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) ArrayList(java.util.ArrayList) Observable(rx.Observable) Inject(javax.inject.Inject) IAlbumView(com.yydcdut.note.views.home.IAlbumView) ContentResolver(android.content.ContentResolver) RxSandBox(com.yydcdut.note.model.rx.RxSandBox) Handler(android.os.Handler) ContextLife(com.yydcdut.note.injector.ContextLife) EventBus(org.greenrobot.eventbus.EventBus) Map(java.util.Map) Category(com.yydcdut.note.entity.Category) FilePathUtils(com.yydcdut.note.utils.FilePathUtils) R(com.yydcdut.note.R) Utils(com.yydcdut.note.utils.Utils) PermissionUtils(com.yydcdut.note.utils.PermissionUtils) RxCategory(com.yydcdut.note.model.rx.RxCategory) CategoryMoveEvent(com.yydcdut.note.bus.CategoryMoveEvent) Subscriber(rx.Subscriber) PhotoNoteCreateEvent(com.yydcdut.note.bus.PhotoNoteCreateEvent) LocalStorageUtils(com.yydcdut.note.utils.LocalStorageUtils) TextUtils(android.text.TextUtils) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IAlbumPresenter(com.yydcdut.note.presenters.home.IAlbumPresenter) List(java.util.List) Permission(com.yydcdut.note.utils.permission.Permission) TreeMap(java.util.TreeMap) ComparatorFactory(com.yydcdut.note.model.compare.ComparatorFactory) ImageLoaderManager(com.yydcdut.note.utils.ImageManager.ImageLoaderManager) CategoryCreateEvent(com.yydcdut.note.bus.CategoryCreateEvent) PhotoNote(com.yydcdut.note.entity.PhotoNote) AspectPermission(com.yydcdut.note.aspect.permission.AspectPermission) YLog(com.yydcdut.note.utils.YLog) IView(com.yydcdut.note.views.IView) Category(com.yydcdut.note.entity.Category) RxCategory(com.yydcdut.note.model.rx.RxCategory) CategoryCreateEvent(com.yydcdut.note.bus.CategoryCreateEvent)

Example 4 with Category

use of com.yydcdut.note.entity.Category in project PhotoNoter by yydcdut.

the class CategoryDB method update.

/**
 * 更新Category
 *
 * @param categories 需要更新的分类
 * @return
 */
public synchronized int update(Category... categories) {
    SQLiteDatabase db = mNotesSQLite.getWritableDatabase();
    db.beginTransaction();
    int rows = 0;
    try {
        for (Category category : categories) {
            ContentValues contentValues = new ContentValues();
            contentValues.put("label", category.getLabel());
            contentValues.put("photosNumber", category.getPhotosNumber());
            contentValues.put("isCheck", category.isCheck() ? 1 : 0);
            contentValues.put("sort", category.getSort());
            rows += db.update(NotesSQLite.TABLE_CATEGORY, contentValues, "_id = ?", new String[] { category.getId() + "" });
        }
        db.setTransactionSuccessful();
    } catch (Exception e) {
        YLog.e(e);
        return -1;
    } finally {
        db.endTransaction();
        db.close();
    }
    return rows;
}
Also used : ContentValues(android.content.ContentValues) Category(com.yydcdut.note.entity.Category) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 5 with Category

use of com.yydcdut.note.entity.Category in project PhotoNoter by yydcdut.

the class CategoryDB method findAll.

/**
 * 获取数据
 *
 * @return Categories
 */
public synchronized List<Category> findAll() {
    List<Category> list = new ArrayList<>();
    SQLiteDatabase db = mNotesSQLite.getReadableDatabase();
    Cursor cursor = db.query(NotesSQLite.TABLE_CATEGORY, null, null, null, null, null, "sort asc");
    while (cursor.moveToNext()) {
        int id = cursor.getInt(cursor.getColumnIndex("_id"));
        String label = cursor.getString(cursor.getColumnIndex("label"));
        int photosNumber = cursor.getInt(cursor.getColumnIndex("photosNumber"));
        boolean isCheck = cursor.getInt(cursor.getColumnIndex("isCheck")) == 0 ? false : true;
        int sort = cursor.getInt(cursor.getColumnIndex("sort"));
        Category category = new Category(id, label, photosNumber, sort, isCheck);
        list.add(category);
    }
    cursor.close();
    db.close();
    return list;
}
Also used : Category(com.yydcdut.note.entity.Category) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Aggregations

Category (com.yydcdut.note.entity.Category)14 Context (android.content.Context)8 Inject (javax.inject.Inject)8 ContextLife (com.yydcdut.note.injector.ContextLife)7 RxCategory (com.yydcdut.note.model.rx.RxCategory)7 FilePathUtils (com.yydcdut.note.utils.FilePathUtils)7 List (java.util.List)7 Handler (android.os.Handler)6 ComparatorFactory (com.yydcdut.note.model.compare.ComparatorFactory)6 RxPhotoNote (com.yydcdut.note.model.rx.RxPhotoNote)6 YLog (com.yydcdut.note.utils.YLog)6 PhotoNote (com.yydcdut.note.entity.PhotoNote)5 IView (com.yydcdut.note.views.IView)5 EventBus (org.greenrobot.eventbus.EventBus)5 AndroidSchedulers (rx.android.schedulers.AndroidSchedulers)5 Schedulers (rx.schedulers.Schedulers)5 Intent (android.content.Intent)4 R (com.yydcdut.note.R)4 CategoryDeleteEvent (com.yydcdut.note.bus.CategoryDeleteEvent)4 CategoryEditEvent (com.yydcdut.note.bus.CategoryEditEvent)4