Ver código fonte

Serialize/Deserialize trigger option on windows

Sebastián Katzer 8 anos atrás
pai
commit
4abccaf32d

+ 4 - 1
src/windows/LocalNotificationProxy.js

@@ -393,14 +393,17 @@ exports.cloneAll = function (objs) {
  */
 exports.clone = function (obj) {
     var ignore = ['action'],
+        dclone = ['trigger'],
         clone  = {};
 
+    if (obj === null) return null;
+
     for (var prop in obj) {
         if (ignore.includes(prop) || typeof obj[prop] === 'function')
             continue;
 
         try {
-            clone[prop] = obj[prop];
+            clone[prop] = dclone.includes(prop) ? exports.clone(obj[prop]) : obj[prop];
         } catch (e) {
             clone[prop] = null;
         }

+ 2 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Options.cs

@@ -105,6 +105,7 @@ namespace LocalNotificationProxy.LocalNotification
 
             options.Id = int.Parse(node.GetAttribute("id"));
             options.Badge = int.Parse(node.GetAttribute("badge"));
+            options.Trigger = Trigger.Parse(node.GetAttribute("trigger"));
 
             if (node.GetAttributeNode("text") != null)
             {
@@ -169,6 +170,7 @@ namespace LocalNotificationProxy.LocalNotification
 
             node.SetAttribute("id", this.Id.ToString());
             node.SetAttribute("badge", this.Badge.ToString());
+            node.SetAttribute("trigger", this.Trigger.GetXml());
 
             if (this.Title != null)
             {

+ 49 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Trigger.cs

@@ -19,6 +19,8 @@
  * limitations under the License.
  */
 
+using Windows.Data.Xml.Dom;
+
 namespace LocalNotificationProxy.LocalNotification
 {
     public sealed class Trigger
@@ -52,5 +54,52 @@ namespace LocalNotificationProxy.LocalNotification
         /// Gets or sets trigger interval.
         /// </summary>
         public string Every { get; set; }
+
+        /// <summary>
+        /// Deserializes the XML string into an instance of Trigger.
+        /// </summary>
+        /// <param name="xml">The serialized instance of Options as an xml string.</param>
+        /// <returns>An instance where all properties have been assigned.</returns>
+        internal static Trigger Parse(string xml)
+        {
+            var doc = new XmlDocument();
+            doc.LoadXml(xml);
+
+            var trigger = new Trigger();
+            var node = doc.DocumentElement;
+
+            trigger.At = long.Parse(node.GetAttribute("at"));
+            trigger.In = long.Parse(node.GetAttribute("in"));
+            trigger.Count = int.Parse(node.GetAttribute("count"));
+            trigger.Occurrence = int.Parse(node.GetAttribute("occurrence"));
+
+            if (node.GetAttributeNode("every") != null)
+            {
+                trigger.Every = node.GetAttribute("every");
+            }
+
+            return trigger;
+        }
+
+        /// <summary>
+        /// Gets the instance as an serialized xml element.
+        /// </summary>
+        /// <returns>Element with all property values set as attributes.</returns>
+        internal string GetXml()
+        {
+            var node = new XmlDocument().CreateElement("trigger");
+
+            node.SetAttribute("at", this.At.ToString());
+            node.SetAttribute("in", this.In.ToString());
+            node.SetAttribute("count", this.Count.ToString());
+            node.SetAttribute("occurrence", this.Occurrence.ToString());
+
+            if (this.Every != null)
+            {
+                node.SetAttribute("every", this.Every);
+            }
+
+            return node.GetXml();
+        }
     }
 }