Exemplo n.º 1
0
        public void EnableAlarm(BaseAlarmSetting alarm)
        {
            try
            {
                if (alarm.Occurrence == OccurrenceType.OnlyOnce)
                {
                    var dateTime = alarm.DateTimeOffset.Date.Add(alarm.Time);
                    if (!CheckAlarmDateTime(dateTime))
                    {
                        return;
                    }

                    CreateNotification(alarm.Id, alarm.AudioFilename, alarm.ImageFilename, dateTime.ToUniversalTime());
                }
                else // repeatedly is checked
                {
                    // TODO: use only day properties not aggregated property!

                    PlanFutureAlarms(alarm);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Exemplo n.º 2
0
        private void LoadAlarmSettingsVersion0()
        {
            // TODO: setting versioning
            Alarms.Clear();

            // we are trying to load setting for the first time => there is nothing
            // when versioning will be supported check mechanism should be better
            // or someone delted the settings.dat file for example.
            if (Windows.Storage.ApplicationData.Current.LocalSettings.Values.ContainsKey(AlarmsKey) == false)
            {
                return;
            }

            var settingsStr = Windows.Storage.ApplicationData.Current.LocalSettings.Values[AlarmsKey] as string;
            var alarmsJson  = JsonValue.Parse(settingsStr).GetArray();

            foreach (var alarmJsonValue in alarmsJson)
            {
                var alarmJson = alarmJsonValue.GetObject();
                var alarm     = new BaseAlarmSetting();
                alarm.Id            = int.Parse(alarmJson[JsonId].GetString());
                alarm.Enabled       = Boolean.Parse(alarmJson[JsonEnabled].GetString());
                alarm.Time          = new TimeSpan(long.Parse(alarmJson[JsonTime].GetString()));
                alarm.DaysOfWeek    = (DayOfWeekType)int.Parse(alarmJson[JsonDaysOfWeek].GetString());
                alarm.AudioFilename = alarmJson[JsonAudioFilename].GetString();
                alarm.ImageFilename = alarmJson[JsonImageFilename].GetString();
                alarm.Occurrence    = (OccurrenceType)Enum.Parse(typeof(OccurrenceType), alarmJson[JsonOccurrence].GetString());

                // Fix: serialization of DateTimeOffset was missing. I do not want to change "date version" so I just check whether key is available here.
                if (alarmJson.ContainsKey(JsonDateTimeOffset))
                {
                    alarm.DateTimeOffset = DateTimeOffset.Parse(alarmJson[JsonDateTimeOffset].GetString(), System.Globalization.CultureInfo.InvariantCulture);
                }

                Alarms.Add(alarm);
            }
        }
Exemplo n.º 3
0
 public void DisableAlarm(BaseAlarmSetting alarm)
 {
     ClearAlarmsNotifications(alarm.Id);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Plans future alarms.
        /// If it is already planned then nothing happens.
        /// </summary>
        /// <param name="alarm"></param>
        public void PlanFutureAlarms(BaseAlarmSetting alarm)
        {
            // get already planned toasts for this alarm
            // so we can won't plan it more then once.
            var plannedToasts = ToastNotificationManager.CreateToastNotifier().GetScheduledToastNotifications()
                                .Where(t => t.Id == alarm.Id.ToString()).ToList();

            var days = new List <DayOfWeek>();

            if (alarm.UseMonday)
            {
                days.Add(DayOfWeek.Monday);
            }
            if (alarm.UseTuesday)
            {
                days.Add(DayOfWeek.Tuesday);
            }
            if (alarm.UseThursday)
            {
                days.Add(DayOfWeek.Thursday);
            }
            if (alarm.UseWednesday)
            {
                days.Add(DayOfWeek.Wednesday);
            }
            if (alarm.UseFriday)
            {
                days.Add(DayOfWeek.Friday);
            }
            if (alarm.UseSaturday)
            {
                days.Add(DayOfWeek.Saturday);
            }
            if (alarm.UseSunday)
            {
                days.Add(DayOfWeek.Sunday);
            }

            // plan 14 days ahead
            // TODO: Background task has to be used to plan future toasts.
            var date = DateTime.Now.Date;

            for (int i = 0; i < 14; i++)
            {
                var plannedDate = date.AddDays(i);
                if (days.Contains(plannedDate.DayOfWeek)) // day is checked to be used for alarm
                {
                    var dateTime = plannedDate.Add(alarm.Time);
                    if (!CheckAlarmDateTime(dateTime))
                    {
                        continue;
                    }

                    // this alarm is already planned
                    // skip planning
                    if (plannedToasts.Any(t => t.DeliveryTime == dateTime))
                    {
                        continue;
                    }

                    // holidays should be ignores so we will check for them.
                    // It applies only for occurenes which are set to "repeatedly"
                    if (alarm.Occurrence == OccurrenceType.Repeatedly && alarm.IgnoreHolidays)
                    {
                        // TODO: handle local time/date time offcet!!! use offset!!!
                        var holiday = BaseAlarmSettings.Instance.GetHoliday(plannedDate.Date);
                        if (holiday != null) // holiday exists => do not set alarm
                        {
                            DateTime holidayInformDateTime;
                            if (holiday.Date.Date == DateTime.Now.Date) // holiday is today => inform immediatelly
                            {
                                holidayInformDateTime = DateTime.Now.AddMinutes(1);
                            }
                            else if (holiday.Date.Date == DateTime.Now.Date.AddDays(1))
                            {
                                holidayInformDateTime = DateTime.Now.AddMinutes(1); // holiday is tomorrow => inform immediatelly so we can change it if required
                            }
                            else // more than one day to holiday => plan message
                            {
                                holidayInformDateTime = holiday.Date.AddDays(-1); // inform us one day before holiday
                            }
                            var message  = $"Alarm not set for {holiday.Date.Date.ToString("MM.dd.yyyy")} because that day is holiday.";
                            var message2 = $"Holiday description: {holiday.LocalDescription}";
                            CreateHolidayNotification(alarm.Id, "Alarm -> Holiday", message, message2, holidayInformDateTime);

                            continue;
                        }
                    }

                    // either holiday is not found (that day is not holiday or holidays are not laoded) or occurenc is set to "once"
                    CreateNotification(alarm.Id, alarm.AudioFilename, alarm.ImageFilename, dateTime.ToUniversalTime());
                }
            }
        }