/** 初始化本类 */
 private void initData() {
   currentBaby = App.getCurrentBaby();
   if (currentBaby == null) {
     ACache aCache = ACache.get(App.getAppContext());
     String json = aCache.getAsString(Baby.CACHE_KEY);
     Log.i("baby currentbaby json", json);
     currentBaby = JSON.parseObject(json, Baby.class);
   }
   if (babyInfos == null) babyInfos = getAllBabyInfoCache();
   months2Weights();
 }
 /** 同步的与云端同步,并更新weighthelper里面数据 */
 public void sync() {
   BabyInfoDao babyInfoDao = new BabyInfoDao();
   List<BabyInfo> babyInfosCloud = babyInfoDao.findByBabyFromCloud(currentBaby);
   if (babyInfosCloud == null || babyInfosCloud.isEmpty()) return;
   if (equals(currentBabyInfo, babyInfosCloud.get(0))) {
     return;
   }
   for (BabyInfo babyInfo : babyInfosCloud) {
     babyInfo.saveInCache(App.getAppContext());
   }
   initData();
 }
 /**
  * 存入一个babyinfo,先存网络,如果成功在存本地,本地也成功,则更新本类的babyinfos,更新一下monthtoweights 并更新currentbabyinfo
  *
  * @param babyInfo 要存入的babyinfo
  */
 public void saveOneWeight(final BabyInfo babyInfo) {
   babyInfo.setBaby(App.getCurrentBaby());
   babyInfo.saveInCloud(
       new SaveCallback() {
         @Override
         public void done(AVException e) {
           if (e != null) {
             e.printStackTrace();
             Log.i(TAG, "babyinfo incloud failed");
             return;
           }
           Log.i(TAG, "babyinfo incloud success");
           babyInfo.saveInCache(
               App.getAppContext(),
               new LocalSaveTask.LocalSaveCallback() {
                 @Override
                 public void done() {
                   babyInfos.add(babyInfo);
                   if (babyInfos.isEmpty()) babyInfos.add(babyInfo);
                   else {
                     BabyInfo tempBabyinfo = babyInfos.get(babyInfos.size() - 1);
                     if (tempBabyinfo.getAge().equals(babyInfo.getAge())) {
                       babyInfos.remove(babyInfos.size() - 1);
                       babyInfos.add(babyInfo);
                     } else {
                       babyInfos.add(babyInfo);
                     }
                   }
                   addOneWeight(babyInfo);
                   String currentDate = currentBabyInfo.getAge();
                   String today = babyInfo.getAge();
                   Date current = null, now = null;
                   try {
                     current = Standar.DATE_FORMAT.parse(currentDate);
                   } catch (ParseException e) {
                     e.printStackTrace();
                     return;
                   }
                   try {
                     now = Standar.DATE_FORMAT.parse(today);
                   } catch (ParseException e) {
                     e.printStackTrace();
                     return;
                   }
                   if (current.compareTo(now) <= 0) {
                     currentBabyInfo = babyInfo;
                   }
                 }
               });
         }
       });
 }
Beispiel #4
0
  private void drawExcle() {
    Context context = App.getAppContext();
    LinearLayout.LayoutParams temperatureParams =
        new LinearLayout.LayoutParams(mWidth, LinearLayout.LayoutParams.MATCH_PARENT);
    LinearLayout.LayoutParams amountParams =
        new LinearLayout.LayoutParams(mWidth, LinearLayout.LayoutParams.MATCH_PARENT);
    LinearLayout temperatureContainer =
        (LinearLayout) layout.findViewById(R.id.temperature_excle_container);
    LinearLayout amountContainer = (LinearLayout) layout.findViewById(R.id.amount_excle_container);

    refreshExcleData(temperatureData, amountData);

    temperatureExcle = new com.inhand.milk.fragment.milk_amount_curve.Excle(mContext);
    temperatureContainer.addView(temperatureExcle, temperatureParams);
    temperatureExcle.setRange(temperatureMax, temperatureMin);
    temperatureExcle.addLine(temperatureData);
    temperatureExcle.setTextColor(
        context
            .getResources()
            .getColor(R.color.milk_amount_curve_temperature_excle_title_text_color));
    temperatureExcle.setFirstBgColor(
        context
            .getResources()
            .getColor(R.color.milk_amount_curve_temperature_excle_first_bg_color));
    temperatureExcle.setSecondBgColor(
        context
            .getResources()
            .getColor(R.color.milk_amount_curve_temperature_excle_seconde_bg_color));
    temperatureExcle.setLineColor(
        context.getResources().getColor(R.color.milk_amount_curve_temperature_excle_line_color));
    setTemperatureExcle(temperatureExcle);

    amountExcle = new com.inhand.milk.fragment.milk_amount_curve.Excle(mContext);
    amountContainer.addView(amountExcle, amountParams);

    amountExcle.setRange(amountMax, amountMin);
    amountExcle.addLine(amountData);
    amountExcle.setTextColor(
        context.getResources().getColor(R.color.milk_amount_curve_amount_excle_title_text_color));
    amountExcle.setFirstBgColor(
        context.getResources().getColor(R.color.milk_amount_curve_amount_excle_first_bg_color));
    amountExcle.setSecondBgColor(
        context.getResources().getColor(R.color.milk_amount_curve_amount_excle_seconde_bg_color));
    amountExcle.setLineColor(
        context.getResources().getColor(R.color.milk_amount_curve_amount_excle_line_color));
    setAmountExcle(amountExcle);
  }
  /**
   * 从本地同步获取所有的babyinfo缓存,并更新currentbabyinfo
   *
   * @return 返回一个babyinfo的list;
   */
  private List<BabyInfo> getAllBabyInfoCache() {
    BabyInfoDao babyInfoDao = new BabyInfoDao();
    if (currentBaby == null) Log.i("baby current", "null");
    Date birth = currentBaby.getCreatedAt();
    if (birth == null) Log.i("baby birth", "null");

    /*
    String birthday = currentBaby.getBirthday();
    Date birth = null;
    try {
        birth = Standar.DATE_FORMAT.parse(birthday);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    */
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(birth);
    int birthYear = calendar.get(Calendar.YEAR);
    int birthMonth = calendar.get(Calendar.MONTH) + 1;
    Date date = new Date();
    calendar.setTime(date);
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH) + 1;
    List<BabyInfo> result = new ArrayList<>();
    for (int i = birthYear; i <= year; i++) {
      for (int j = birthMonth; j <= month; j++) {
        String time;
        if (j >= 10) time = String.valueOf(i) + "-" + String.valueOf(j);
        else time = String.valueOf(i) + "-0" + String.valueOf(j);
        Log.d("baby gettime", time);
        List<BabyInfo> tempBabyInfos = babyInfoDao.findByDateFromCache(App.getAppContext(), time);
        if (tempBabyInfos == null) continue;
        result.addAll(tempBabyInfos);
        int count = tempBabyInfos.size();
        if (count <= 0) continue;
        currentBabyInfo = tempBabyInfos.get(count - 1);
        Log.d("baby currentbabyinfo", "not null");
      }
    }

    return result;
  }