Search in sources :

Example 1 with SaveListener

use of cn.bmob.v3.listener.SaveListener in project CoCoin by Nightonke.

the class AccountBookTodayViewActivity method sync.

private void sync() {
    if (!SettingManager.getInstance().getLoggenOn()) {
        CoCoinUtil.showToast(mContext, R.string.login_tip);
    } else {
        syncSuccessNumber = 0;
        syncFailedNumber = 0;
        syncQueryDialog = new MaterialDialog.Builder(this).title(R.string.sync_querying_title).content(R.string.sync_querying_content).negativeText(R.string.cancel).progress(true, 0).onAny(new MaterialDialog.SingleButtonCallback() {

            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                if (which == DialogAction.NEGATIVE) {
                }
            }
        }).show();
        final User user = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class);
        final MyQuery myQuery = new MyQuery();
        myQuery.setTask(++TaskManager.QUERY_UPDATE_TASK);
        myQuery.query = new BmobQuery<>();
        myQuery.query.addWhereEqualTo("userId", user.getObjectId());
        myQuery.query.setLimit(1);
        myQuery.query.findObjects(CoCoinApplication.getAppContext(), new FindListener<UploadInfo>() {

            @Override
            public void onSuccess(List<UploadInfo> object) {
                if (myQuery.getTask() != TaskManager.QUERY_UPDATE_TASK)
                    return;
                else {
                    syncQueryDialog.dismiss();
                    cloudRecordNumber = 0;
                    Calendar cal = null;
                    if (object.size() == 0) {
                    } else {
                        cloudRecordNumber = object.get(0).getRecordNumber();
                        cloudOldDatabaseUrl = object.get(0).getDatabaseUrl();
                        cloudOldDatabaseFileName = object.get(0).getFileName();
                        uploadObjectId = object.get(0).getObjectId();
                        cal = Calendar.getInstance();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        try {
                            cal.setTime(sdf.parse(object.get(0).getUpdatedAt()));
                        } catch (ParseException p) {
                        }
                    }
                    String content = CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_cloud_record_0) + cloudRecordNumber + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_cloud_record_1) + (cal == null ? CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_cloud_time_2) : CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_cloud_time_0) + CoCoinUtil.GetCalendarString(CoCoinApplication.getAppContext(), cal) + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_cloud_time_1)) + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_mobile_record_0) + RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size() + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_mobile_record_1) + (SettingManager.getInstance().getRecentlySyncTime() == null ? CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_mobile_time_2) : CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_mobile_time_0) + CoCoinUtil.GetCalendarString(CoCoinApplication.getAppContext(), SettingManager.getInstance().getRecentlySyncTime()) + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_info_mobile_time_1)) + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sync_choose_content);
                    syncChooseDialog = new MaterialDialog.Builder(mContext).title(R.string.sync_choose_title).content(content).positiveText(R.string.sync_to_cloud).negativeText(R.string.sync_to_mobile).neutralText(R.string.cancel).onAny(new MaterialDialog.SingleButtonCallback() {

                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            syncChooseDialog.dismiss();
                            if (which == DialogAction.POSITIVE) {
                                // sync to cloud
                                String subContent = "";
                                if (RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size() == 0) {
                                    subContent = CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.mobile_record_empty);
                                    new MaterialDialog.Builder(mContext).title(R.string.sync).content(subContent).positiveText(R.string.ok_1).show();
                                    return;
                                } else {
                                    subContent = CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sure_to_cloud_0) + RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size() + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sure_to_cloud_1);
                                }
                                new MaterialDialog.Builder(mContext).title(R.string.sync).content(subContent).positiveText(R.string.ok_1).negativeText(R.string.cancel).onAny(new MaterialDialog.SingleButtonCallback() {

                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        if (which == DialogAction.POSITIVE) {
                                            syncProgressDialog = new MaterialDialog.Builder(mContext).title(R.string.syncing).content(CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.uploading_0) + "1" + CoCoinUtil.GetString(mContext, R.string.uploading_1)).progress(false, RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size(), true).cancelable(false).show();
                                            final String databasePath = CoCoinUtil.GetRecordDatabasePath(CoCoinApplication.getAppContext());
                                            //                                                                final BmobFile bmobFile = new BmobFile(new File(databasePath));
                                            //                                                                bmobFile.uploadblock(mContext, new UploadFileListener() {
                                            //
                                            //                                                                    @Override
                                            //                                                                    public void onSuccess() {
                                            //                                                                        if (BuildConfig.DEBUG) {
                                            //                                                                            Log.d("CoCoin", "Upload successfully fileName: " + databasePath);
                                            //                                                                            Log.d("CoCoin", "Upload successfully url: " + bmobFile.getFileUrl(mContext));
                                            //                                                                        }
                                            //                                                                        // the new database is uploaded successfully
                                            //                                                                        // delete the old database(if there is)
                                            //                                                                        if (cloudOldDatabaseUrl != null) {
                                            //                                                                            deleteOldDatabaseOnCloud(cloudOldDatabaseUrl);
                                            //                                                                        }
                                            //                                                                        // update the UploadInfo record for the new url
                                            //                                                                        if (uploadObjectId == null) {
                                            //                                                                            // first time
                                            //                                                                            UploadInfo uploadInfo = new UploadInfo();
                                            //                                                                            uploadInfo.setUserId(user.getObjectId());
                                            //                                                                            uploadInfo.setRecordNumber(RecordManager.getInstance(mContext).RECORDS.size());
                                            //                                                                            uploadInfo.setDatabaseUrl(bmobFile.getFileUrl(mContext));
                                            //                                                                            uploadInfo.save(mContext, new SaveListener() {
                                            //                                                                                @Override
                                            //                                                                                public void onSuccess() {
                                            //                                                                                    // upload successfully
                                            //                                                                                    syncProgressDialog.dismiss();
                                            //                                                                                    new MaterialDialog.Builder(mContext)
                                            //                                                                                            .title(R.string.sync_completely_title)
                                            //                                                                                            .content(RecordManager.getInstance(mContext).RECORDS.size() + CoCoinUtil.GetString(mContext, R.string.uploading_fail_1))
                                            //                                                                                            .positiveText(R.string.ok_1)
                                            //                                                                                            .show();
                                            //                                                                                }
                                            //                                                                                @Override
                                            //                                                                                public void onFailure(int code, String arg0) {
                                            //                                                                                    // 添加失败
                                            //                                                                                }
                                            //                                                                            });
                                            //                                                                        } else {
                                            //                                                                            UploadInfo uploadInfo = new UploadInfo();
                                            //                                                                            uploadInfo.setUserId(user.getObjectId());
                                            //                                                                            uploadInfo.setRecordNumber(RecordManager.getInstance(mContext).RECORDS.size());
                                            //                                                                            uploadInfo.setDatabaseUrl(bmobFile.getFileUrl(mContext));
                                            //                                                                            uploadInfo.update(mContext, uploadObjectId, new UpdateListener() {
                                            //                                                                                @Override
                                            //                                                                                public void onSuccess() {
                                            //                                                                                    // upload successfully
                                            //                                                                                    syncProgressDialog.dismiss();
                                            //                                                                                    new MaterialDialog.Builder(mContext)
                                            //                                                                                            .title(R.string.sync_completely_title)
                                            //                                                                                            .content(RecordManager.getInstance(mContext).RECORDS.size() + CoCoinUtil.GetString(mContext, R.string.uploading_fail_1))
                                            //                                                                                            .positiveText(R.string.ok_1)
                                            //                                                                                            .show();
                                            //                                                                                }
                                            //                                                                                @Override
                                            //                                                                                public void onFailure(int code, String msg) {
                                            //                                                                                    // upload failed
                                            //                                                                                    Log.i("bmob","更新失败:"+msg);
                                            //                                                                                }
                                            //                                                                            });
                                            //                                                                        }
                                            //                                                                    }
                                            //
                                            //                                                                    @Override
                                            //                                                                    public void onProgress(Integer value) {
                                            //                                                                        syncProgressDialog.setProgress(value);
                                            //                                                                    }
                                            //
                                            //                                                                    @Override
                                            //                                                                    public void onFailure(int code, String msg) {
                                            //                                                                        // upload failed
                                            //                                                                        if (BuildConfig.DEBUG) Log.d("CoCoin", "Upload database failed " + code + " " + msg);
                                            //                                                                        syncProgressDialog.dismiss();
                                            //                                                                        new MaterialDialog.Builder(mContext)
                                            //                                                                                .title(R.string.sync_failed)
                                            //                                                                                .content(R.string.uploading_fail_0)
                                            //                                                                                .positiveText(R.string.ok_1)
                                            //                                                                                .show();
                                            //                                                                    }
                                            //                                                                });
                                            BmobProFile.getInstance(CoCoinApplication.getAppContext()).upload(databasePath, new UploadListener() {

                                                @Override
                                                public void onSuccess(String fileName, String url, BmobFile file) {
                                                    CoCoinUtil.deleteBmobUploadCach(CoCoinApplication.getAppContext());
                                                    if (BuildConfig.DEBUG) {
                                                        Log.d("CoCoin", "Upload successfully fileName: " + fileName);
                                                        Log.d("CoCoin", "Upload successfully url: " + url);
                                                    }
                                                    // delete the old database(if there is)
                                                    if (cloudOldDatabaseFileName != null) {
                                                        deleteOldDatabaseOnCloud(cloudOldDatabaseFileName);
                                                    }
                                                    // update the UploadInfo record for the new url
                                                    UploadInfo uploadInfo = new UploadInfo();
                                                    uploadInfo.setUserId(user.getObjectId());
                                                    uploadInfo.setRecordNumber(RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size());
                                                    uploadInfo.setDatabaseUrl(file.getFileUrl(CoCoinApplication.getAppContext()));
                                                    uploadInfo.setFileName(fileName);
                                                    if (uploadObjectId == null) {
                                                        // insert
                                                        uploadInfo.save(CoCoinApplication.getAppContext(), new SaveListener() {

                                                            @Override
                                                            public void onSuccess() {
                                                                // upload successfully
                                                                syncProgressDialog.dismiss();
                                                                new MaterialDialog.Builder(mContext).title(R.string.sync_completely_title).content(RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size() + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.uploading_fail_1)).positiveText(R.string.ok_1).cancelable(false).show();
                                                            }

                                                            @Override
                                                            public void onFailure(int code, String arg0) {
                                                                uploadFailed(code, arg0);
                                                            }
                                                        });
                                                    } else {
                                                        // update
                                                        uploadInfo.update(CoCoinApplication.getAppContext(), uploadObjectId, new UpdateListener() {

                                                            @Override
                                                            public void onSuccess() {
                                                                // upload successfully
                                                                syncProgressDialog.dismiss();
                                                                new MaterialDialog.Builder(mContext).title(R.string.sync_completely_title).content(RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size() + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.uploading_fail_1)).positiveText(R.string.ok_1).cancelable(false).show();
                                                            }

                                                            @Override
                                                            public void onFailure(int code, String msg) {
                                                                uploadFailed(code, msg);
                                                            }
                                                        });
                                                    }
                                                }

                                                @Override
                                                public void onProgress(int progress) {
                                                    syncProgressDialog.setProgress((int) (progress * 1.0 / 100 * RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size()));
                                                }

                                                @Override
                                                public void onError(int statuscode, String errormsg) {
                                                    // upload failed
                                                    uploadFailed(statuscode, errormsg);
                                                }
                                            });
                                        }
                                    }
                                }).show();
                            } else if (which == DialogAction.NEGATIVE) {
                                // sync to mobile
                                String subContent = "";
                                if (cloudRecordNumber == 0) {
                                    subContent = CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.cloud_record_empty);
                                    new MaterialDialog.Builder(mContext).title(R.string.sync).content(subContent).positiveText(R.string.ok_1).show();
                                    return;
                                } else {
                                    subContent = CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sure_to_mobile_0) + cloudRecordNumber + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.sure_to_mobile_1);
                                }
                                new MaterialDialog.Builder(mContext).title(R.string.sync).content(subContent).positiveText(R.string.ok_1).negativeText(R.string.cancel).onAny(new MaterialDialog.SingleButtonCallback() {

                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        if (which == DialogAction.POSITIVE) {
                                            syncProgressDialog = new MaterialDialog.Builder(mContext).title(R.string.syncing).content(CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.downloading_0) + "1" + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.downloading_1)).progress(false, cloudRecordNumber, true).cancelable(false).show();
                                            // download the database file to mobile
                                            BmobProFile.getInstance(CoCoinApplication.getAppContext()).download(cloudOldDatabaseFileName, new DownloadListener() {

                                                @Override
                                                public void onSuccess(String fullPath) {
                                                    // copy the new database to mobile
                                                    try {
                                                        Log.d("CoCoin", "Download successfully " + fullPath);
                                                        syncProgressDialog.setContent(R.string.sync_completely_content);
                                                        byte[] buffer = new byte[1024];
                                                        File file = new File(fullPath);
                                                        InputStream inputStream = new FileInputStream(file);
                                                        String outFileNameString = CoCoinUtil.GetRecordDatabasePath(CoCoinApplication.getAppContext());
                                                        OutputStream outputStream = new FileOutputStream(outFileNameString);
                                                        int length;
                                                        while ((length = inputStream.read(buffer)) > 0) {
                                                            outputStream.write(buffer, 0, length);
                                                        }
                                                        Log.d("CoCoin", "Download successfully copy completely");
                                                        outputStream.flush();
                                                        outputStream.close();
                                                        inputStream.close();
                                                        file.delete();
                                                        Log.d("CoCoin", "Download successfully delete completely");
                                                        // refresh data
                                                        RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.clear();
                                                        RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS = null;
                                                        RecordManager.getInstance(CoCoinApplication.getAppContext());
                                                        todayModeAdapter.notifyDataSetChanged();
                                                        Log.d("CoCoin", "Download successfully refresh completely");
                                                        syncProgressDialog.dismiss();
                                                        new MaterialDialog.Builder(mContext).title(R.string.sync_completely_title).content(cloudRecordNumber + CoCoinUtil.GetString(CoCoinApplication.getAppContext(), R.string.downloading_fail_1)).positiveText(R.string.ok_1).cancelable(false).show();
                                                    } catch (IOException i) {
                                                        i.printStackTrace();
                                                    }
                                                }

                                                @Override
                                                public void onProgress(String localPath, int percent) {
                                                    syncProgressDialog.setProgress((int) (((float) percent / 100) * RecordManager.getInstance(CoCoinApplication.getAppContext()).RECORDS.size()));
                                                }

                                                @Override
                                                public void onError(int statuscode, String errormsg) {
                                                    downloadFailed(statuscode, errormsg);
                                                }
                                            });
                                        }
                                    }
                                }).show();
                            } else {
                            }
                        }
                    }).show();
                }
            }

            @Override
            public void onError(int code, String msg) {
                syncQueryDialog.dismiss();
                if (BuildConfig.DEBUG)
                    Log.d("CoCoin", "Query: " + msg);
                if (syncQueryDialog != null)
                    syncQueryDialog.dismiss();
                new MaterialDialog.Builder(mContext).title(R.string.sync_querying_fail_title).content(R.string.sync_querying_fail_content).positiveText(R.string.ok_1).show();
            }
        });
    }
}
Also used : SaveListener(cn.bmob.v3.listener.SaveListener) BmobUser(cn.bmob.v3.BmobUser) User(com.nightonke.saver.model.User) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) UploadListener(com.bmob.btp.callback.UploadListener) NonNull(android.support.annotation.NonNull) UpdateListener(cn.bmob.v3.listener.UpdateListener) BmobFile(cn.bmob.v3.datatype.BmobFile) UploadInfo(com.nightonke.saver.model.UploadInfo) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DownloadListener(com.bmob.btp.callback.DownloadListener) DialogAction(com.afollestad.materialdialogs.DialogAction) FileOutputStream(java.io.FileOutputStream) MyQuery(com.nightonke.saver.ui.MyQuery) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) BmobFile(cn.bmob.v3.datatype.BmobFile) BmobProFile(com.bmob.BmobProFile) File(java.io.File)

