Пример #1
0
        /// <summary>
        /// Update an reminder's date with multiple days as repeat type
        /// </summary>
        /// <param name="rem"></param>
        /// <param name="allowUpdateTime">Determines if this method is allowed to update the TIME part of the datetime object</param>
        private static void UpdateReminderDateMultipleDays(Reminder rem, bool allowUpdateTime = false)
        {
            DayOfWeek     dayOfWeekOfThisReminder = Convert.ToDateTime(rem.Date).DayOfWeek;
            List <string> days = BLDateTime.SortDayOfWeekStringList(rem.RepeatDays.Split(',').ToList()); //These are the days this reminder has. Example: monday,thursday,sunday

            int indexOfDay = 0;

            foreach (string day in days)
            {
                if (day.ToLower() != dayOfWeekOfThisReminder.ToString().ToLower())
                {
                    indexOfDay++;
                }
                else
                {
                    break;
                }
            }
            DayOfWeek nextDay;

            if (indexOfDay + 1 >= days.Count)
            {
                indexOfDay = 0;
                nextDay    = BLDateTime.GetDayOfWeekFromString(days[indexOfDay]);
            }
            else
            {
                nextDay = BLDateTime.GetDayOfWeekFromString(days[indexOfDay + 1]);
            }


            int toAddDays = BLDateTime.GetAmountOfDaysBetween(dayOfWeekOfThisReminder, nextDay);

            if (toAddDays == 0)
            {
                toAddDays = 7;
            }
            //Days between monday and monday is 0, but in our case that means 1 week, so 7 days.
            if (rem.UpdateTime == 0 || !allowUpdateTime)
            {
                rem.Date = Convert.ToDateTime(rem.Date).AddDays(toAddDays).ToString();
            }
            else //Update time aswell
            {
                rem.Date = Convert.ToDateTime(rem.Date).AddDays(toAddDays).ToShortDateString() + " " + DateTime.Now.ToLongTimeString();
            }
        }
