Search in sources :

Example 1 with DeleteListener

use of cn.bmob.v3.listener.DeleteListener 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 2 with DeleteListener

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

the class RecordManager method deleteRecord.

// delete a coCoinRecord//////////////////////////////////////////////////////////////////////////////////
public static long deleteRecord(final CoCoinRecord coCoinRecord, boolean deleteInList) {
    long deletedNumber = db.deleteRecord(coCoinRecord.getId());
    if (deletedNumber > 0) {
        if (BuildConfig.DEBUG)
            Log.d("CoCoin", "recordManager.deleteRecord: Delete " + coCoinRecord.toString() + " S");
        User user = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class);
        // if we can delete the coCoinRecord from server
        //            if (user != null && coCoinRecord.getLocalObjectId() != null) {
        //                coCoinRecord.delete(CoCoinApplication.getAppContext(), new DeleteListener() {
        //                    @Override
        //                    public void onSuccess() {
        //                        if (BuildConfig.DEBUG) {
        //                            if (BuildConfig.DEBUG) Log.d("CoCoin",
        //                                    "recordManager.deleteRecord: Delete online " + coCoinRecord.toString() + " S");
        //                        }
        //                        CoCoinToast.getInstance()
        //                                .showToast(R.string.delete_successfully_online, SuperToast.Background.BLUE);
        //                    }
        //                    @Override
        //                    public void onFailure(int code, String msg) {
        //                        if (BuildConfig.DEBUG) {
        //                            if (BuildConfig.DEBUG) Log.d("CoCoin",
        //                                    "recordManager.deleteRecord: Delete online " + coCoinRecord.toString() + " F");
        //                        }
        //                        CoCoinToast.getInstance()
        //                                .showToast(R.string.delete_failed_online, SuperToast.Background.RED);
        //                    }
        //                });
        //            } else {
        //                CoCoinToast.getInstance()
        //                        .showToast(R.string.delete_successfully_locale, SuperToast.Background.BLUE);
        //            }
        CoCoinToast.getInstance().showToast(R.string.delete_successfully_locale, SuperToast.Background.BLUE);
        // update RECORDS list and SUM
        SUM -= (int) coCoinRecord.getMoney();
        if (deleteInList) {
            int size = RECORDS.size();
            for (int i = 0; i < RECORDS.size(); i++) {
                if (RECORDS.get(i).getId() == coCoinRecord.getId()) {
                    RECORDS.remove(i);
                    if (BuildConfig.DEBUG)
                        Log.d("CoCoin", "recordManager.deleteRecord: Delete in RECORD " + coCoinRecord.toString() + " S");
                    break;
                }
            }
        }
    } else {
        if (BuildConfig.DEBUG)
            Log.d("CoCoin", "recordManager.deleteRecord: Delete " + coCoinRecord.toString() + " F");
        CoCoinToast.getInstance().showToast(R.string.delete_failed_locale, SuperToast.Background.RED);
    }
    return coCoinRecord.getId();
}
Also used : BmobUser(cn.bmob.v3.BmobUser)

Aggregations

BmobUser (cn.bmob.v3.BmobUser)2 BmobQuery (cn.bmob.v3.BmobQuery)1 BmobFile (cn.bmob.v3.datatype.BmobFile)1 DeleteListener (cn.bmob.v3.listener.DeleteListener)1 SaveListener (cn.bmob.v3.listener.SaveListener)1 UpdateListener (cn.bmob.v3.listener.UpdateListener)1 UploadFileListener (cn.bmob.v3.listener.UploadFileListener)1 Logo (com.nightonke.saver.model.Logo)1 User (com.nightonke.saver.model.User)1 File (java.io.File)1