Example 2 with SaveListener

use of cn.bmob.v3.listener.SaveListener in project CoCoin by Nightonke.

the class AccountBookSettingActivity method uploadLogoToServer.

// update a logo to server//////////////////////////////////////////////////////////////////////////
private void uploadLogoToServer() {
    if (!SettingManager.getInstance().getHasLogo()) {
        // the user haven't set the logo
        return;
    }
    final File file = new File(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME);
    final User user = getCurrentUser();
    // if login/////////////////////////////////////////////////////////////////////////////////////////
    if (user != null) {
        if (user.getLogoObjectId() != "") {
            // if the logo id is not null, then there must be a logo and a logo file in the server//////////////
            // judge whether there is an old logo of the same user//////////////////////////////////////////////
            BmobQuery<Logo> bmobQuery = new BmobQuery();
            bmobQuery.addWhereEqualTo("objectId", user.getLogoObjectId());
            bmobQuery.findObjects(CoCoinApplication.getAppContext(), new FindListener<Logo>() {

                @Override
                public void onSuccess(List<Logo> object) {
                    // there has been an old logo in the server/////////////////////////////////////////////////////////
                    // then there must be an old logo file in server////////////////////////////////////////////////////
                    // then we should delete the old one////////////////////////////////////////////////////////////////
                    Log.d("Saver", "There is an old logo");
                    String url = object.get(0).getFile().getUrl();
                    BmobFile oldLogoFile = new BmobFile();
                    oldLogoFile.setUrl(url);
                    oldLogoFile.delete(CoCoinApplication.getAppContext(), new DeleteListener() {

                        @Override
                        public void onSuccess() {
                            Log.d("Saver", "Successfully delete the old logo.");
                            // after delete, we should upload a new logo file///////////////////////////////////////////////////
                            final BmobFile newLogoFile = new BmobFile(file);
                            newLogoFile.uploadblock(CoCoinApplication.getAppContext(), new UploadFileListener() {

                                @Override
                                public void onSuccess() {
                                    // after upload the new logo file, we should put the new logo the Logo table////////////////////////
                                    Logo newLogo = new Logo(newLogoFile);
                                    newLogo.update(CoCoinApplication.getAppContext(), user.getLogoObjectId(), new UpdateListener() {

                                        @Override
                                        public void onSuccess() {
                                            Log.d("Saver", "Update logo successfully");
                                        }

                                        @Override
                                        public void onFailure(int arg0, String arg1) {
                                            Log.d("Saver", "Update logo failed " + arg1);
                                        }
                                    });
                                }

                                @Override
                                public void onProgress(Integer arg0) {
                                }

                                @Override
                                public void onFailure(int arg0, String arg1) {
                                    Log.d("Saver", "Upload failed " + arg1);
                                }
                            });
                        }

                        @Override
                        public void onFailure(int code, String msg) {
                            Log.d("Saver", "Fail to delete the old logo. " + msg);
                        }
                    });
                }

                @Override
                public void onError(int code, String msg) {
                    // the picture is lost
                    Log.d("Saver", "Can't find the old logo in server.");
                }
            });
        } else {
            // the user has no logo before//////////////////////////////////////////////////////////////////////
            final BmobFile newLogoFile = new BmobFile(file);
            newLogoFile.uploadblock(CoCoinApplication.getAppContext(), new UploadFileListener() {

                @Override
                public void onSuccess() {
                    String url = newLogoFile.getFileUrl(CoCoinApplication.getAppContext());
                    Log.d("Saver", "Upload successfully " + url);
                    final Logo newLogo = new Logo(newLogoFile);
                    newLogo.save(CoCoinApplication.getAppContext(), new SaveListener() {

                        @Override
                        public void onSuccess() {
                            Log.d("Saver", "Save the new logo successfully.");
                            SettingManager.getInstance().setLogoObjectId(newLogo.getObjectId());
                            updateSettingsToServer(UPDATE_LOGO_ID);
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            Log.d("Saver", "Save the new logo fail.");
                        }
                    });
                }

                @Override
                public void onProgress(Integer arg0) {
                }

                @Override
                public void onFailure(int arg0, String arg1) {
                    Log.d("Saver", "Upload failed " + arg1);
                }
            });
        }
    }
}
Also used : SaveListener(cn.bmob.v3.listener.SaveListener) BmobUser(cn.bmob.v3.BmobUser) User(com.nightonke.saver.model.User) UploadFileListener(cn.bmob.v3.listener.UploadFileListener) Logo(com.nightonke.saver.model.Logo) BmobQuery(cn.bmob.v3.BmobQuery) DeleteListener(cn.bmob.v3.listener.DeleteListener) UpdateListener(cn.bmob.v3.listener.UpdateListener) BmobFile(cn.bmob.v3.datatype.BmobFile) File(java.io.File) BmobFile(cn.bmob.v3.datatype.BmobFile)