Пример #2
0
        /// <summary>
        /// Gives a new value to a reminder based on it's repeating type, and inserts it into the database
        /// </summary>
        /// <param name="rem"></param>
        public static void UpdateReminder(Reminder rem)
        {
            if (rem != null)
            {
                BLIO.Log("Updating reminder with id " + rem.Id);
                //Enable the reminder again
                rem.Enabled = 1;

                if (rem.RepeatType == ReminderRepeatType.WORKDAYS.ToString()) //Add days to the reminder so that the next date will be a new workday
                {
                    rem.Date = BLDateTime.GetNextReminderWorkDay(rem, true).ToString();
                }

                if (rem.RepeatType == ReminderRepeatType.DAILY.ToString())    //Add a day to the reminder
                {
                    UpdateReminderDateDaily(rem, true);
                }


                if (rem.RepeatType == ReminderRepeatType.MONTHLY.ToString())
                {
                    if (rem.Date.Split(',').Length > 1)
                    {
                        List <DateTime> reminderDates = new List <DateTime>();

                        foreach (string date in rem.Date.Split(','))     //go through each date. with monthly reminders, it can have multiple dates, seperated by comma's
                        {
                            if (Convert.ToDateTime(date) < DateTime.Now) //get the next day of the monthly day of the date. example: 10-6-2017 -> 10-7-2017 BUT 31-1-2017 -> 31-3-2017, since february doesnt have 31 days
                            {
                                reminderDates.Add(Convert.ToDateTime(BLDateTime.GetDateForNextDayOfMonth(Convert.ToDateTime(date)).ToShortDateString() + " " + Convert.ToDateTime(date).ToShortTimeString()));
                            }
                            else
                            {
                                reminderDates.Add(Convert.ToDateTime(date)); //Date in the future? good! do nothing with it.
                            }
                        }
                        //have to make sure the first date is in front.
                        reminderDates.Sort();

                        //Now, we're going to put the (sorted) dates in a string
                        string newDateString = "";
                        foreach (DateTime date in reminderDates)
                        {
                            if (rem.UpdateTime == 0)
                            {
                                newDateString += date.ToString() + ",";
                            }
                            else
                            {
                                newDateString += date.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ",";
                            }
                        }


                        rem.Date = newDateString.Remove(newDateString.Length - 1, 1);
                    }
                    else
                    {//There's only one date in this string.
                        if (rem.UpdateTime == 0)
                        {
                            rem.Date = BLDateTime.GetDateForNextDayOfMonth(Convert.ToDateTime(rem.Date)).ToShortDateString() + " " + Convert.ToDateTime(rem.Date).ToShortTimeString();
                        }
                        else
                        {
                            rem.Date = BLDateTime.GetDateForNextDayOfMonth(Convert.ToDateTime(rem.Date)).ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
                        }
                    }
                }

                if (rem.RepeatType == ReminderRepeatType.MULTIPLE_DAYS.ToString())
                {
                    if (rem.UpdateTime == 0)
                    {
                        rem.Date = Convert.ToDateTime(BLDateTime.GetEarliestDateFromListOfStringDays(rem.RepeatDays)).ToShortDateString() + " " + Convert.ToDateTime(rem.Date).ToShortTimeString();
                    }
                    else
                    {
                        rem.Date = Convert.ToDateTime(BLDateTime.GetEarliestDateFromListOfStringDays(rem.RepeatDays)).ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
                    }
                }

                if (rem.EveryXCustom != null)
                {
                    while (Convert.ToDateTime(rem.Date) < DateTime.Now)
                    {
                        //The user has a custom reminder, every x minutes,hours,days,weeks, or months
                        switch (rem.RepeatType.ToLower())
                        {
                        case "minutes":
                            rem.Date = Convert.ToDateTime(rem.Date).AddMinutes((double)rem.EveryXCustom).ToString();
                            break;

                        case "hours":
                            rem.Date = Convert.ToDateTime(rem.Date).AddHours((double)rem.EveryXCustom).ToString();
                            break;

                        case "days":
                            rem.Date = Convert.ToDateTime(rem.Date).AddDays((double)rem.EveryXCustom).ToString();
                            if (rem.UpdateTime == 1)
                            {
                                rem.Date = Convert.ToDateTime(rem.Date).ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
                            }
                            break;

                        case "weeks":
                            rem.Date = Convert.ToDateTime(rem.Date).AddDays((double)rem.EveryXCustom * 7).ToString();
                            if (rem.UpdateTime == 1)
                            {
                                rem.Date = Convert.ToDateTime(rem.Date).ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
                            }
                            break;

                        case "months":
                            rem.Date = Convert.ToDateTime(rem.Date).AddMonths((int)rem.EveryXCustom).ToString();
                            if (rem.UpdateTime == 1)
                            {
                                rem.Date = Convert.ToDateTime(rem.Date).ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
                            }
                            break;
                        }
                    }
                }
                if (rem.RepeatType == ReminderRepeatType.NONE.ToString())
                {
                    if (rem.Date.Split(',').Length > 1) //multiple dates seperated by comma's
                    {
                        string newDateString = "";      //The new date1,date2,date3 string that we will assign to the reminder

                        string[] dateArray = rem.Date.Split(',');

                        dateArray = dateArray.Where(s => Convert.ToDateTime(s) > DateTime.Now).ToArray(); //remove all elements from the array that already happened

                        if (dateArray.Length == 0)
                        {
                            DLReminders.ArchiveReminder(rem);
                            return;
                        }

                        foreach (string date in dateArray)
                        {
                            newDateString += date + ",";
                        }

                        newDateString = newDateString.Remove(newDateString.Length - 1, 1); //remove the last ','

                        rem.Date = newDateString;
                    }
                    else//it had one date, and that date caused this popup. Let's delete the reminder.
                    {
                        DLReminders.ArchiveReminder(rem);
                        return;
                    }
                }
                //finally, Write the changes to the database
                DLReminders.EditReminder(rem);
                BLIO.Log("Reminder updated");
            }
            else
            {
                throw new ArgumentNullException("parameter rem in UpdateReminder is null.");
            }
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="rem"></param>
        public static void SkipToNextReminderDate(Reminder rem)
        {
            if (!IsRepeatableReminder(rem))
            {
                return; //If the parameter is a reminder that doesn't have 2 or more dates, we can't continue.
            }
            switch (rem.RepeatType)
            {
            case "NONE":     //Remove the first element [0] from the string, and assign the new value to the reminder
                List <string> dates         = rem.Date.Split(',').ToList();
                string        newDateString = "";
                dates.RemoveAt(0);

                foreach (string dat in dates)
                {
                    newDateString += dat + ",";
                }

                newDateString = newDateString.Remove(newDateString.Length - 1, 1);
                rem.Date      = newDateString;

                break;

            case "DAILY":
                rem.Date = Convert.ToDateTime(rem.Date).AddDays(1).ToString();
                break;

            case "MONTHLY":
                //Add 1 month to the first date of the month string, [0] and add it to the end
                List <string> monthDates      = rem.Date.Split(',').ToList();
                string        newMonthString  = "";
                int           monthDay        = Convert.ToDateTime(monthDates[0]).Day;
                int           monthsInAdvance = 1;
                string        updatedDate     = "";

                //If you add 1 month, and the day is not equal, then that means the next month doesnt have enough days. Keep adding months until we find the good month
                //Example: Day of month: 31. Add 1 month to january, thats february. It doesnt have 31 days, so it should go to the next month, etc etc
                if (Convert.ToDateTime(monthDates[0]).AddMonths(1).Day != monthDay)
                {
                    while (Convert.ToDateTime(monthDates[0]).AddMonths(monthsInAdvance).Day != monthDay)
                    {
                        monthsInAdvance++;
                    }
                    updatedDate = Convert.ToDateTime(monthDates[0]).AddMonths(monthsInAdvance).ToString();
                }
                else
                {
                    updatedDate = Convert.ToDateTime(monthDates[0]).AddMonths(1).ToString();     //Just add 1 month
                }
                //Remove the old date at index 0, and add the new date at the end.
                monthDates.RemoveAt(0);
                monthDates.Add(updatedDate);

                foreach (string monthDate in monthDates)
                {
                    newMonthString += monthDate + ",";
                }

                newDateString = newMonthString.Remove(newMonthString.Length - 1, 1);
                rem.Date      = newDateString;
                break;

            case "WORKDAYS":
                if (Convert.ToDateTime(rem.Date).DayOfWeek == DayOfWeek.Friday)
                {
                    rem.Date = Convert.ToDateTime(rem.Date).AddDays(3).ToString();     //Add 3 days, friday -> saturday -> sunday -> monday
                }
                else if (Convert.ToDateTime(rem.Date).DayOfWeek == DayOfWeek.Saturday)
                {
                    rem.Date = Convert.ToDateTime(rem.Date).AddDays(2).ToString();     //Add 2 days,  saturday -> sunday -> monday
                }
                else
                {
                    rem.Date = Convert.ToDateTime(rem.Date).AddDays(1).ToString();
                }
                break;

            case "MULTIPLE_DAYS":
                DayOfWeek     dayOfWeekOfThisReminder = Convert.ToDateTime(rem.Date).DayOfWeek;
                List <string> days = BLDateTime.SortDayOfWeekStringList(rem.RepeatDays.Split(',').ToList());    //These are the days this reminder has. Example: monday,thursday,sunday

                int indexOfDay = 0;
                foreach (string day in days)
                {
                    if (day.ToLower() != dayOfWeekOfThisReminder.ToString().ToLower())
                    {
                        indexOfDay++;
                    }
                    else
                    {
                        break;
                    }
                }
                DayOfWeek nextDay;

                if (indexOfDay + 1 >= days.Count)
                {
                    indexOfDay = 0;
                    nextDay    = BLDateTime.GetDayOfWeekFromString(days[indexOfDay]);
                }
                else
                {
                    nextDay = BLDateTime.GetDayOfWeekFromString(days[indexOfDay + 1]);
                }


                int toAddDays = BLDateTime.GetAmountOfDaysBetween(dayOfWeekOfThisReminder, nextDay);
                if (toAddDays == 0)
                {
                    toAddDays = 7;
                }
                //Days between monday and monday is 0, but in our case that means 1 week, so 7 days.
                rem.Date = Convert.ToDateTime(rem.Date).AddDays(toAddDays).ToString();
                break;

            default:
                if (rem.EveryXCustom != null)
                {
                    switch (rem.RepeatType.ToLower())
                    {
                    case "minutes":
                        rem.Date = Convert.ToDateTime(rem.Date).AddMinutes((double)rem.EveryXCustom).ToString();
                        break;

                    case "hours":
                        rem.Date = Convert.ToDateTime(rem.Date).AddHours((double)rem.EveryXCustom).ToString();
                        break;

                    case "days":
                        rem.Date = Convert.ToDateTime(rem.Date).AddDays((double)rem.EveryXCustom).ToString();
                        break;

                    case "weeks":
                        rem.Date = Convert.ToDateTime(rem.Date).AddDays((double)rem.EveryXCustom * 7).ToString();
                        break;

                    case "months":
                        rem.Date = Convert.ToDateTime(rem.Date).AddMonths((int)rem.EveryXCustom).ToString();
                        break;
                    }
                }
                break;
            }
        }