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();
});
}
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);
}
});
}
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)));
}
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;
}
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;
}
Aggregations