// getInstance////////////////////////////////////////////////////////////////////////////////////// public static synchronized RecordManager getInstance(Context context) { if (RECORDS == null || TAGS == null || TAG_NAMES == null || SUM == null || recordManager == null) { SUM = 0; RECORDS = new LinkedList<>(); TAGS = new LinkedList<>(); TAG_NAMES = new HashMap<>(); recordManager = new RecordManager(context); db.getData(); if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) Log.d("CoCoin", "Load " + RECORDS.size() + " records S"); if (BuildConfig.DEBUG) Log.d("CoCoin", "Load " + TAGS.size() + " tags S"); } TAGS.add(0, new Tag(-1, "Sum Histogram", -4)); TAGS.add(0, new Tag(-2, "Sum Pie", -5)); for (Tag tag : TAGS) TAG_NAMES.put(tag.getId(), tag.getName()); sortTAGS(); } return recordManager; }
public static int deleteTag(int id) { int deletedId = -1; if (BuildConfig.DEBUG) Log.d( "CoCoin", "Manager: Delete tag: " + "Tag(id = " + id + ", deletedId = " + deletedId + ")"); boolean tagReference = false; for (CoCoinRecord coCoinRecord : RECORDS) { if (coCoinRecord.getTag() == id) { tagReference = true; break; } } if (tagReference) { return DELETE_TAG_ERROR_TAG_REFERENCE; } deletedId = db.deleteTag(id); if (deletedId == -1) { if (BuildConfig.DEBUG) Log.d("CoCoin", "Delete the above tag FAIL!"); return DELETE_TAG_ERROR_DATABASE_ERROR; } else { if (BuildConfig.DEBUG) Log.d("CoCoin", "Delete the above tag SUCCESSFULLY!"); for (Tag tag : TAGS) { if (tag.getId() == deletedId) { TAGS.remove(tag); break; } } TAG_NAMES.remove(id); sortTAGS(); } return deletedId; }
// constructor////////////////////////////////////////////////////////////////////////////////////// private RecordManager(Context context) { try { db = db.getInstance(context); if (BuildConfig.DEBUG) if (BuildConfig.DEBUG) Log.d("CoCoin", "db.getInstance(context) S"); } catch (IOException e) { e.printStackTrace(); } if (FIRST_TIME) { // if the app starts firstly, create // tags/////////////////////////////////////////////////////////// SharedPreferences preferences = context.getSharedPreferences("Values", Context.MODE_PRIVATE); if (preferences.getBoolean("FIRST_TIME", true)) { createTags(); SharedPreferences.Editor editor = context.getSharedPreferences("Values", Context.MODE_PRIVATE).edit(); editor.putBoolean("FIRST_TIME", false); editor.commit(); } } if (RANDOM_DATA) { SharedPreferences preferences = context.getSharedPreferences("Values", Context.MODE_PRIVATE); if (preferences.getBoolean("RANDOM", false)) { return; } randomDataCreater(); SharedPreferences.Editor editor = context.getSharedPreferences("Values", Context.MODE_PRIVATE).edit(); editor.putBoolean("RANDOM", true); editor.commit(); } }
// save // tag///////////////////////////////////////////////////////////////////////////////////////// public static int saveTag(Tag tag) { int insertId = -1; if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) Log.d("CoCoin", "recordManager.saveTag: " + tag.toString()); } boolean duplicatedName = false; for (Tag t : TAGS) { if (t.getName().equals(tag.getName())) { duplicatedName = true; break; } } if (duplicatedName) { return SAVE_TAG_ERROR_DUPLICATED_NAME; } insertId = db.saveTag(tag); if (insertId == -1) { if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) Log.d("CoCoin", "Save the above tag FAIL!"); return SAVE_TAG_ERROR_DATABASE_ERROR; } } else { if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) Log.d("CoCoin", "Save the above tag SUCCESSFULLY!"); } TAGS.add(tag); TAG_NAMES.put(tag.getId(), tag.getName()); sortTAGS(); } return insertId; }
// saveRecord/////////////////////////////////////////////////////////////////////////////////////// public static long saveRecord(final CoCoinRecord coCoinRecord) { long insertId = -1; // this is a new coCoinRecord, which is not uploaded coCoinRecord.setIsUploaded(false); // User user = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class); // if (user != null) coCoinRecord.setUserId(user.getObjectId()); // else coCoinRecord.setUserId(null); if (BuildConfig.DEBUG) if (BuildConfig.DEBUG) Log.d("CoCoin", "recordManager.saveRecord: Save " + coCoinRecord.toString() + " S"); insertId = db.saveRecord(coCoinRecord); if (insertId == -1) { if (BuildConfig.DEBUG) if (BuildConfig.DEBUG) Log.d("CoCoin", "recordManager.saveRecord: Save the above coCoinRecord FAIL!"); CoCoinToast.getInstance().showToast(R.string.save_failed_locale, SuperToast.Background.RED); } else { if (BuildConfig.DEBUG) if (BuildConfig.DEBUG) Log.d("CoCoin", "recordManager.saveRecord: Save the above coCoinRecord SUCCESSFULLY!"); RECORDS.add(coCoinRecord); SUM += (int) coCoinRecord.getMoney(); // if (user != null) { // // already login // coCoinRecord.save(CoCoinApplication.getAppContext(), new SaveListener() { // @Override // public void onSuccess() { // if (BuildConfig.DEBUG) // if (BuildConfig.DEBUG) Log.d("CoCoin", // "recordManager.saveRecord: Save online " + coCoinRecord.toString() + " S"); // coCoinRecord.setIsUploaded(true); // coCoinRecord.setLocalObjectId(coCoinRecord.getObjectId()); // db.updateRecord(coCoinRecord); // CoCoinToast.getInstance() // .showToast(R.string.save_successfully_online, // SuperToast.Background.BLUE); // } // @Override // public void onFailure(int code, String msg) { // if (BuildConfig.DEBUG) // if (BuildConfig.DEBUG) Log.d("CoCoin", // "recordManager.saveRecord: Save online " + coCoinRecord.toString() + " F"); // if (BuildConfig.DEBUG) // if (BuildConfig.DEBUG) Log.d("CoCoin", // "recordManager.saveRecord: Save online msg: " + msg + " code " + code); // CoCoinToast.getInstance() // .showToast(R.string.save_failed_online, // SuperToast.Background.RED); // } // }); // } else { // CoCoinToast.getInstance() // .showToast(R.string.save_successfully_locale, // SuperToast.Background.BLUE); // } CoCoinToast.getInstance() .showToast(R.string.save_successfully_locale, SuperToast.Background.BLUE); } return insertId; }
public static long updateTag(Tag tag) { int updateId = -1; if (BuildConfig.DEBUG) Log.d("CoCoin", "Manager: Update tag: " + tag.toString()); updateId = db.updateTag(tag); if (updateId == -1) { if (BuildConfig.DEBUG) Log.d("CoCoin", "Update the above tag FAIL!"); } else { if (BuildConfig.DEBUG) Log.d("CoCoin", "Update the above tag SUCCESSFULLY!" + " - " + updateId); for (Tag t : TAGS) { if (t.getId() == tag.getId()) { t.set(tag); break; } } sortTAGS(); } return updateId; }
public static long updateRecord(final CoCoinRecord coCoinRecord) { long updateNumber = db.updateRecord(coCoinRecord); if (updateNumber <= 0) { if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) Log.d("CoCoin", "recordManager.updateRecord " + coCoinRecord.toString() + " F"); } CoCoinToast.getInstance().showToast(R.string.update_failed_locale, SuperToast.Background.RED); } else { if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) Log.d("CoCoin", "recordManager.updateRecord " + coCoinRecord.toString() + " S"); } p = RECORDS.size() - 1; for (; p >= 0; p--) { if (RECORDS.get(p).getId() == coCoinRecord.getId()) { SUM -= (int) RECORDS.get(p).getMoney(); SUM += (int) coCoinRecord.getMoney(); RECORDS.get(p).set(coCoinRecord); break; } } coCoinRecord.setIsUploaded(false); // User user = BmobUser // .getCurrentUser(CoCoinApplication.getAppContext(), User.class); // if (user != null) { // // already login // if (coCoinRecord.getLocalObjectId() != null) { // // this coCoinRecord has been push to the server // coCoinRecord.setUserId(user.getObjectId()); // coCoinRecord.update(CoCoinApplication.getAppContext(), // coCoinRecord.getLocalObjectId(), new UpdateListener() { // @Override // public void onSuccess() { // if (BuildConfig.DEBUG) { // if (BuildConfig.DEBUG) Log.d("CoCoin", // "recordManager.updateRecord update online " + coCoinRecord.toString() + " S"); // } // coCoinRecord.setIsUploaded(true); // RECORDS.get(p).setIsUploaded(true); // db.updateRecord(coCoinRecord); // // CoCoinToast.getInstance().showToast(R.string.update_successfully_online, // SuperToast.Background.BLUE); // } // // @Override // public void onFailure(int code, String msg) { // if (BuildConfig.DEBUG) { // if (BuildConfig.DEBUG) Log.d("CoCoin", // "recordManager.updateRecord update online " + coCoinRecord.toString() + " F"); // } // if (BuildConfig.DEBUG) { // if (BuildConfig.DEBUG) Log.d("CoCoin", // "recordManager.updateRecord update online code" + code + " msg " + msg ); // } // // CoCoinToast.getInstance().showToast(R.string.update_failed_online, // SuperToast.Background.RED); // } // }); // } else { // // this coCoinRecord has not been push to the server // coCoinRecord.setUserId(user.getObjectId()); // coCoinRecord.save(CoCoinApplication.getAppContext(), new SaveListener() // { // @Override // public void onSuccess() { // if (BuildConfig.DEBUG) { // if (BuildConfig.DEBUG) Log.d("CoCoin", // "recordManager.updateRecord save online " + coCoinRecord.toString() + " S"); // } // coCoinRecord.setIsUploaded(true); // // coCoinRecord.setLocalObjectId(coCoinRecord.getObjectId()); // RECORDS.get(p).setIsUploaded(true); // // RECORDS.get(p).setLocalObjectId(coCoinRecord.getObjectId()); // db.updateRecord(coCoinRecord); // // CoCoinToast.getInstance().showToast(R.string.update_successfully_online, // SuperToast.Background.BLUE); // } // @Override // public void onFailure(int code, String msg) { // if (BuildConfig.DEBUG) { // if (BuildConfig.DEBUG) Log.d("CoCoin", // "recordManager.updateRecord save online " + coCoinRecord.toString() + " F"); // } // if (BuildConfig.DEBUG) { // if (BuildConfig.DEBUG) Log.d("CoCoin", // "recordManager.updateRecord save online code" + code + " msg " + msg ); // } // // CoCoinToast.getInstance().showToast(R.string.update_failed_online, // SuperToast.Background.RED); // } // }); // } // } else { // // has not login // db.updateRecord(coCoinRecord); // CoCoinToast.getInstance().showToast(R.string.update_successfully_locale, // SuperToast.Background.BLUE); // } db.updateRecord(coCoinRecord); CoCoinToast.getInstance() .showToast(R.string.update_successfully_locale, SuperToast.Background.BLUE); } return updateNumber; }
// 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(); }