Example 3 with SaveListener

use of cn.bmob.v3.listener.SaveListener in project CoCoin by Nightonke.

the class AccountBookSettingActivity method userRegister.

private void userRegister() {
    registerDialog = new MaterialDialog.Builder(this).title(R.string.go_register).typeface(CoCoinUtil.GetTypeface(), CoCoinUtil.GetTypeface()).customView(R.layout.dialog_user_register, true).build();
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    registerDialogView = registerDialog.getCustomView();
    registerDialogButton = (CircularProgressButton) registerDialogView.findViewById(R.id.button);
    registerDialogButton.setTypeface(CoCoinUtil.GetTypeface());
    registerDialogButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            registerDialogButton.setProgress(1);
            registerDialog.setCancelable(false);
            // User register, a new user////////////////////////////////////////////////////////////////////////
            final User user = new User();
            // basic info
            user.setUsername(registerUserName.getText().toString());
            user.setPassword(registerPassword.getText().toString());
            user.setEmail(registerUserEmail.getText().toString());
            user.setAndroidId(CoCoinApplication.getAndroidId());
            if (BuildConfig.DEBUG)
                Log.d("CoCoin", "Android Id: " + user.getAndroidId());
            // settings info
            // user.setLogo();
            user.setIsMonthLimit(SettingManager.getInstance().getIsMonthLimit());
            user.setMonthLimit(SettingManager.getInstance().getMonthLimit());
            user.setIsColorRemind(SettingManager.getInstance().getIsColorRemind());
            user.setMonthWarning(SettingManager.getInstance().getMonthWarning());
            user.setRemindColor(SettingManager.getInstance().getRemindColor());
            user.setIsForbidden(SettingManager.getInstance().getIsForbidden());
            user.setAccountBookName(SettingManager.getInstance().getAccountBookName());
            user.setAccountBookPassword(SettingManager.getInstance().getPassword());
            // Todo store tag order
            user.setShowPicture(SettingManager.getInstance().getShowPicture());
            user.setIsHollow(SettingManager.getInstance().getIsHollow());
            user.setLogoObjectId("");
            user.signUp(CoCoinApplication.getAppContext(), new SaveListener() {

                @Override
                public void onSuccess() {
                    registerDialogButton.setProgress(0);
                    registerDialog.setCancelable(true);
                    registerDialogButton.setIdleText(getResourceString(R.string.register_complete));
                    // if register successfully/////////////////////////////////////////////////////////////////////////
                    SettingManager.getInstance().setLoggenOn(true);
                    SettingManager.getInstance().setUserName(registerUserName.getText().toString());
                    SettingManager.getInstance().setUserEmail(registerUserEmail.getText().toString());
                    SettingManager.getInstance().setUserPassword(registerPassword.getText().toString());
                    showToast(4, registerUserName.getText().toString());
                    // if login successfully////////////////////////////////////////////////////////////////////////////
                    user.login(CoCoinApplication.getAppContext(), new SaveListener() {

                        @Override
                        public void onSuccess() {
                            SettingManager.getInstance().setTodayViewInfoShouldChange(true);
                            updateViews();
                        // use a new method
                        //                                RecordManager.updateOldRecordsToServer();
                        }

                        @Override
                        public void onFailure(int code, String msg) {
                        // if login failed//////////////////////////////////////////////////////////////////////////////////
                        }
                    });
                    final Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            if (registerDialog != null)
                                registerDialog.dismiss();
                            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
                        }
                    }, 500);
                }

                // if register failed///////////////////////////////////////////////////////////////////////////////
                @Override
                public void onFailure(int code, String msg) {
                    if (BuildConfig.DEBUG)
                        Log.d("CoCoin", "Register failed: " + msg);
                    String tip = getResourceString(R.string.network_disconnection);
                    if (msg.charAt(1) == 's')
                        tip = getResourceString(R.string.user_name_exist);
                    if (msg.charAt(0) == 'e')
                        tip = getResourceString(R.string.user_email_exist);
                    if (msg.charAt(1) == 'n')
                        tip = getResourceString(R.string.user_mobile_exist);
                    registerDialogButton.setIdleText(tip);
                    registerDialogButton.setProgress(0);
                    registerDialog.setCancelable(true);
                }
            });
        }
    });
    final MDButton positiveAction = registerDialog.getActionButton(DialogAction.POSITIVE);
    positiveAction.setEnabled(false);
    final EmailValidator emailValidator = new EmailValidator();
    TextView userNameTV = (TextView) registerDialog.getCustomView().findViewById(R.id.register_user_name_text);
    TextView userEmailTV = (TextView) registerDialog.getCustomView().findViewById(R.id.register_user_email_text);
    TextView userPasswordTV = (TextView) registerDialog.getCustomView().findViewById(R.id.register_password_text);
    userNameTV.setTypeface(CoCoinUtil.GetTypeface());
    userEmailTV.setTypeface(CoCoinUtil.GetTypeface());
    userPasswordTV.setTypeface(CoCoinUtil.GetTypeface());
    registerUserName = (MaterialEditText) registerDialog.getCustomView().findViewById(R.id.register_user_name);
    registerUserEmail = (MaterialEditText) registerDialog.getCustomView().findViewById(R.id.register_user_email);
    registerPassword = (MaterialEditText) registerDialog.getCustomView().findViewById(R.id.register_password);
    registerUserName.setTypeface(CoCoinUtil.GetTypeface());
    registerUserName.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            boolean emailOK = emailValidator.validate(registerUserEmail.getText().toString());
            registerDialogButton.setEnabled(0 < registerUserName.getText().toString().length() && registerUserName.getText().toString().length() <= 16 && registerPassword.getText().toString().length() > 0 && emailOK);
            if (emailValidator.validate(registerUserEmail.getText().toString())) {
                registerUserEmail.validate();
            } else {
                registerUserEmail.invalidate();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    registerUserEmail.setTypeface(CoCoinUtil.GetTypeface());
    registerUserEmail.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            boolean emailOK = emailValidator.validate(registerUserEmail.getText().toString());
            registerDialogButton.setEnabled(0 < registerUserName.getText().toString().length() && registerUserName.getText().toString().length() <= 16 && registerPassword.getText().toString().length() > 0 && emailOK);
            if (emailValidator.validate(registerUserEmail.getText().toString())) {
                registerUserEmail.validate();
            } else {
                registerUserEmail.invalidate();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    registerPassword.setTypeface(CoCoinUtil.GetTypeface());
    registerPassword.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            boolean emailOK = emailValidator.validate(registerUserEmail.getText().toString());
            registerDialogButton.setEnabled(0 < registerUserName.getText().toString().length() && registerUserName.getText().toString().length() <= 16 && registerPassword.getText().toString().length() > 0 && emailOK);
            if (emailValidator.validate(registerUserEmail.getText().toString())) {
                registerUserEmail.validate();
            } else {
                registerUserEmail.invalidate();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    registerDialog.show();
}
Also used : SaveListener(cn.bmob.v3.listener.SaveListener) MDButton(com.afollestad.materialdialogs.internal.MDButton) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) BmobUser(cn.bmob.v3.BmobUser) User(com.nightonke.saver.model.User) EmailValidator(com.nightonke.saver.util.EmailValidator) Handler(android.os.Handler) InputMethodManager(android.view.inputmethod.InputMethodManager) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) RiseNumberTextView(com.nightonke.saver.ui.RiseNumberTextView) View(android.view.View) TextView(android.widget.TextView) MaterialIconView(net.steamcrafted.materialiconlib.MaterialIconView) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) RiseNumberTextView(com.nightonke.saver.ui.RiseNumberTextView) TextView(android.widget.TextView)

