Example #1
0
 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;
 }
Example #2
0
 // 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;
 }
Example #3
0
 public static List<CoCoinRecord> queryRecordByRemark(String remark) {
   List<CoCoinRecord> list = new LinkedList<>();
   for (CoCoinRecord coCoinRecord : RECORDS) {
     if (CoCoinUtil.IsStringRelation(coCoinRecord.getRemark(), remark)) {
       list.add(coCoinRecord);
     }
   }
   return list;
 }
Example #4
0
 public static List<CoCoinRecord> queryRecordByTag(int tag) {
   List<CoCoinRecord> list = new LinkedList<>();
   for (CoCoinRecord coCoinRecord : RECORDS) {
     if (coCoinRecord.getTag() == tag) {
       list.add(coCoinRecord);
     }
   }
   return list;
 }
Example #5
0
 public static List<CoCoinRecord> queryRecordByCurrency(String currency) {
   List<CoCoinRecord> list = new LinkedList<>();
   for (CoCoinRecord coCoinRecord : RECORDS) {
     if (coCoinRecord.getCurrency().equals(currency)) {
       list.add(coCoinRecord);
     }
   }
   return list;
 }
Example #6
0
 public static List<CoCoinRecord> queryRecordByTime(Calendar c1, Calendar c2) {
   List<CoCoinRecord> list = new LinkedList<>();
   for (CoCoinRecord coCoinRecord : RECORDS) {
     if (coCoinRecord.isInTime(c1, c2)) {
       list.add(coCoinRecord);
     }
   }
   return list;
 }
Example #7
0
 public static List<CoCoinRecord> queryRecordByMoney(
     double money1, double money2, String currency) {
   List<CoCoinRecord> list = new LinkedList<>();
   for (CoCoinRecord coCoinRecord : RECORDS) {
     if (coCoinRecord.isInMoney(money1, money2, currency)) {
       list.add(coCoinRecord);
     }
   }
   return list;
 }
Example #8
0
  private void randomDataCreater() {

    Random random = new Random();

    List<CoCoinRecord> createdCoCoinRecords = new ArrayList<>();

    Calendar now = Calendar.getInstance();
    Calendar c = Calendar.getInstance();
    c.set(2015, 0, 1, 0, 0, 0);
    c.add(Calendar.SECOND, 1);

    while (c.before(now)) {
      for (int i = 0; i < RANDOM_DATA_NUMBER_ON_EACH_DAY; i++) {
        Calendar r = (Calendar) c.clone();
        int hour = random.nextInt(24);
        int minute = random.nextInt(60);
        int second = random.nextInt(60);

        r.set(Calendar.HOUR_OF_DAY, hour);
        r.set(Calendar.MINUTE, minute);
        r.set(Calendar.SECOND, second);
        r.add(Calendar.SECOND, 0);

        int tag = random.nextInt(TAGS.size());
        int expense = random.nextInt(RANDOM_DATA_EXPENSE_ON_EACH_DAY) + 1;

        CoCoinRecord coCoinRecord = new CoCoinRecord();
        coCoinRecord.setCalendar(r);
        coCoinRecord.setMoney(expense);
        coCoinRecord.setTag(tag);
        coCoinRecord.setCurrency("RMB");
        coCoinRecord.setRemark("备注:这里显示备注~");

        createdCoCoinRecords.add(coCoinRecord);
      }
      c.add(Calendar.DATE, 1);
    }

    Collections.sort(
        createdCoCoinRecords,
        new Comparator<CoCoinRecord>() {
          @Override
          public int compare(CoCoinRecord lhs, CoCoinRecord rhs) {
            if (lhs.getCalendar().before(rhs.getCalendar())) {
              return -1;
            } else if (lhs.getCalendar().after(rhs.getCalendar())) {
              return 1;
            } else {
              return 0;
            }
          }
        });

    for (CoCoinRecord coCoinRecord : createdCoCoinRecords) {
      saveRecord(coCoinRecord);
    }
  }
Example #9
0
  public static long updateOldRecordsToServer() {
    long counter = 0;
    User user = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class);
    if (user != null) {
      // already
      // login////////////////////////////////////////////////////////////////////////////////////
      isLastOne = false;
      for (int i = 0; i < RECORDS.size(); i++) {
        if (i == RECORDS.size() - 1) isLastOne = true;
        final CoCoinRecord coCoinRecord = RECORDS.get(i);
        if (!coCoinRecord.getIsUploaded()) {
          // has been
          // changed/////////////////////////////////////////////////////////////////////////////////
          if (coCoinRecord.getLocalObjectId() != null) {
            // there is an old coCoinRecord in server, we should update this
            // coCoinRecord///////////////////////////////////
            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.updateOldRecordsToServer update online "
                                + coCoinRecord.toString()
                                + " S");
                    }
                    coCoinRecord.setIsUploaded(true);
                    coCoinRecord.setLocalObjectId(coCoinRecord.getObjectId());
                    db.updateRecord(coCoinRecord);
                    // after updating, get the old records from
                    // server//////////////////////////////////////////////////
                    if (isLastOne) getRecordsFromServer();
                  }

                  @Override
                  public void onFailure(int code, String msg) {
                    if (BuildConfig.DEBUG) {
                      if (BuildConfig.DEBUG)
                        Log.d(
                            "CoCoin",
                            "recordManager.updateOldRecordsToServer update online "
                                + coCoinRecord.toString()
                                + " F");
                    }
                    if (BuildConfig.DEBUG) {
                      if (BuildConfig.DEBUG)
                        Log.d(
                            "CoCoin",
                            "recordManager.updateOldRecordsToServer update online code"
                                + code
                                + " msg "
                                + msg);
                    }
                  }
                });
          } else {
            counter++;
            coCoinRecord.setUserId(user.getObjectId());
            coCoinRecord.save(
                CoCoinApplication.getAppContext(),
                new SaveListener() {
                  @Override
                  public void onSuccess() {
                    if (BuildConfig.DEBUG) {
                      if (BuildConfig.DEBUG)
                        Log.d(
                            "CoCoin",
                            "recordManager.updateOldRecordsToServer save online "
                                + coCoinRecord.toString()
                                + " S");
                    }
                    coCoinRecord.setIsUploaded(true);
                    coCoinRecord.setLocalObjectId(coCoinRecord.getObjectId());
                    db.updateRecord(coCoinRecord);
                    // after updating, get the old records from
                    // server//////////////////////////////////////////////////
                    if (isLastOne) getRecordsFromServer();
                  }

                  @Override
                  public void onFailure(int code, String msg) {
                    if (BuildConfig.DEBUG) {
                      if (BuildConfig.DEBUG)
                        Log.d(
                            "CoCoin",
                            "recordManager.updateOldRecordsToServer save online "
                                + coCoinRecord.toString()
                                + " F");
                    }
                    if (BuildConfig.DEBUG) {
                      if (BuildConfig.DEBUG)
                        Log.d(
                            "CoCoin",
                            "recordManager.updateOldRecordsToServer save online code"
                                + code
                                + " msg "
                                + msg);
                    }
                  }
                });
          }
        }
      }
    } else {

    }

    if (BuildConfig.DEBUG) {
      if (BuildConfig.DEBUG)
        Log.d(
            "CoCoin",
            "recordManager.updateOldRecordsToServer update " + counter + " records to server.");
    }

    if (RECORDS.size() == 0) getRecordsFromServer();

    return counter;
  }
Example #10
0
 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;
 }
Example #11
0
  // 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();
  }