Search in sources :

Example 1 with JsonDataResponse

use of com.wingjay.jianshi.network.JsonDataResponse in project jianshi by wingjay.

the class SyncManager method syncLog.

public synchronized void syncLog() {
    final List<EventLog> logItems = SQLite.select().from(EventLog.class).queryList();
    JsonObject syncLog = new JsonObject();
    JsonArray array = new JsonArray();
    for (EventLog eventLog : logItems) {
        array.add(new Gson().toJsonTree(eventLog));
    }
    syncLog.add("log_items", array);
    userService.syncLog(syncLog).subscribe(new Action1<JsonDataResponse<JsonObject>>() {

        @Override
        public void call(JsonDataResponse<JsonObject> jsonObjectJsonDataResponse) {
            if (jsonObjectJsonDataResponse.getRc() == Constants.ServerResultCode.RESULT_OK) {
                JsonObject object = jsonObjectJsonDataResponse.getData();
                int syncedCount = object.get("synced_count").getAsInt();
                Timber.i("synced log count %d", syncedCount);
                for (EventLog eventLog : logItems) {
                    SQLite.delete(EventLog.class).where(EventLog_Table.id.eq(eventLog.getId())).execute();
                    syncedCount--;
                    if (syncedCount <= 0) {
                        break;
                    }
                }
            }
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            Timber.e(throwable, "SyncLog failed");
        }
    });
}
Also used : JsonArray(com.google.gson.JsonArray) JsonDataResponse(com.wingjay.jianshi.network.JsonDataResponse) EventLog(com.wingjay.jianshi.db.model.EventLog) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson)

Example 2 with JsonDataResponse

use of com.wingjay.jianshi.network.JsonDataResponse 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)

Example 3 with JsonDataResponse

use of com.wingjay.jianshi.network.JsonDataResponse in project jianshi by wingjay.

the class UserManager method login.

public void login(final Context context, @NonNull String email, @NonNull String password) {
    final ProgressDialog dialog = ProgressDialog.show(context, context.getString(R.string.logining), "");
    userService.login(email, password).compose(RxUtil.<JsonDataResponse<User>>normalSchedulers()).doOnTerminate(new Action0() {

        @Override
        public void call() {
            dialog.dismiss();
        }
    }).subscribe(new Action1<JsonDataResponse<User>>() {

        @Override
        public void call(JsonDataResponse<User> userJsonDataResponse) {
            if (userJsonDataResponse.getRc() == Constants.ServerResultCode.RESULT_OK) {
                User user = userJsonDataResponse.getData();
                if (user == null || user.getId() <= 0 || TextUtils.isEmpty(user.getEncryptedToken())) {
                    Toast.makeText(context, context.getString(R.string.login_failure), Toast.LENGTH_SHORT).show();
                    return;
                }
                userPrefsLazy.get().setAuthToken(user.getEncryptedToken());
                userPrefsLazy.get().setUser(user);
                context.startActivity(MainActivity.createIntent(context));
            } else {
                Timber.e("login failure msg: %s", userJsonDataResponse.getMsg());
                Toast.makeText(context, userJsonDataResponse.getMsg(), Toast.LENGTH_SHORT).show();
            }
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable e) {
            Timber.e(e, "login failure");
            Toast.makeText(context, context.getString(R.string.network_error), Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : Action0(rx.functions.Action0) User(com.wingjay.jianshi.bean.User) JsonDataResponse(com.wingjay.jianshi.network.JsonDataResponse) ProgressDialog(android.app.ProgressDialog)

Example 4 with JsonDataResponse

use of com.wingjay.jianshi.network.JsonDataResponse in project jianshi by wingjay.

the class UserManager method signup.

public void signup(final Context context, @NonNull String email, @NonNull String password) {
    final ProgressDialog dialog = ProgressDialog.show(context, context.getString(R.string.signuping), "");
    userService.signup(email, password).compose(RxUtil.<JsonDataResponse<User>>normalSchedulers()).doOnTerminate(new Action0() {

        @Override
        public void call() {
            dialog.dismiss();
        }
    }).subscribe(new Action1<JsonDataResponse<User>>() {

        @Override
        public void call(JsonDataResponse<User> userJsonDataResponse) {
            if (userJsonDataResponse.getRc() == Constants.ServerResultCode.RESULT_OK) {
                User user = userJsonDataResponse.getData();
                if (user == null || user.getId() <= 0 || TextUtils.isEmpty(user.getEncryptedToken())) {
                    Toast.makeText(context, context.getString(R.string.signup_failure), Toast.LENGTH_SHORT).show();
                    return;
                }
                userPrefsLazy.get().setAuthToken(user.getEncryptedToken());
                userPrefsLazy.get().setUser(user);
                context.startActivity(MainActivity.createIntent(context));
            } else {
                Timber.e("signup failure msg: %s", userJsonDataResponse.getMsg());
                Toast.makeText(context, userJsonDataResponse.getMsg(), Toast.LENGTH_SHORT).show();
            }
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable e) {
            Timber.e(e, "signup failure");
            Toast.makeText(context, context.getString(R.string.network_error), Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : Action0(rx.functions.Action0) User(com.wingjay.jianshi.bean.User) JsonDataResponse(com.wingjay.jianshi.network.JsonDataResponse) ProgressDialog(android.app.ProgressDialog)

Aggregations

JsonDataResponse (com.wingjay.jianshi.network.JsonDataResponse)4 ProgressDialog (android.app.ProgressDialog)2 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 User (com.wingjay.jianshi.bean.User)2 Action0 (rx.functions.Action0)2 Gson (com.google.gson.Gson)1 JsonParser (com.google.gson.JsonParser)1 SyncModel (com.wingjay.jianshi.bean.SyncModel)1 Diary (com.wingjay.jianshi.db.model.Diary)1 EventLog (com.wingjay.jianshi.db.model.EventLog)1 PushData (com.wingjay.jianshi.db.model.PushData)1