Example #1
0
        public static List<DateTime> GetNextDueDate(String repeatFlag, DateTime dueDate, String repeatFrom, DateTime completedTime, String timeZone, int limit)
        {
            List<DateTime> dueDates = new List<DateTime>();
            if (string.IsNullOrEmpty(repeatFlag) || dueDate == null)
            {
                return dueDates;
            }
            bool isLunar;
            try
            {
                TickRRule rRule = new TickRRule(repeatFlag);
                isLunar = rRule.IsLunarFrequency();
                if (IsRepeatFromCompleteTime(repeatFrom, completedTime))
                {
                    // update rRule, We must clear byDay and byMonthDay when the
                    // task repeat from completedTime.
                    rRule.SetByDay(new List<IWeekDay>());
                    rRule.SetByMonthDay(new int[0]);
                    repeatFlag = rRule.ToTickTickIcal();
                }
                if (rRule.GetCompletedRepeatCount() >= rRule.GetCount())
                {
                    return dueDates;
                }

                if (string.IsNullOrEmpty(timeZone))
                {
                    timeZone = NodaTime.DateTimeZoneProviders.Tzdb.GetSystemDefault().Id;
                }
                DateTimeZone taskTimeZone = DateTimeZoneProviders.Tzdb.GetZoneOrNull(timeZone); //.getTimeZone(timeZone);

                // TODO 有问题 阳历
                //GregorianCalendar taskCal = new GregorianCalendar(taskTimeZone);
                //GregorianCalendar utcCal = new GregorianCalendar(TimeUtils.utcTimezone());
                Calendar taskCal = new Calendar();
                taskCal.ChangeTimeZone(taskTimeZone.Id);
                Calendar utcCal = new Calendar();
                utcCal.ChangeTimeZone(DateTimeZoneProviders.Tzdb.GetSystemDefault().Id);

                DateTime taskStart = InitRepeatStartDate(repeatFrom, completedTime, dueDate, taskCal);
                DateTime utcStart = ConvertDateToUTCDate(taskStart, taskCal, utcCal);
                long untilDateTime = GetTimeFromDateValue(rRule.GetUntil());
                if (isLunar)
                {
                    //DateTime now = DateTime.UtcNow; // TODO 为什么要这么写?   DateTimeUtils.GetCurrentDate();
                    ////当前时间已经超过截止重复时间,必定没有下个有效的重复时间
                    //if (isAfterUntilDate(now, untilDateTime))
                    //{
                    //    return dueDates;
                    //}
                    //GregorianCalendar start = new GregorianCalendar();
                    //start.setTime(dueDate);
                    //Date next = getNextLunarDueDate(start, rRule);
                    //int i = 0;
                    //while (next != null && dueDates.size() < limit && !isAfterUntilDate(next,
                    //        untilDateTime))
                    //{
                    //    start.setTime(next);
                    //    next = getNextLunarDueDate(start, rRule);
                    //    if (next != null && !now.after(next))
                    //    {
                    //        dueDates.add(next);
                    //    }
                    //    if (i++ > 1000)
                    //    {
                    //        break;
                    //    }
                    //}
                    //if (dueDates.isEmpty())
                    //{
                    //    Log.e(TAG, "Get next due_date error: repeatFlag = " + repeatFlag);
                    //}
                    //return dueDates;
                }
                String rRuleString = rRule.ToIcal();
                if (untilDateTime > 0)
                {
                    rRuleString = RemoveKeyValueFromRRule(TickRRule.UNTIL_KEY, rRuleString);
                }
                // TODO 这是干嘛的。。。
                //DateIterator di = DateIteratorFactory.createDateIterator(rRuleString, utcStart, TimeUtils.utcTimezone(), true);
                if (Constants.RepeatFromStatus.DEFAULT.Equals(repeatFrom)
                        || string.IsNullOrEmpty(repeatFrom))
                {
                    // Repeat from default, next dueDate 不会返回小于Now
                    DateTime utcNow = ConvertDateToUTCDate(DateTime.UtcNow, taskCal, utcCal);//(DateTimeUtils.getCurrentDate(), taskCal, utcCal);
                    // TODO advanceTo 是什么,比什么早?
                    //if (utcNow > utcStart)
                    //{
                    //    di.advanceTo(utcNow);
                    //}
                }
                DateTime next;
                int count = 0;
                //while (di.hasNext() && dueDates.Count < limit)
                //{
                //    next = ConvertUtcDateToDate(di.next(), taskCal, utcCal);
                //    if (next > taskStart)
                //    {
                //        //得到的下次重复时间已超过截止重复时间,无效
                //        if (IsAfterUntilDate(next, untilDateTime))
                //        {
                //            return dueDates;
                //        }
                //        else
                //        {
                //            dueDates.Add(next);
                //        }
                //    }
                //    if (count++ > 10000)
                //    {
                //        // 对极端的情况跳出来,不要循环了。
                //        break;
                //    }
                //}
            }
            catch (Exception e)
            {
                //Log.e(TAG, "Get next due_date error: repeatFlag = " + repeatFlag, e);
            }
            return dueDates;

        }
        /// <summary>
        /// 改变重复时间
        /// </summary>
        /// <param name="repeatTimeEnum"></param>
        public void ChangeRepeatTime(int repeatTimeEnum)
        {
            if (repeatTimeEnum.Equals(TaskRepeatItemEnum.DOES_NOT_REPEAT))
            {
                return;
            }
            TickRRule rule = new TickRRule();
            //if (rule == null)
            //{
            //    rule = new TickRRule();
            //    rule.SetInterval(1);
            //}
            var taskDate = DateTime.UtcNow;
            List<IWeekDay> weekdayNums = new List<IWeekDay>();

            switch (repeatTimeEnum)
            {
                case TaskRepeatItemEnum.DOES_NOT_REPEAT:
                    rule = null;
                    //mApplication.getAnalyticsInstance().sendRepeatSetEvent(
                    //        AnalyticsConstants.DateSetupParams.LABEL_REPEAT_NO);
                    //if (!repeatArray.isEmpty())
                    //{
                    //    if (repeatArray.get(repeatArray.size() - 1).type
                    //            == RepeatSettingsItem.ITEM_TYPE_END_REPEAT)
                    //    {
                    //        repeatArray.get(repeatArray.size() - 1).summary = "";
                    //    }
                    //    if (repeatArray.get(repeatArray.size() - 2).type
                    //            == RepeatSettingsItem.ITEM_TYPE_CUSTOM)
                    //    {
                    //        repeatArray.get(repeatArray.size() - 2).summary = "";
                    //    }
                    //}
                    //mSettingsAdapter.notifyDataSetChanged();
                    break;
                case TaskRepeatItemEnum.REPEATS_DAILY:
                    //mApplication.getAnalyticsInstance().sendRepeatSetEvent(
                    //        AnalyticsConstants.DateSetupParams.LABEL_REPEAT_DAILY);
                    rule = new TickRRule("RRULE:FREQ=DAILY;INTERVAL=1");
                    rule.SetInterval(1);
                    rule.SetFreq(FrequencyType.Daily);
                    break;
                case TaskRepeatItemEnum.REPEATS_EVERY_WEEKDAY:
                    //mApplication.getAnalyticsInstance().sendRepeatSetEvent(
                    //        AnalyticsConstants.DateSetupParams.LABEL_REPEAT_WEEKDAY);
                    rule = new TickRRule();
                    rule.SetInterval(1);
                    rule.SetFreq(FrequencyType.Weekly);
                    weekdayNums.Clear();
                    weekdayNums.Add(new WeekDay(DayOfWeek.Monday, 0));
                    weekdayNums.Add(new WeekDay(DayOfWeek.Tuesday, 0));
                    weekdayNums.Add(new WeekDay(DayOfWeek.Wednesday, 0));
                    weekdayNums.Add(new WeekDay(DayOfWeek.Thursday, 0));
                    weekdayNums.Add(new WeekDay(DayOfWeek.Friday, 0));
                    rule.SetByDay(weekdayNums);
                    break;
                case TaskRepeatItemEnum.REPEATS_WEEKLY_ON_DAY:
                    //mApplication.getAnalyticsInstance().sendRepeatSetEvent(
                    //        AnalyticsConstants.DateSetupParams.LABEL_REPEAT_WEEKLY);
                    rule = new TickRRule();
                    rule.SetInterval(1);
                    rule.SetFreq(FrequencyType.Weekly);
                    weekdayNums.Clear();
                    weekdayNums.Add(RepeatUtils.CreateWeekdayNum(taskDate));
                    rule.SetByDay(weekdayNums);
                    break;
                case TaskRepeatItemEnum.REPEATS_MONTHLY_ON_DAY_COUNT:
                    //mApplication.getAnalyticsInstance().sendRepeatSetEvent(
                    //        AnalyticsConstants.DateSetupParams.LABEL_REPEAT_MONTHLY_WEEK);
                    rule = new TickRRule();
                    rule.SetInterval(1);
                    rule.SetFreq(FrequencyType.Monthly);
                    weekdayNums.Clear();
                    weekdayNums.Add(RepeatUtils.CreateMonthWeekdayNum(taskDate));
                    rule.SetByDay(weekdayNums);
                    break;
                case TaskRepeatItemEnum.REPEATS_MONTHLY_ON_DAY:
                    //mApplication.getAnalyticsInstance().sendRepeatSetEvent(
                    //        AnalyticsConstants.DateSetupParams.LABEL_REPEAT_MONTHLY_DAY);
                    rule = new TickRRule();
                    rule.SetInterval(1);
                    rule.SetFreq(FrequencyType.Monthly);
                    if (DateTimeUtils.IsLastDayOfMonth(taskDate))
                    {
                        rule.SetByMonthDay(new int[] { -1 });
                    }
                    break;
                case TaskRepeatItemEnum.REPEATS_YEARLY:
                    //mApplication.getAnalyticsInstance().sendRepeatSetEvent(
                    //        AnalyticsConstants.DateSetupParams.LABEL_REPEAT_YEARLY);
                    rule = new TickRRule();
                    rule.SetInterval(1);
                    rule.SetFreq(FrequencyType.Yearly);
                    rule.SetByMonth(new int[] { taskDate.Month + 1 });
                    rule.SetByMonthDay(new int[] { taskDate.Day });
                    break;

                case TaskRepeatItemEnum.REPEATS_YEARLY_LUNAR:
                    //mApplication.getAnalyticsInstance().sendRepeatSetEvent(
                    //        AnalyticsConstants.DateSetupParams.LABEL_REPEAT_YEARLY_LUNAR);
                    rule = new TickRRule();
                    rule.SetInterval(1);
                    rule.SetLunarFrequency(FrequencyType.Yearly);
                    DateTime selected = taskDate;
                    // TODO 农历暂时不动。。。

                    //String time = LunarUtil.SIMPLE_DATE_FORMAT.format(selected);
                    //LunarEntity lunar = null;
                    //try
                    //{
                    //    lunar = LunarUtil.solar2Lunar(time);
                    //}
                    //catch (ParseException e)
                    //{
                    //    e.printStackTrace();
                    //}
                    //if (lunar != null)
                    //{
                    //    rule.setByMonth(new int[] {
                    //        lunar.getMonth()
                    //});

                    //    if (lunar.getDay() == 30)
                    //    {
                    //        rule.setByMonthDay(new int[] {
                    //            -1
                    //    });
                    //    }
                    //    else
                    //    {
                    //        rule.setByMonthDay(new int[] {
                    //            lunar.getDay()
                    //    });
                    //    }
                    //}
                    break;
                case TaskRepeatItemEnum.REPEATS_CUSTOM:
                    // TODO 用户自定义先不做
                    //new CustomRepeatSetDialog(mActivity, dialogSetRRuleCallback)
                    //        .showCustomRepeatDialog();
                    break;
                case TaskRepeatItemEnum.REPEATS_END:
                    // TODO 禁用当前的窗体
                    //if (view.isEnabled())
                    //{
                    //    new RepeatEndViewController(mActivity, repeatEndCallback).showRepeatEndDialog();
                    //}
                    break;
                default:
                    break;
            }
            // 直接设置的repeat选项中,repeatFrom始终是DEFAULT
            if (repeatTimeEnum != TaskRepeatItemEnum.REPEATS_CUSTOM && repeatTimeEnum != TaskRepeatItemEnum.REPEATS_END)
            {
                // TODO 未实现
                //if (!repeatArray.isEmpty())
                //{
                //    if (repeatArray.get(repeatArray.size() - 2).type
                //            == RepeatSettingsItem.ITEM_TYPE_CUSTOM)
                //    {
                //        repeatArray.get(repeatArray.size() - 2).summary = "";
                //    }
                //    if (repeatArray.get(repeatArray.size() - 1).type
                //            == RepeatSettingsItem.ITEM_TYPE_END_REPEAT)
                //    {
                //        repeatArray.get(repeatArray.size() - 1).summary = mActivity.getResources()
                //                .getStringArray(R.array.remind_before_name)[0];
                //    }
                //}
                //mRepeatSetHandler.onRepeatSet(rule, Constants.RepeatFromStatus.DEFAULT);
            }

            //更改选中状态
            //if (item.type != RepeatSettingsItem.ITEM_TYPE_END_REPEAT
            //        && item.type != RepeatSettingsItem.ITEM_TYPE_CUSTOM)
            //{
            //    for (int i = 0; i < repeatArray.size(); i++)
            //    {
            //        if (repeatArray.get(i) != null)
            //        {
            //            repeatArray.get(i).selected = false;
            //        }
            //    }
            //    item.selected = true;
            //}
            //mSettingsAdapter.notifyDataSetChanged();


            // fqn 修改repeat状态
            if (rule == null)
            {
                this.Tasks.RepeatFlag = string.Empty;
                return;
            }
            this.Tasks.RepeatFlag = rule.ToTickTickIcal();

        }