use of com.wingjay.jianshi.db.model.Diary in project jianshi by wingjay.
the class DiaryListAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(DiaryListViewHolder diaryListViewHolder, int position) {
final Diary d = diaryList.get(position);
if (position == 0) {
diaryListViewHolder.year.setVisibility(View.VISIBLE);
} else {
if (diaryList.get(position).getYearCN().equals(diaryList.get(position - 1).getYearCN())) {
diaryListViewHolder.year.setVisibility(View.GONE);
} else {
diaryListViewHolder.year.setVisibility(View.VISIBLE);
}
}
diaryListViewHolder.title.setText(d.getCatalogueTitle());
diaryListViewHolder.year.setText(d.getYearCN());
diaryListViewHolder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onItemClick(d);
}
}
});
diaryListViewHolder.title.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (listener != null) {
listener.onItemLongClick(d);
}
return true;
}
});
}
use of com.wingjay.jianshi.db.model.Diary in project jianshi by wingjay.
the class SyncManager method sync.
public synchronized void sync(@Nullable final SyncResultListener syncResultListener) {
final List<PushData> pushDataList = SQLite.select().from(PushData.class).queryList();
JsonParser jsonParser = new JsonParser();
JsonObject syncData = new JsonObject();
JsonArray array = new JsonArray();
for (PushData pushData : pushDataList) {
array.add(jsonParser.parse(pushData.getData()));
}
syncData.add("sync_items", array);
syncData.add("need_pull", gson.toJsonTree(1));
syncData.add("sync_token", gson.toJsonTree(userPrefs.getSyncToken()));
Timber.d("Sync Data : %s", syncData.toString());
userService.sync(syncData).subscribe(new Action1<JsonDataResponse<SyncModel>>() {
@Override
public void call(JsonDataResponse<SyncModel> response) {
if (response.getRc() == Constants.ServerResultCode.RESULT_OK) {
SyncModel syncModel = response.getData();
userPrefs.setSyncToken(syncModel.getSyncToken());
if (syncModel.getUpsert() != null) {
for (Diary diary : syncModel.getUpsert()) {
diary.save();
}
}
if (syncModel.getDelete() != null) {
for (Diary diary : syncModel.getDelete()) {
diary.save();
}
}
int syncCount = syncModel.getSyncedCount();
for (PushData pushData : pushDataList) {
SQLite.delete().from(PushData.class).where(PushData_Table.id.eq(pushData.getId())).execute();
syncCount--;
if (syncCount <= 0) {
break;
}
}
}
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Timber.e(throwable, "Sync Failed");
}
});
Observable.just(null).subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Object>() {
@Override
public void call(Object o) {
if (syncResultListener != null) {
if (SQLite.select().from(PushData.class).queryList().size() > 0) {
syncResultListener.onFailure();
} else {
syncResultListener.onSuccess();
}
}
}
});
}
use of com.wingjay.jianshi.db.model.Diary in project jianshi by wingjay.
the class EditActivity method saveDiary.
private void saveDiary() {
if (!checkNotNull()) {
Toast.makeText(EditActivity.this, R.string.edit_content_not_null, Toast.LENGTH_SHORT).show();
return;
}
String titleString = (TextUtils.isEmpty(title.getText().toString())) ? title.getHint().toString() : title.getText().toString();
String contentString = (TextUtils.isEmpty(content.getText().toString())) ? content.getHint().toString() : content.getText().toString();
if (diary == null) {
diary = new Diary();
diary.setTitle(titleString);
diary.setContent(contentString);
diary.setUuid(UUID.randomUUID().toString().toUpperCase());
diary.setTime_created(DateUtil.getCurrentTimeStamp());
} else {
diary.setTitle(titleString);
diary.setContent(contentString);
diary.setTime_modified(DateUtil.getCurrentTimeStamp());
}
diaryService.saveDiary(diary).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
Intent i = ViewActivity.createIntent(EditActivity.this, diary.getUuid());
startActivity(i);
finish();
}
});
}
Aggregations