소스 검색

Added every: { minute:, hour:, ... } } for Windows
TODO: end of hour, day, month

Sebastián Katzer 8 년 전
부모
커밋
61e8c70b5a

+ 22 - 3
src/windows/LocalNotificationProxy.js

@@ -352,11 +352,11 @@ exports.clicked = function (xml, input) {
  * @return [ Void ]
  */
 exports.fireEvent = function (event, toast, data) {
-    var meta   = Object.assign({ event: event, queued: !ready }, data),
+    var meta   = Object.assign({ event: event }, data),
         plugin = cordova.plugins.notification.local.core;
 
     if (!ready) {
-        queue.push([event, toast, meta]);
+        queue.push(arguments);
         return;
     }
 
@@ -457,12 +457,31 @@ exports.parseTrigger = function (obj) {
     for (var prop in trigger) {
         val = spec[prop];
         if (!val) continue;
-        trigger[prop] = prop == 'every' ? val.toString() : val;
+        trigger[prop] = prop == 'every' ? exports.parseEvery(val) : val;
     }
 
     return trigger;
 };
 
+/**
+ * Parse trigger.every spec into instance of prefered type.
+ *
+ * @param [ Object ] spec The trigger.every object.
+ *
+ * @return [ LocalNotification.Every|String ]
+ */
+exports.parseEvery = function (spec) {
+    var every = new LocalNotification.Every();
+
+    if (typeof spec !== 'object') return spec.toString();
+
+    for (var prop in every) {
+        if (spec[prop]) every[prop] = parseInt(spec[prop]);
+    }
+
+    return every;
+};
+
 /**
  * Parse action specs into instances of prefered types.
  *

+ 130 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Every.cs

@@ -0,0 +1,130 @@
+/*
+ * Apache 2.0 License
+ *
+ * Copyright (c) Sebastian Katzer 2017
+ *
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apache License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://opensource.org/licenses/Apache-2.0/ and read it before using this
+ * file.
+ *
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ */
+
+namespace LocalNotificationProxy.LocalNotification
+{
+    using System;
+
+    public sealed class Every
+    {
+        private static readonly string[] Intervals = { null, "minute", "hour", "day", "month", "year" };
+
+        /// <summary>
+        /// Gets or sets the minute.
+        /// </summary>
+        public int Minute { get; set; }
+
+        /// <summary>
+        /// Gets or sets the hour.
+        /// </summary>
+        public int Hour { get; set; }
+
+        /// <summary>
+        /// Gets or sets the day.
+        /// </summary>
+        public int Day { get; set; }
+
+        /// <summary>
+        /// Gets or sets the month.
+        /// </summary>
+        public int Month { get; set; }
+
+        /// <summary>
+        /// Gets or sets the year.
+        /// </summary>
+        public int Year { get; set; }
+
+        /// <summary>
+        /// Gets a value indicating whether the minute is not fix.
+        /// </summary>
+        internal bool MinuteIsVariable { get => this.Minute == 0; }
+
+        /// <summary>
+        /// Gets a value indicating whether the hour is not fix.
+        /// </summary>
+        internal bool HourIsVariable { get => this.Hour == 0; }
+
+        /// <summary>
+        /// Gets a value indicating whether the day is not fix.
+        /// </summary>
+        internal bool DayIsVariable { get => this.Day == 0; }
+
+        /// <summary>
+        /// Gets a value indicating whether the month is not fix.
+        /// </summary>
+        internal bool MonthIsVariable { get => this.Month == 0; }
+
+        /// <summary>
+        /// Gets a value indicating whether the year is not fix.
+        /// </summary>
+        internal bool YearIsVariable { get => this.Year == 0; }
+
+        /// <summary>
+        /// Gets the interval as a string representation.
+        /// </summary>
+        /// <returns>year, month, ...</returns>
+        internal string Interval
+        {
+            get
+            {
+                var pos = Array.IndexOf(this.ToArray(), 0);
+
+                return Intervals[pos + 1];
+            }
+        }
+
+        /// <summary>
+        /// Gets an array of all date parts to construct a datetime instance.
+        /// </summary>
+        /// <returns>[min, hour, day, month, year]</returns>
+        internal int[] ToArray()
+        {
+            return new int[] { this.Minute, this.Hour, this.Day, this.Month, this.Year };
+        }
+
+        /// <summary>
+        /// Gets an array of all date parts to construct a datetime instance.
+        /// </summary>
+        /// <returns>[min, hour, day, month, year]</returns>
+        internal int[] ToArray2()
+        {
+            var today = DateTime.Today;
+            var ary = this.ToArray();
+
+            ary[2] = this.Day > 0 ? this.Day : today.Day;
+            ary[3] = this.Month > 0 ? this.Month : today.Month;
+            ary[4] = this.Year > 0 ? this.Year : today.Year;
+
+            return ary;
+        }
+
+        /// <summary>
+        /// Converts the date time components into a datetime object.
+        /// </summary>
+        /// <returns>A datetime object</returns>
+        internal DateTime ToDateTime()
+        {
+            var p = this.ToArray2();
+
+            return new DateTime(p[4], p[3], p[2], p[1], p[0], 0);
+        }
+    }
+}

+ 1 - 59
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Notification.cs

@@ -292,65 +292,7 @@
         {
             get
             {
-                var trigger = this.Options.Trigger;
-                var minDate = DateTime.Now.AddSeconds(0.1);
-                DateTime date;
-
-                if (trigger.In != 0)
-                {
-                    date = DateTime.Now.AddSeconds(trigger.In);
-                }
-                else
-                {
-                    date = DateTimeOffset.FromUnixTimeMilliseconds(trigger.At * 1000).LocalDateTime;
-                }
-
-                date = date.AddTicks(this.Interval.Ticks * (trigger.Occurrence - 1));
-
-                return (date < minDate) ? minDate : date;
-            }
-        }
-
-        /// <summary>
-        /// Gets the parsed repeat interval.
-        /// </summary>
-        public TimeSpan Interval
-        {
-            get
-            {
-                var every = this.Options.Trigger.Every;
-
-                switch (every)
-                {
-                    case null:
-                    case "":
-                        return TimeSpan.Zero;
-                    case "second":
-                        return new TimeSpan(TimeSpan.TicksPerSecond);
-                    case "minute":
-                        return new TimeSpan(TimeSpan.TicksPerMinute);
-                    case "hour":
-                        return new TimeSpan(TimeSpan.TicksPerHour);
-                    case "day":
-                        return new TimeSpan(TimeSpan.TicksPerDay);
-                    case "week":
-                        return new TimeSpan(TimeSpan.TicksPerDay * 7);
-                    case "month":
-                        return new TimeSpan(TimeSpan.TicksPerDay * 31);
-                    case "quarter":
-                        return new TimeSpan(TimeSpan.TicksPerHour * 2190);
-                    case "year":
-                        return new TimeSpan(TimeSpan.TicksPerDay * 365);
-                }
-
-                try
-                {
-                    return TimeSpan.FromSeconds(60 * int.Parse(every));
-                }
-                catch
-                {
-                    return TimeSpan.Zero;
-                }
+                return this.Options.Trigger.Date;
             }
         }
 

+ 235 - 5
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Trigger.cs

@@ -19,12 +19,16 @@
  * limitations under the License.
  */
 
-using Windows.Data.Xml.Dom;
-
 namespace LocalNotificationProxy.LocalNotification
 {
+    using System;
+    using Windows.Data.Xml.Dom;
+
     public sealed class Trigger
     {
+        private DateTime? triggerDate;
+        private TimeSpan? triggerInterval;
+
         /// <summary>
         /// Gets trigger type.
         /// </summary>
@@ -53,7 +57,88 @@ namespace LocalNotificationProxy.LocalNotification
         /// <summary>
         /// Gets or sets trigger interval.
         /// </summary>
-        public string Every { get; set; }
+        public object Every { get; set; }
+
+        /// <summary>
+        /// Gets the date when to trigger the notification.
+        /// </summary>
+        internal DateTime Date
+        {
+            get
+            {
+                var minDate = DateTime.Now.AddSeconds(0.1);
+
+                if (!this.triggerDate.HasValue)
+                {
+                    this.triggerDate = this.Every is Every ? this.GetRelDate() : this.GetFixDate();
+                }
+
+                var ticks = this.Interval.Ticks * (this.Occurrence - 1);
+                var date = this.triggerDate.Value.AddTicks(ticks);
+
+                return (date < minDate) ? minDate : date;
+            }
+        }
+
+        /// <summary>
+        /// Gets the parsed repeat interval.
+        /// </summary>
+        internal TimeSpan Interval
+        {
+            get
+            {
+                if (this.triggerInterval.HasValue)
+                {
+                    return this.triggerInterval.Value;
+                }
+
+                var every = this.Every is Every ? (this.Every as Every).Interval : this.Every;
+
+                try
+                {
+                    switch (every)
+                    {
+                        case null:
+                        case "":
+                            this.triggerInterval = TimeSpan.Zero;
+                            break;
+                        case "second":
+                            this.triggerInterval = new TimeSpan(TimeSpan.TicksPerSecond);
+                            break;
+                        case "minute":
+                            this.triggerInterval = new TimeSpan(TimeSpan.TicksPerMinute);
+                            break;
+                        case "hour":
+                            this.triggerInterval = new TimeSpan(TimeSpan.TicksPerHour);
+                            break;
+                        case "day":
+                            this.triggerInterval = new TimeSpan(TimeSpan.TicksPerDay);
+                            break;
+                        case "week":
+                            this.triggerInterval = new TimeSpan(TimeSpan.TicksPerDay * 7);
+                            break;
+                        case "month":
+                            this.triggerInterval = new TimeSpan(TimeSpan.TicksPerDay * 31);
+                            break;
+                        case "quarter":
+                            this.triggerInterval = new TimeSpan(TimeSpan.TicksPerHour * 2190);
+                            break;
+                        case "year":
+                            this.triggerInterval = new TimeSpan(TimeSpan.TicksPerDay * 365);
+                            break;
+                        default:
+                            this.triggerInterval = TimeSpan.FromSeconds(60 * int.Parse(every as string));
+                            break;
+                    }
+                }
+                catch
+                {
+                    this.triggerInterval = TimeSpan.Zero;
+                }
+
+                return this.triggerInterval.Value;
+            }
+        }
 
         /// <summary>
         /// Deserializes the XML string into an instance of Trigger.
@@ -94,12 +179,157 @@ namespace LocalNotificationProxy.LocalNotification
             node.SetAttribute("count", this.Count.ToString());
             node.SetAttribute("occurrence", this.Occurrence.ToString());
 
-            if (this.Every != null)
+            if (this.Every is string)
             {
-                node.SetAttribute("every", this.Every);
+                node.SetAttribute("every", this.Every as string);
             }
 
             return node.GetXml();
         }
+
+        /// <summary>
+        /// Gets the date when to trigger the notification.
+        /// </summary>
+        /// <returns>The fix date specified by trigger.at or trigger.in</returns>
+        private DateTime GetFixDate()
+        {
+            DateTime date;
+
+            if (this.In != 0)
+            {
+                date = DateTime.Now.AddSeconds(this.In);
+            }
+            else
+            {
+                date = DateTimeOffset.FromUnixTimeMilliseconds(this.At * 1000).LocalDateTime;
+            }
+
+            return date;
+        }
+
+        /// <summary>
+        /// Gets the date when to trigger the notification.
+        /// </summary>
+        /// <returns>The first matching date specified by trigger.every</returns>
+        private DateTime GetRelDate()
+        {
+            var every = this.Every as Every;
+            var p = every.ToArray2();
+            var date = every.ToDateTime();
+            var now = DateTime.Now;
+
+            if (date >= now || date.Year < now.Year)
+            {
+                return date;
+            }
+
+            if (date.Month < now.Month)
+            {
+                switch (every.Interval)
+                {
+                    case "minute":
+                    case "hour":
+                    case "day":
+                        if (every.YearIsVariable)
+                        {
+                            p[4] = now.Year + 1;
+                        }
+
+                        break;
+                    case "year":
+                        p[4] = now.Year + 1;
+                        break;
+                }
+            }
+            else if (date.Day < now.Day)
+            {
+                switch (every.Interval)
+                {
+                    case "minute":
+                    case "hour":
+                        if (every.MonthIsVariable)
+                        {
+                            // TODO: end of year
+                            p[3] = now.Month + 1;
+                        }
+                        else if (every.YearIsVariable)
+                        {
+                            p[4] = now.Year + 1;
+                        }
+
+                        break;
+                    case "month":
+                        // TODO: end of year
+                        p[3] = now.Month + 1;
+                        break;
+                    case "year":
+                        p[4] = now.Year + 1;
+                        break;
+                }
+            }
+            else if (date.Hour < now.Hour)
+            {
+                switch (every.Interval)
+                {
+                    case "minute":
+                        if (every.DayIsVariable)
+                        {
+                            // TODO: end of month
+                            p[2] = now.Day + 1;
+                        }
+                        else if (every.MonthIsVariable)
+                        {
+                            // TODO: end of year
+                            p[3] = now.Month + 1;
+                        }
+
+                        break;
+                    case "hour":
+                        // TODO: end of day
+                        p[1] = now.Hour;
+                        break;
+                    case "day":
+                        // TODO: end of month
+                        p[2] = now.Day + 1;
+                        break;
+                    case "month":
+                        // TODO: end of year
+                        p[3] = now.Month + 1;
+                        break;
+                    case "year":
+                        p[4] = now.Year + 1;
+                        break;
+                }
+            }
+            else if (date.Minute < now.Minute)
+            {
+                switch (every.Interval)
+                {
+                    case "minute":
+                        // TODO: end of hour
+                        p[0] = now.Minute + 1;
+                        break;
+                    case "hour":
+                        // TODO: end of day
+                        p[1] = now.Hour + 1;
+                        break;
+                    case "day":
+                        // TODO: end of month
+                        p[2] = now.Day + 1;
+                        break;
+                    case "month":
+                        // TODO: end of year
+                        p[3] = now.Month + 1;
+                        break;
+                    case "year":
+                        p[4] = now.Year + 1;
+                        break;
+                }
+            }
+
+            date = new DateTime(p[4], p[3], p[2], p[1], p[0], 0);
+
+            return date;
+        }
     }
 }

+ 1 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotificationProxy.csproj

@@ -113,6 +113,7 @@
     <Compile Include="LocalNotificationProxy.cs" />
     <Compile Include="LocalNotification\Builder.cs" />
     <Compile Include="LocalNotification\Button.cs" />
+    <Compile Include="LocalNotification\Every.cs" />
     <Compile Include="LocalNotification\Notification.cs" />
     <Compile Include="LocalNotification\IAction.cs" />
     <Compile Include="LocalNotification\Input.cs" />

BIN
src/windows/lib.UW/ARM/LocalNotificationProxy.winmd


BIN
src/windows/lib.UW/x64/LocalNotificationProxy.winmd


BIN
src/windows/lib.UW/x86/LocalNotificationProxy.winmd