Commit e599fa6b by Sebastián Katzer

Implemented new unit flag

trigger: { unit: '..' } specifies the interval unit (second, minute, ...)
Defauls to: second
parent 7500616d
...@@ -384,15 +384,17 @@ ...@@ -384,15 +384,17 @@
*/ */
- (UNTimeIntervalNotificationTrigger*) triggerWithTimeInterval - (UNTimeIntervalNotificationTrigger*) triggerWithTimeInterval
{ {
long interval = [[self valueForTriggerOption:@"every"] longValue]; double ticks = [[self valueForTriggerOption:@"every"] doubleValue];
NSString* unit = [self valueForTriggerOption:@"unit"];
double seconds = [self convertTicksToSeconds:ticks unit:unit];
if (interval < 60) { if (seconds < 60) {
NSLog(@"time interval must be at least 60 if repeating"); NSLog(@"time interval must be at least 60 sec if repeating");
interval = 60; seconds = 60;
} }
return [UNTimeIntervalNotificationTrigger return [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:interval repeats:YES]; triggerWithTimeInterval:seconds repeats:YES];
} }
/** /**
...@@ -464,14 +466,11 @@ ...@@ -464,14 +466,11 @@
*/ */
- (double) timeInterval - (double) timeInterval
{ {
double interval = [[self valueForTriggerOption:@"in"] double ticks = [[self valueForTriggerOption:@"in"] doubleValue];
doubleValue]; NSString* unit = [self valueForTriggerOption:@"unit"];
double seconds = [self convertTicksToSeconds:ticks unit:unit];
if (interval == 0) {
interval = [self.triggerDate timeIntervalSinceDate:NSDate.date]; return MAX(0.01f, seconds);
}
return MAX(0.01f, interval);
} }
/** /**
...@@ -515,12 +514,11 @@ ...@@ -515,12 +514,11 @@
NSDateComponents* date = [[NSDateComponents alloc] init]; NSDateComponents* date = [[NSDateComponents alloc] init];
NSDictionary* every = [self valueForTriggerOption:@"every"]; NSDictionary* every = [self valueForTriggerOption:@"every"];
date.second = 0;
for (NSString* key in every) { for (NSString* key in every) {
long value = [[every valueForKey:key] longValue]; long value = [[every valueForKey:key] longValue];
if ([key isEqualToString:@"second"]) {
date.second = value;
} else
if ([key isEqualToString:@"minute"]) { if ([key isEqualToString:@"minute"]) {
date.minute = value; date.minute = value;
} else } else
...@@ -774,4 +772,42 @@ ...@@ -774,4 +772,42 @@
return url; return url;
} }
/**
* Convert the amount of ticks into seconds.
*
* @param [ double ] ticks The amount of ticks.
* @param [ NSString* ] unit The unit of the ticks (minute, hour, day, ...)
*
* @return [ double ] Amount of ticks in seconds.
*/
- (double) convertTicksToSeconds:(double)ticks unit:(NSString*)unit
{
if ([unit isEqualToString:@"second"]) {
return ticks;
} else
if ([unit isEqualToString:@"minute"]) {
return ticks * 60;
} else
if ([unit isEqualToString:@"hour"]) {
return ticks * 60 * 60;
} else
if ([unit isEqualToString:@"day"]) {
return ticks * 60 * 60 * 24;
} else
if ([unit isEqualToString:@"week"]) {
return ticks * 60 * 60 * 24 * 7;
} else
if ([unit isEqualToString:@"month"]) {
return ticks * 60 * 60 * 24 * 30.438;
} else
if ([unit isEqualToString:@"quarter"]) {
return ticks * 60 * 60 * 24 * 91.313;
} else
if ([unit isEqualToString:@"year"]) {
return ticks * 60 * 60 * 24 * 365;
}
return 0;
}
@end @end
...@@ -473,7 +473,7 @@ exports.parseTrigger = function (obj) { ...@@ -473,7 +473,7 @@ exports.parseTrigger = function (obj) {
exports.parseEvery = function (spec) { exports.parseEvery = function (spec) {
var every = new LocalNotification.Every(); var every = new LocalNotification.Every();
if (typeof spec !== 'object') return spec.toString(); if (typeof spec !== 'object') return spec;
for (var prop in every) { for (var prop in every) {
if (spec[prop]) every[prop] = parseInt(spec[prop]); if (spec[prop]) every[prop] = parseInt(spec[prop]);
......
...@@ -29,36 +29,41 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -29,36 +29,41 @@ namespace LocalNotificationProxy.LocalNotification
private DateTime? triggerDate; private DateTime? triggerDate;
/// <summary> /// <summary>
/// Gets trigger type. /// Gets the trigger type.
/// </summary> /// </summary>
public string Type { get; } = "calendar"; public string Type { get; } = "calendar";
/// <summary> /// <summary>
/// Gets or sets trigger date. /// Gets or sets the trigger date.
/// </summary> /// </summary>
public long At { get; set; } = 0; public long At { get; set; } = 0;
/// <summary> /// <summary>
/// Gets or sets relative trigger date in seconds from now. /// Gets or sets the relative trigger date from now.
/// </summary> /// </summary>
public long In { get; set; } = 0; public int In { get; set; } = 0;
/// <summary> /// <summary>
/// Gets or sets trigger count. /// Gets or sets the trigger count.
/// </summary> /// </summary>
public int Count { get; set; } = 1; public int Count { get; set; } = 1;
/// <summary> /// <summary>
/// Gets trigger occurrence. /// Gets the trigger occurrence.
/// </summary> /// </summary>
public int Occurrence { get; internal set; } = 1; public int Occurrence { get; internal set; } = 1;
/// <summary> /// <summary>
/// Gets or sets trigger interval. /// Gets or sets the trigger interval.
/// </summary> /// </summary>
public object Every { get; set; } public object Every { get; set; }
/// <summary> /// <summary>
/// Gets or sets the trigger unit.
/// </summary>
public string Unit { get; set; } = "second";
/// <summary>
/// Gets the date when to trigger the notification. /// Gets the date when to trigger the notification.
/// </summary> /// </summary>
internal DateTime? Date internal DateTime? Date
...@@ -110,8 +115,9 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -110,8 +115,9 @@ namespace LocalNotificationProxy.LocalNotification
var trigger = new Trigger(); var trigger = new Trigger();
var node = doc.DocumentElement; var node = doc.DocumentElement;
trigger.At = long.Parse(node.GetAttribute("at")); trigger.At = int.Parse(node.GetAttribute("at"));
trigger.In = long.Parse(node.GetAttribute("in")); trigger.In = int.Parse(node.GetAttribute("in"));
trigger.Unit = node.GetAttribute("unit");
trigger.Count = int.Parse(node.GetAttribute("count")); trigger.Count = int.Parse(node.GetAttribute("count"));
trigger.Occurrence = int.Parse(node.GetAttribute("occurrence")); trigger.Occurrence = int.Parse(node.GetAttribute("occurrence"));
...@@ -133,35 +139,65 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -133,35 +139,65 @@ namespace LocalNotificationProxy.LocalNotification
node.SetAttribute("at", this.At.ToString()); node.SetAttribute("at", this.At.ToString());
node.SetAttribute("in", this.In.ToString()); node.SetAttribute("in", this.In.ToString());
node.SetAttribute("unit", this.Unit);
node.SetAttribute("count", this.Count.ToString()); node.SetAttribute("count", this.Count.ToString());
node.SetAttribute("occurrence", this.Occurrence.ToString()); node.SetAttribute("occurrence", this.Occurrence.ToString());
if (this.Every is string) if (!(this.Every is Every))
{ {
node.SetAttribute("every", this.Every as string); node.SetAttribute("every", this.Every.ToString());
} }
return node.GetXml(); return node.GetXml();
} }
/// <summary> /// <summary>
/// Adds the interval to the specified date.
/// </summary>
/// <param name="date">The date where to add the interval of ticks</param>
/// <param name="interval">minute, hour, day, ...</param>
/// <param name="ticks">The number of minutes, hours, days, ...</param>
/// <returns>A new datetime instance</returns>
private DateTime? AddInterval(DateTime date, string interval, int ticks)
{
switch (interval)
{
case null:
case "":
return null;
case "second":
return DateTime.Now.AddSeconds(ticks);
case "minute":
return DateTime.Now.AddMinutes(ticks);
case "hour":
return DateTime.Now.AddHours(ticks);
case "day":
return DateTime.Now.AddDays(ticks);
case "week":
return DateTime.Now.AddDays(ticks * 7);
case "month":
return DateTime.Now.AddMonths(ticks);
case "quarter":
return DateTime.Now.AddMonths(ticks * 3);
case "year":
return DateTime.Now.AddYears(ticks);
default:
return null;
}
}
/// <summary>
/// Gets the date when to trigger the notification. /// Gets the date when to trigger the notification.
/// </summary> /// </summary>
/// <returns>The fix date specified by trigger.at or trigger.in</returns> /// <returns>The fix date specified by trigger.at or trigger.in</returns>
private DateTime GetFixDate() private DateTime? GetFixDate()
{ {
DateTime date;
if (this.In != 0) if (this.In != 0)
{ {
date = DateTime.Now.AddSeconds(this.In); return this.AddInterval(DateTime.Now, this.Unit, this.In);
}
else
{
date = DateTimeOffset.FromUnixTimeMilliseconds(this.At * 1000).LocalDateTime;
} }
return date; return DateTimeOffset.FromUnixTimeMilliseconds(this.At * 1000).LocalDateTime;
} }
/// <summary> /// <summary>
...@@ -292,67 +328,30 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -292,67 +328,30 @@ namespace LocalNotificationProxy.LocalNotification
} }
var every = this.Every is Every ? (this.Every as Every).Interval : this.Every; var every = this.Every is Every ? (this.Every as Every).Interval : this.Every;
DateTime? nextDate;
switch (every) if (every is double)
{ {
case null: var ticks = Convert.ToInt32(every);
case "":
return null;
case "minute":
date = date.AddMinutes(multiple * 1);
break;
case "hour":
date = date.AddHours(multiple * 1);
break;
case "day":
date = date.AddDays(multiple * 1);
break;
case "week":
date = date.AddDays(multiple * 7);
break;
case "month":
date = date.AddMonths(multiple * 1);
break;
case "quarter":
date = date.AddMonths(multiple * 3);
break;
case "year":
date = date.AddYears(multiple * 1);
break;
default:
try
{
var seconds = int.Parse(every as string);
if (seconds == 0) if (ticks == 0)
{ {
return null; return null;
} }
date = date.AddSeconds(multiple * seconds);
}
catch
{
return null;
}
break; nextDate = this.AddInterval(date, this.Unit, multiple * ticks);
} }
else
if (this.Every is Every)
{ {
return this.GetRelDate(date); nextDate = this.AddInterval(date, every as string, multiple);
if (nextDate.HasValue && this.Every is Every)
{
nextDate = this.GetRelDate(nextDate.Value);
}
} }
return date; return nextDate;
} }
} }
} }
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