Exemplo n.º 1
0
  public static void getLastIntake(Medicine medicine) {
    String recurrenceRule = medicine.getRecurrenceRule();
    String intakesRule = medicine.getSchedule();

    if (!TextUtils.isEmpty(recurrenceRule) && !TextUtils.isEmpty(intakesRule)) {
      Time dtStart = TimeUtils.getTimeDateFromString(medicine.getStartDate());
      List<IntakeHelper> intakes = IntakeUtils.parseDailyIntakes(intakesRule);

      long[] dates;
      int length;
      LocalDate date;
      LocalTime time;

      int durationType = getDurationType(dtStart, recurrenceRule);

      if (durationType == END_NEVER) {
        medicine.setEndDate(-1);
      } else {
        dates = expand(dtStart, recurrenceRule, dtStart.toMillis(false), -1);
        length = dates.length;
        if (length > 0) {
          if (durationType == END_BY_DATE) {
            date = TimeUtils.getDateFromMillis(dates[length - 1]);
            time = intakes.get(intakes.size() - 1).getTime();
          } else {
            // durationType == END_BY_COUNT
            int dailyIntakes = intakes.size();
            if (length % dailyIntakes == 0) {
              date = TimeUtils.getDateFromMillis(dates[(length / dailyIntakes) - 1]);
              time = intakes.get(intakes.size() - 1).getTime();
            } else {
              date = TimeUtils.getDateFromMillis(dates[length / dailyIntakes]);
              time = intakes.get((length % dailyIntakes) - 1).getTime();
            }
          }
          medicine.setEndDate(TimeUtils.getMillis(date, time));
        } else {
          medicine.setEndDate(0);
        }
      }
    }
  }