Example 4 with SaveListener

use of cn.bmob.v3.listener.SaveListener in project CoCoin by Nightonke.

the class AccountBookSettingActivity method userLogin.

private void userLogin() {
    loginDialog = new MaterialDialog.Builder(this).title(R.string.go_login).typeface(CoCoinUtil.GetTypeface(), CoCoinUtil.GetTypeface()).customView(R.layout.dialog_user_login, true).build();
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    loginDialogView = loginDialog.getCustomView();
    loginDialogButton = (CircularProgressButton) loginDialogView.findViewById(R.id.button);
    loginDialogButton.isIndeterminateProgressMode();
    loginDialogButton.setProgress(0);
    loginDialogButton.setTypeface(CoCoinUtil.GetTypeface());
    loginDialogButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            loginDialog.setCancelable(false);
            loginDialogButton.setProgress(1);
            // the user ask to login////////////////////////////////////////////////////////////////////////////
            final User user = new User();
            user.setUsername(loginUserName.getText().toString());
            user.setPassword(loginPassword.getText().toString());
            user.login(CoCoinApplication.getAppContext(), new SaveListener() {

                // try with user name///////////////////////////////////////////////////////////////////////////////
                @Override
                public void onSuccess() {
                    loginDialog.setCancelable(true);
                    loginDialogButton.setProgress(0);
                    loginDialogButton.setIdleText(getResourceString(R.string.login_complete));
                    // login successfully through user name/////////////////////////////////////////////////////////////
                    SettingManager.getInstance().setTodayViewInfoShouldChange(true);
                    User loginUser = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class);
                    if (!CoCoinApplication.getAndroidId().equals(loginUser.getAndroidId())) {
                        // 2 users on one mobile////////////////////////////////////////////////////////////////////////////
                        showToast(7, "unique...");
                        return;
                    }
                    SettingManager.getInstance().setLoggenOn(true);
                    SettingManager.getInstance().setUserName(loginUserName.getText().toString());
                    SettingManager.getInstance().setUserEmail(loginUser.getEmail());
                    updateViews();
                    // use a new method
                    //                        RecordManager.updateOldRecordsToServer();
                    whetherSyncSettingsFromServer();
                    showToast(6, loginUserName.getText().toString());
                    final Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            if (loginDialog != null)
                                loginDialog.dismiss();
                            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
                        }
                    }, 500);
                }

                // login fail through user name/////////////////////////////////////////////////////////////////////
                @Override
                public void onFailure(int code, String msg) {
                    // try with user email//////////////////////////////////////////////////////////////////////////////
                    user.setEmail(loginUserName.getText().toString());
                    user.login(CoCoinApplication.getAppContext(), new SaveListener() {

                        @Override
                        public void onSuccess() {
                            loginDialog.setCancelable(true);
                            loginDialogButton.setProgress(0);
                            loginDialogButton.setIdleText(getResourceString(R.string.login_complete));
                            // login successfully through user email////////////////////////////////////////////////////////////
                            SettingManager.getInstance().setTodayViewInfoShouldChange(true);
                            User loginUser = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class);
                            if (!CoCoinApplication.getAndroidId().equals(loginUser.getAndroidId())) {
                                // 2 users on one mobile////////////////////////////////////////////////////////////////////////////
                                showToast(7, "unique...");
                                return;
                            }
                            String userName = loginUser.getUsername();
                            SettingManager.getInstance().setLoggenOn(true);
                            SettingManager.getInstance().setUserName(userName);
                            SettingManager.getInstance().setUserEmail(loginUserName.getText().toString());
                            SettingManager.getInstance().setUserPassword(loginPassword.getText().toString());
                            updateViews();
                            // use a new method
                            //                                RecordManager.updateOldRecordsToServer();
                            whetherSyncSettingsFromServer();
                            showToast(6, userName);
                            final Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {

                                @Override
                                public void run() {
                                    if (loginDialog != null)
                                        loginDialog.dismiss();
                                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                    imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
                                }
                            }, 500);
                        }

                        // login fail through user name and email///////////////////////////////////////////////////////////
                        @Override
                        public void onFailure(int code, String msg) {
                            loginDialog.setCancelable(true);
                            loginDialogButton.setProgress(0);
                            String tip = getResourceString(R.string.network_disconnection);
                            if (msg.charAt(0) == 'u')
                                tip = getResourceString(R.string.user_name_or_password_incorrect);
                            if (msg.charAt(1) == 'n')
                                tip = getResourceString(R.string.user_mobile_exist);
                            loginDialogButton.setIdleText(tip);
                        }
                    });
                }
            });
        }
    });
    final MDButton positiveAction = loginDialog.getActionButton(DialogAction.POSITIVE);
    positiveAction.setEnabled(false);
    TextView userNameTV = (TextView) loginDialog.getCustomView().findViewById(R.id.login_user_name_text);
    TextView userPasswordTV = (TextView) loginDialog.getCustomView().findViewById(R.id.login_password_text);
    userNameTV.setTypeface(CoCoinUtil.GetTypeface());
    userPasswordTV.setTypeface(CoCoinUtil.GetTypeface());
    loginUserName = (MaterialEditText) loginDialog.getCustomView().findViewById(R.id.login_user_name);
    loginPassword = (MaterialEditText) loginDialog.getCustomView().findViewById(R.id.login_password);
    loginUserName.setTypeface(CoCoinUtil.GetTypeface());
    loginUserName.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            loginDialogButton.setEnabled(0 < loginUserName.getText().toString().length() && 0 < loginPassword.getText().toString().length());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    loginPassword.setTypeface(CoCoinUtil.GetTypeface());
    loginPassword.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            loginDialogButton.setEnabled(0 < loginUserName.getText().toString().length() && 0 < loginPassword.getText().toString().length());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    loginDialog.show();
}
Also used : SaveListener(cn.bmob.v3.listener.SaveListener) MDButton(com.afollestad.materialdialogs.internal.MDButton) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) BmobUser(cn.bmob.v3.BmobUser) User(com.nightonke.saver.model.User) Handler(android.os.Handler) InputMethodManager(android.view.inputmethod.InputMethodManager) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) RiseNumberTextView(com.nightonke.saver.ui.RiseNumberTextView) View(android.view.View) TextView(android.widget.TextView) MaterialIconView(net.steamcrafted.materialiconlib.MaterialIconView) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) RiseNumberTextView(com.nightonke.saver.ui.RiseNumberTextView) TextView(android.widget.TextView)

