Search in sources :

Example 1 with PushData

use of com.wingjay.jianshi.db.model.PushData in project jianshi by wingjay.

the class Change method handleChangeByDBKey.

public static void handleChangeByDBKey(DBKey dbKey, JsonObject object) {
    JsonObject jsonObject = new JsonObject();
    jsonObject.add(dbKey.getKey(), object);
    PushData pushData = new PushData();
    pushData.setData(jsonObject.toString());
    pushData.setTimeCreated(DateUtil.getCurrentTimeStamp());
    pushData.save();
}
Also used : JsonObject(com.google.gson.JsonObject) PushData(com.wingjay.jianshi.db.model.PushData)

Example 2 with PushData

use of com.wingjay.jianshi.db.model.PushData 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();
                }
            }
        }
    });
}
Also used : SyncModel(com.wingjay.jianshi.bean.SyncModel) JsonDataResponse(com.wingjay.jianshi.network.JsonDataResponse) JsonObject(com.google.gson.JsonObject) PushData(com.wingjay.jianshi.db.model.PushData) Diary(com.wingjay.jianshi.db.model.Diary) JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonObject (com.google.gson.JsonObject)2 PushData (com.wingjay.jianshi.db.model.PushData)2 JsonArray (com.google.gson.JsonArray)1 JsonParser (com.google.gson.JsonParser)1 SyncModel (com.wingjay.jianshi.bean.SyncModel)1 Diary (com.wingjay.jianshi.db.model.Diary)1 JsonDataResponse (com.wingjay.jianshi.network.JsonDataResponse)1