Explorar el Código

Proper calculate next trigger date (Windows)

Sebastián Katzer hace 8 años
padre
commit
7500616d59

+ 1 - 1
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Builder.cs

@@ -69,7 +69,7 @@ namespace LocalNotificationProxy.LocalNotification
         /// If there is at least one more toast variant to build.
         /// </summary>
         /// <returns>True if there are more toasts to build.</returns>
-        public bool HasNext() => this.Trigger.Count > this.Trigger.Occurrence;
+        public bool HasNext() => this.Trigger.Count >= this.Trigger.Occurrence;
 
         /// <summary>
         /// Moves the flag to the next toast variant.

+ 5 - 5
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Every.cs

@@ -61,17 +61,17 @@ namespace LocalNotificationProxy.LocalNotification
         /// <summary>
         /// Converts the date time components into a datetime object.
         /// </summary>
+        /// <param name="now">The relative date to calculate the date from.</param>
         /// <returns>A datetime object</returns>
-        internal DateTime ToDateTime()
+        internal DateTime ToDateTime(DateTime now)
         {
             var p = this.ToArray();
-            var today = DateTime.Today;
 
             p[0] = this.Minute.GetValueOrDefault();
             p[1] = this.Hour.GetValueOrDefault();
-            p[2] = this.Day.GetValueOrDefault(today.Day);
-            p[3] = this.Month.GetValueOrDefault(today.Month);
-            p[4] = this.Year.GetValueOrDefault(today.Year);
+            p[2] = this.Day.GetValueOrDefault(now.Day);
+            p[3] = this.Month.GetValueOrDefault(now.Month);
+            p[4] = this.Year.GetValueOrDefault(now.Year);
 
             return new DateTime(p[4].Value, p[3].Value, p[2].Value, p[1].Value, p[0].Value, 0);
         }

+ 3 - 2
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Manager.cs

@@ -52,11 +52,12 @@ namespace LocalNotificationProxy.LocalNotification
                 {
                     var toast = builder.Build();
 
-                    if (toast != null)
+                    if (toast == null)
                     {
-                        ToastNotifier.AddToSchedule(toast);
+                        break;
                     }
 
+                    ToastNotifier.AddToSchedule(toast);
                     builder.MoveNext();
                 }
                 while (builder.HasNext());

+ 58 - 17
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Trigger.cs

@@ -169,10 +169,19 @@ namespace LocalNotificationProxy.LocalNotification
         /// </summary>
         /// <returns>The first matching date specified by trigger.every</returns>
         private DateTime? GetRelDate()
+        {
+            return this.GetRelDate(DateTime.Now);
+        }
+
+        /// <summary>
+        /// Gets the date when to trigger the notification.
+        /// </summary>
+        /// <param name="now">The relative date to calculate the date from.</param>
+        /// <returns>The first matching date specified by trigger.every</returns>
+        private DateTime? GetRelDate(DateTime now)
         {
             var every = this.Every as Every;
-            var date = every.ToDateTime();
-            var now = DateTime.Now;
+            var date = every.ToDateTime(now);
 
             if (date >= now)
             {
@@ -274,7 +283,6 @@ namespace LocalNotificationProxy.LocalNotification
         /// <returns>The next valid trigger date</returns>
         private DateTime? GetNextTriggerDate()
         {
-            var every = this.Every is Every ? (this.Every as Every).Interval : this.Every;
             var date = this.triggerDate.Value;
             var multiple = this.Occurrence - 1;
 
@@ -283,35 +291,68 @@ namespace LocalNotificationProxy.LocalNotification
                 return date;
             }
 
+            var every = this.Every is Every ? (this.Every as Every).Interval : this.Every;
+
             switch (every)
             {
                 case null:
                 case "":
-                    return date;
+                    return null;
+
                 case "minute":
-                    return date.AddMinutes(multiple * 1);
+                    date = date.AddMinutes(multiple * 1);
+                    break;
+
                 case "hour":
-                    return date.AddHours(multiple * 1);
+                    date = date.AddHours(multiple * 1);
+                    break;
+
                 case "day":
-                    return date.AddDays(multiple * 1);
+                    date = date.AddDays(multiple * 1);
+                    break;
+
                 case "week":
-                    return date.AddDays(multiple * 7);
+                    date = date.AddDays(multiple * 7);
+                    break;
+
                 case "month":
-                    return date.AddMonths(multiple * 1);
+                    date = date.AddMonths(multiple * 1);
+                    break;
+
                 case "quarter":
-                    return date.AddMonths(multiple * 3);
+                    date = date.AddMonths(multiple * 3);
+                    break;
+
                 case "year":
-                    return date.AddYears(multiple * 1);
-            }
+                    date = date.AddYears(multiple * 1);
+                    break;
 
-            try
-            {
-                return date.AddSeconds(multiple * (60 * int.Parse(every as string)));
+                default:
+                    try
+                    {
+                        var seconds = int.Parse(every as string);
+
+                        if (seconds == 0)
+                        {
+                            return null;
+                        }
+
+                        date = date.AddSeconds(multiple * seconds);
+                    }
+                    catch
+                    {
+                        return null;
+                    }
+
+                    break;
             }
-            catch
+
+            if (this.Every is Every)
             {
-                return null;
+                return this.GetRelDate(date);
             }
+
+            return date;
         }
     }
 }