Example 5 with SaveListener

use of cn.bmob.v3.listener.SaveListener in project CoCoin by Nightonke.

the class HelpFeedbackFragment method onViewCreated.

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mScrollView = (ObservableScrollView) view.findViewById(R.id.scrollView);
    MaterialViewPagerHelper.registerScrollView(getActivity(), mScrollView, null);
    title = (TextView) view.findViewById(R.id.title);
    title.setTypeface(CoCoinUtil.getInstance().typefaceLatoLight);
    input = (EditText) view.findViewById(R.id.edittext);
    input.setTypeface(CoCoinUtil.getInstance().typefaceLatoLight);
    help = (TextView) view.findViewById(R.id.helper);
    help.setTypeface(CoCoinUtil.getInstance().typefaceLatoLight);
    number = (TextView) view.findViewById(R.id.number);
    number.setTypeface(CoCoinUtil.getInstance().typefaceLatoLight);
    send = (TextView) view.findViewById(R.id.send);
    send.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (exceed) {
                new MaterialDialog.Builder(mContext).title(R.string.help_feedback_dialog_title).content(R.string.help_feedback_dialog_content).positiveText(R.string.ok_1).show();
            } else {
                CoCoinUtil.getInstance().showToast(CoCoinApplication.getAppContext(), CoCoinApplication.getAppContext().getResources().getString(R.string.help_feedback_sent));
                Feedback feedback = new Feedback();
                feedback.setContent(input.getText().toString());
                feedback.save(CoCoinApplication.getAppContext(), new SaveListener() {

                    @Override
                    public void onSuccess() {
                        CoCoinUtil.getInstance().showToast(CoCoinApplication.getAppContext(), CoCoinApplication.getAppContext().getResources().getString(R.string.help_feedback_sent_successfully));
                    }

                    @Override
                    public void onFailure(int code, String arg0) {
                        CoCoinUtil.getInstance().showToast(CoCoinApplication.getAppContext(), CoCoinApplication.getAppContext().getResources().getString(R.string.help_feedback_sent_fail));
                    }
                });
            }
        }
    });
    input.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            setNumberText();
            try {
                ((OnTextChangeListener) activity).onTextChange(input.getText().toString(), exceed);
            } catch (ClassCastException cce) {
                cce.printStackTrace();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    send.requestFocus();
    setNumberText();
}
Also used : SaveListener(cn.bmob.v3.listener.SaveListener) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) TextView(android.widget.TextView) ObservableScrollView(com.github.ksoichiro.android.observablescrollview.ObservableScrollView) View(android.view.View) Feedback(com.nightonke.saver.model.Feedback) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable)

Aggregations

SaveListener (cn.bmob.v3.listener.SaveListener)6 BmobUser (cn.bmob.v3.BmobUser)5 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)4 User (com.nightonke.saver.model.User)4 Editable (android.text.Editable)3 TextWatcher (android.text.TextWatcher)3 View (android.view.View)3 TextView (android.widget.TextView)3 UpdateListener (cn.bmob.v3.listener.UpdateListener)3 Handler (android.os.Handler)2 InputMethodManager (android.view.inputmethod.InputMethodManager)2 BmobFile (cn.bmob.v3.datatype.BmobFile)2 MDButton (com.afollestad.materialdialogs.internal.MDButton)2 RiseNumberTextView (com.nightonke.saver.ui.RiseNumberTextView)2 CircleImageView (de.hdodenhof.circleimageview.CircleImageView)2 File (java.io.File)2 MaterialIconView (net.steamcrafted.materialiconlib.MaterialIconView)2 NonNull (android.support.annotation.NonNull)1 BmobQuery (cn.bmob.v3.BmobQuery)1 DeleteListener (cn.bmob.v3.listener.DeleteListener)1