Commit 7500616d by Sebastián Katzer

Proper calculate next trigger date (Windows)

parent 55890836
...@@ -69,7 +69,7 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -69,7 +69,7 @@ namespace LocalNotificationProxy.LocalNotification
/// If there is at least one more toast variant to build. /// If there is at least one more toast variant to build.
/// </summary> /// </summary>
/// <returns>True if there are more toasts to build.</returns> /// <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> /// <summary>
/// Moves the flag to the next toast variant. /// Moves the flag to the next toast variant.
......
...@@ -61,17 +61,17 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -61,17 +61,17 @@ namespace LocalNotificationProxy.LocalNotification
/// <summary> /// <summary>
/// Converts the date time components into a datetime object. /// Converts the date time components into a datetime object.
/// </summary> /// </summary>
/// <param name="now">The relative date to calculate the date from.</param>
/// <returns>A datetime object</returns> /// <returns>A datetime object</returns>
internal DateTime ToDateTime() internal DateTime ToDateTime(DateTime now)
{ {
var p = this.ToArray(); var p = this.ToArray();
var today = DateTime.Today;
p[0] = this.Minute.GetValueOrDefault(); p[0] = this.Minute.GetValueOrDefault();
p[1] = this.Hour.GetValueOrDefault(); p[1] = this.Hour.GetValueOrDefault();
p[2] = this.Day.GetValueOrDefault(today.Day); p[2] = this.Day.GetValueOrDefault(now.Day);
p[3] = this.Month.GetValueOrDefault(today.Month); p[3] = this.Month.GetValueOrDefault(now.Month);
p[4] = this.Year.GetValueOrDefault(today.Year); 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); return new DateTime(p[4].Value, p[3].Value, p[2].Value, p[1].Value, p[0].Value, 0);
} }
......
...@@ -52,11 +52,12 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -52,11 +52,12 @@ namespace LocalNotificationProxy.LocalNotification
{ {
var toast = builder.Build(); var toast = builder.Build();
if (toast != null) if (toast == null)
{ {
ToastNotifier.AddToSchedule(toast); break;
} }
ToastNotifier.AddToSchedule(toast);
builder.MoveNext(); builder.MoveNext();
} }
while (builder.HasNext()); while (builder.HasNext());
......
...@@ -170,9 +170,18 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -170,9 +170,18 @@ namespace LocalNotificationProxy.LocalNotification
/// <returns>The first matching date specified by trigger.every</returns> /// <returns>The first matching date specified by trigger.every</returns>
private DateTime? GetRelDate() 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 every = this.Every as Every;
var date = every.ToDateTime(); var date = every.ToDateTime(now);
var now = DateTime.Now;
if (date >= now) if (date >= now)
{ {
...@@ -274,7 +283,6 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -274,7 +283,6 @@ namespace LocalNotificationProxy.LocalNotification
/// <returns>The next valid trigger date</returns> /// <returns>The next valid trigger date</returns>
private DateTime? GetNextTriggerDate() private DateTime? GetNextTriggerDate()
{ {
var every = this.Every is Every ? (this.Every as Every).Interval : this.Every;
var date = this.triggerDate.Value; var date = this.triggerDate.Value;
var multiple = this.Occurrence - 1; var multiple = this.Occurrence - 1;
...@@ -283,35 +291,68 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -283,35 +291,68 @@ namespace LocalNotificationProxy.LocalNotification
return date; return date;
} }
var every = this.Every is Every ? (this.Every as Every).Interval : this.Every;
switch (every) switch (every)
{ {
case null: case null:
case "": case "":
return date; return null;
case "minute": case "minute":
return date.AddMinutes(multiple * 1); date = date.AddMinutes(multiple * 1);
break;
case "hour": case "hour":
return date.AddHours(multiple * 1); date = date.AddHours(multiple * 1);
break;
case "day": case "day":
return date.AddDays(multiple * 1); date = date.AddDays(multiple * 1);
break;
case "week": case "week":
return date.AddDays(multiple * 7); date = date.AddDays(multiple * 7);
break;
case "month": case "month":
return date.AddMonths(multiple * 1); date = date.AddMonths(multiple * 1);
break;
case "quarter": case "quarter":
return date.AddMonths(multiple * 3); date = date.AddMonths(multiple * 3);
break;
case "year": case "year":
return date.AddYears(multiple * 1); date = date.AddYears(multiple * 1);
} break;
try default:
{ try
return date.AddSeconds(multiple * (60 * int.Parse(every as string))); {
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;
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment