Przeglądaj źródła

Add (de)serializer for every (windows)

Sebastián Katzer 8 lat temu
rodzic
commit
759ff1dedf

+ 120 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Toast/Every.cs

@@ -21,6 +21,9 @@
 
 namespace LocalNotificationProxy.LocalNotification.Toast
 {
+    using System;
+    using Windows.Data.Xml.Dom;
+
     public sealed class Every
     {
         /// <summary>
@@ -72,5 +75,122 @@ namespace LocalNotificationProxy.LocalNotification.Toast
         /// Gets or sets the year.
         /// </summary>
         public int? Year { get; set; }
+
+        /// <summary>
+        /// Deserializes the XML string into an instance of Every.
+        /// </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 Every Parse(string xml)
+        {
+            var doc = new XmlDocument();
+            doc.LoadXml(xml);
+
+            var every = new Every();
+            var node = doc.DocumentElement;
+
+            if (node.GetAttributeNode("minute") != null)
+            {
+                every.Minute = Convert.ToInt32(node.GetAttribute("minute"));
+            }
+
+            if (node.GetAttributeNode("hour") != null)
+            {
+                every.Hour = Convert.ToInt32(node.GetAttribute("hour"));
+            }
+
+            if (node.GetAttributeNode("day") != null)
+            {
+                every.Day = Convert.ToInt32(node.GetAttribute("day"));
+            }
+
+            if (node.GetAttributeNode("weekday") != null)
+            {
+                every.Weekday = Convert.ToInt32(node.GetAttribute("weekday"));
+            }
+
+            if (node.GetAttributeNode("week") != null)
+            {
+                every.Week = Convert.ToInt32(node.GetAttribute("week"));
+            }
+
+            if (node.GetAttributeNode("weekdayordinal") != null)
+            {
+                every.WeekdayOrdinal = Convert.ToInt32(node.GetAttribute("weekdayOrdinal"));
+            }
+
+            if (node.GetAttributeNode("weekOfMonth") != null)
+            {
+                every.WeekOfMonth = Convert.ToInt32(node.GetAttribute("weekOfMonth"));
+            }
+
+            if (node.GetAttributeNode("month") != null)
+            {
+                every.Month = Convert.ToInt32(node.GetAttribute("month"));
+            }
+
+            if (node.GetAttributeNode("year") != null)
+            {
+                every.Year = Convert.ToInt32(node.GetAttribute("year"));
+            }
+
+            return every;
+        }
+
+        /// <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("every");
+
+            if (this.Minute.HasValue)
+            {
+                node.SetAttribute("minute", this.Minute.ToString());
+            }
+
+            if (this.Hour.HasValue)
+            {
+                node.SetAttribute("hour", this.Hour.ToString());
+            }
+
+            if (this.Day.HasValue)
+            {
+                node.SetAttribute("day", this.Day.ToString());
+            }
+
+            if (this.Weekday.HasValue)
+            {
+                node.SetAttribute("weekday", this.Weekday.ToString());
+            }
+
+            if (this.Week.HasValue)
+            {
+                node.SetAttribute("week", this.Week.ToString());
+            }
+
+            if (this.WeekdayOrdinal.HasValue)
+            {
+                node.SetAttribute("weekdayOrdinal", this.WeekdayOrdinal.ToString());
+            }
+
+            if (this.WeekOfMonth.HasValue)
+            {
+                node.SetAttribute("weekOfMonth", this.WeekOfMonth.ToString());
+            }
+
+            if (this.Month.HasValue)
+            {
+                node.SetAttribute("month", this.Month.ToString());
+            }
+
+            if (this.Year.HasValue)
+            {
+                node.SetAttribute("year", this.Year.ToString());
+            }
+
+            return node.GetXml();
+        }
     }
 }

+ 15 - 4
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Toast/Trigger.cs

@@ -97,9 +97,13 @@ namespace LocalNotificationProxy.LocalNotification.Toast
                 trigger.Unit = node.GetAttribute("unit");
             }
 
-            if (node.GetAttributeNode("every") != null)
+            if (node.GetAttributeNode("strEvery") != null)
             {
-                trigger.Every = node.GetAttribute("every");
+                trigger.Every = node.GetAttribute("strEvery");
+            }
+            else if (node.GetAttributeNode("hshEvery") != null)
+            {
+                trigger.Every = Toast.Every.Parse(node.GetAttribute("hshEvery"));
             }
 
             return trigger;
@@ -125,9 +129,16 @@ namespace LocalNotificationProxy.LocalNotification.Toast
                 node.SetAttribute("unit", this.Unit);
             }
 
-            if (this.Every != null && !(this.Every is Every))
+            if (this.Every != null)
             {
-                node.SetAttribute("every", this.Every.ToString());
+                if (this.Every is Every)
+                {
+                    node.SetAttribute("hshEvery", (this.Every as Every).GetXml());
+                }
+                else
+                {
+                    node.SetAttribute("strEvery", this.Every.ToString());
+                }
             }
 
             return node.GetXml();