Sebastián Katzer 8 жил өмнө
parent
commit
b01a137542

+ 54 - 17
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Builder.cs

@@ -26,24 +26,24 @@ namespace LocalNotificationProxy.LocalNotification
 
     internal class Builder
     {
-        /// <summary>
-        /// Notification properties
-        /// </summary>
-        private Options options;
-
         /// <summary>
         /// Initializes a new instance of the <see cref="Builder"/> class.
         /// </summary>
         /// <param name="options">Notification properties to set.</param>
         internal Builder(Options options)
         {
-            this.options = options;
+            this.Content = new Content(options);
         }
 
+        /// <summary>
+        /// Gets content
+        /// </summary>
+        public Content Content { get; private set; }
+
         /// <summary>
         /// Gets options
         /// </summary>
-        public Options Options { get => this.options; }
+        private Options Options { get => this.Content.Options; }
 
         /// <summary>
         /// Build a toast notification specified by the options.
@@ -51,10 +51,24 @@ namespace LocalNotificationProxy.LocalNotification
         /// <returns>A fully configured toast notification instance.</returns>
         public ScheduledToastNotification Build()
         {
-            var toast = new ToastContent()
+            var toast = this.InitToast();
+
+            this.AddAttachments(toast);
+            this.AddActions(toast);
+
+            return this.GetNotification(toast);
+        }
+
+        /// <summary>
+        /// Gets the initialize skeleton for a toast notification.
+        /// </summary>
+        /// <returns>Basic skeleton with sound, image and text.</returns>
+        private ToastContent InitToast()
+        {
+            return new ToastContent()
             {
-                Launch = this.Options.Identifier,
-                Audio = this.Options.ToastAudio,
+                Launch = this.Content.GetXml(),
+                Audio = this.Content.Sound,
 
                 Visual = new ToastVisual()
                 {
@@ -73,33 +87,56 @@ namespace LocalNotificationProxy.LocalNotification
                             }
                         },
 
-                        AppLogoOverride = this.Options.ToastLogo
+                        AppLogoOverride = this.Content.Image
                     }
                 },
 
                 Actions = new ToastActionsCustom()
                 {
-                    Buttons = { }
+                    Buttons = { },
+                    Inputs = { }
                 }
             };
+        }
 
-            foreach (var image in this.Options.ImageAttachments)
+        /// <summary>
+        /// Adds attachments to the toast.
+        /// </summary>
+        /// <param name="toast">Tho toast to extend for.</param>
+        private void AddAttachments(ToastContent toast)
+        {
+            foreach (var image in this.Content.Attachments)
             {
                 toast.Visual.BindingGeneric.Children.Add(image);
             }
+        }
 
-            foreach (var btn in this.Options.ToastButtons)
+        /// <summary>
+        /// Adds buttons and input fields to the toast.
+        /// </summary>
+        /// <param name="toast">Tho toast to extend for.</param>
+        private void AddActions(ToastContent toast)
+        {
+            foreach (var btn in this.Content.Buttons)
             {
                 (toast.Actions as ToastActionsCustom).Buttons.Add(btn);
             }
+        }
 
+        /// <summary>
+        /// Converts the toast into a notification.
+        /// </summary>
+        /// <param name="toast">The toast to convert.</param>
+        /// <returns>A notification ready to schedule.</returns>
+        private ScheduledToastNotification GetNotification(ToastContent toast)
+        {
             var xml = toast.GetXml();
-            var at = this.Options.TriggerDate;
+            var at = this.Content.Date;
             ScheduledToastNotification notification;
 
-            if (this.Options.IsRepeating())
+            if (this.Content.IsRepeating())
             {
-                var interval = this.Options.RepeatInterval;
+                var interval = this.Content.Interval;
                 notification = new ScheduledToastNotification(xml, at, interval, 5);
             }
             else

+ 243 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Content.cs

@@ -0,0 +1,243 @@
+namespace LocalNotificationProxy.LocalNotification
+{
+    using System;
+    using System.Collections.Generic;
+    using Microsoft.Toolkit.Uwp.Notifications;
+    using Windows.Data.Xml.Dom;
+
+    internal class Content
+    {
+        /// <summary>
+        /// Initializes a new instance of the <see cref="Content"/> class.
+        /// </summary>
+        /// <param name="options">The options hash map from JS side.</param>
+        public Content(Options options)
+        {
+            this.Options = options;
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="Content"/> class.
+        /// </summary>
+        /// <param name="xml">The options as a xml string.</param>
+        public Content(string xml)
+        {
+            this.Options = Options.Parse(xml);
+        }
+
+        public Options Options { get; private set; }
+
+        /// <summary>
+        /// Gets a ToastAudio object based on the specified sound uri.
+        /// </summary>
+        public ToastAudio Sound
+        {
+            get
+            {
+                var sound = new ToastAudio();
+                var path = this.Options.Sound;
+
+                if (path == null || path.Length == 0 || path.Equals("false"))
+                {
+                    sound.Silent = true;
+                }
+                else
+                if (path.StartsWith("file:///") || path.StartsWith("http"))
+                {
+                    sound.Src = new Uri(path, UriKind.Absolute);
+                }
+                else
+                if (path.StartsWith("file://"))
+                {
+                    sound.Src = new Uri(path.Replace("file:/", "ms-appx:///www"));
+                }
+                else
+                if (path.StartsWith("res://"))
+                {
+                    sound.Src = new Uri(path.Replace("res://", "ms-winsoundevent:notification."));
+                }
+                else
+                if (path.StartsWith("app://"))
+                {
+                    sound.Src = new Uri(path.Replace("app:/", "ms-appdata://"));
+                }
+
+                return sound;
+            }
+        }
+
+        /// <summary>
+        /// Gets a GenericAppLogo object based on the specified icon uri.
+        /// </summary>
+        public ToastGenericAppLogo Image
+        {
+            get
+            {
+                var image = new ToastGenericAppLogo();
+                var path = this.Options.Image;
+
+                if (path == null || path.StartsWith("res://logo"))
+                {
+                    image.Source = string.Empty;
+                }
+                else
+                if (path.StartsWith("file:///") || path.StartsWith("http"))
+                {
+                    image.Source = path;
+                }
+                else
+                if (path.StartsWith("file://"))
+                {
+                    image.Source = path.Replace("file:/", "ms-appx:///www");
+                }
+                else
+                if (path.StartsWith("res://"))
+                {
+                    image.Source = path.Replace("res://", "ms-appx:///images");
+                }
+                else
+                if (path.StartsWith("app://"))
+                {
+                    image.Source = path.Replace("app:/", "ms-appdata://local");
+                }
+                else
+                {
+                    image.Source = string.Empty;
+                }
+
+                if (image.Source.EndsWith("?crop=none"))
+                {
+                    image.HintCrop = ToastGenericAppLogoCrop.None;
+                }
+                else
+                if (image.Source.EndsWith("?crop=cirlce"))
+                {
+                    image.HintCrop = ToastGenericAppLogoCrop.Circle;
+                }
+
+                return image;
+            }
+        }
+
+        /// <summary>
+        /// Gets the parsed image attachments.
+        /// </summary>
+        public List<AdaptiveImage> Attachments
+        {
+            get
+            {
+                var images = new List<AdaptiveImage>();
+
+                if (this.Options.Attachments == null)
+                {
+                    return images;
+                }
+
+                foreach (string path in this.Options.Attachments)
+                {
+                    var image = new AdaptiveImage();
+
+                    if (path.StartsWith("file:///") || path.StartsWith("http"))
+                    {
+                        image.Source = path;
+                    }
+                    else
+                    if (path.StartsWith("file://"))
+                    {
+                        image.Source = path.Replace("file:/", "ms-appx:///www");
+                    }
+                    else
+                    if (path.StartsWith("res://"))
+                    {
+                        image.Source = path.Replace("res://", "ms-appx:///images");
+                    }
+                    else
+                    if (path.StartsWith("app://"))
+                    {
+                        image.Source = path.Replace("app:/", "ms-appdata://local");
+                    }
+
+                    if (image.Source != null)
+                    {
+                        images.Add(image);
+                    }
+                }
+
+                return images;
+            }
+        }
+
+        /// <summary>
+        /// Gets all toast buttons.
+        /// </summary>
+        public List<ToastButton> Buttons
+        {
+            get
+            {
+                var buttons = new List<ToastButton>();
+
+                foreach (var action in this.Options.Buttons)
+                {
+                    buttons.Add(action.ToastButton);
+                }
+
+                return buttons;
+            }
+        }
+
+        /// <summary>
+        /// Gets the date when to trigger the notification.
+        /// </summary>
+        public DateTime Date
+        {
+            get
+            {
+                var date = DateTimeOffset.FromUnixTimeMilliseconds(this.Options.At * 1000).LocalDateTime;
+                var minDate = DateTime.Now.AddSeconds(0.1);
+
+                return (date < minDate) ? minDate : date;
+            }
+        }
+
+        /// <summary>
+        /// Gets the parsed repeat interval.
+        /// </summary>
+        public TimeSpan Interval
+        {
+            get
+            {
+                switch (this.Options.Every)
+                {
+                    case "minute":
+                        return new TimeSpan(TimeSpan.TicksPerMinute);
+                    case "hour":
+                        return new TimeSpan(TimeSpan.TicksPerHour);
+                    case "day":
+                        return new TimeSpan(TimeSpan.TicksPerDay);
+                    default:
+                        return TimeSpan.Zero;
+                }
+            }
+        }
+
+        /// <summary>
+        /// Gets the instance as an serialized xml element.
+        /// </summary>
+        /// <returns>Element with all property values set as attributes.</returns>
+        public string GetXml()
+        {
+            return this.Options.GetXml();
+        }
+
+        /// <summary>
+        /// If the notification shall be repeating.
+        /// </summary>
+        /// <returns>True if the Every property has some value.</returns>
+        public bool IsRepeating()
+        {
+            var every = this.Options.Every;
+
+            return every != null && every.Length > 0 && !every.Equals("0");
+        }
+    }
+}

+ 40 - 245
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Options.cs

@@ -83,247 +83,6 @@ namespace LocalNotificationProxy.LocalNotification
         /// </summary>
         public Button[] Buttons { get; set; }
 
-        /// <summary>
-        /// Gets the date when to trigger the notification.
-        /// </summary>
-        internal DateTime TriggerDate
-        {
-            get
-            {
-                var date = DateTimeOffset.FromUnixTimeMilliseconds(this.At * 1000).LocalDateTime;
-                var minDate = DateTime.Now.AddSeconds(0.1);
-
-                return (date < minDate) ? minDate : date;
-            }
-        }
-
-        /// <summary>
-        /// Gets the parsed repeat interval.
-        /// </summary>
-        internal TimeSpan RepeatInterval
-        {
-            get
-            {
-                switch (this.Every)
-                {
-                    case "minute":
-                        return new TimeSpan(TimeSpan.TicksPerMinute);
-                    case "hour":
-                        return new TimeSpan(TimeSpan.TicksPerHour);
-                    case "day":
-                        return new TimeSpan(TimeSpan.TicksPerDay);
-                    default:
-                        return TimeSpan.Zero;
-                }
-            }
-        }
-
-        /// <summary>
-        /// Gets a ToastAudio object based on the specified sound uri.
-        /// </summary>
-        internal ToastAudio ToastAudio
-        {
-            get
-            {
-                var sound = new ToastAudio();
-                string path = this.Sound;
-
-                if (path == null || path.Length == 0 || path.Equals("false"))
-                {
-                    sound.Silent = true;
-                }
-                else
-                if (path.StartsWith("file:///") || path.StartsWith("http"))
-                {
-                    sound.Src = new System.Uri(path, System.UriKind.Absolute);
-                }
-                else
-                if (path.StartsWith("file://"))
-                {
-                    sound.Src = new System.Uri(path.Replace("file:/", "ms-appx:///www"));
-                }
-                else
-                if (path.StartsWith("res://"))
-                {
-                    sound.Src = new System.Uri(path.Replace("res://", "ms-winsoundevent:notification."));
-                }
-                else
-                if (path.StartsWith("app://"))
-                {
-                    sound.Src = new System.Uri(path.Replace("app:/", "ms-appdata://"));
-                }
-
-                return sound;
-            }
-        }
-
-        /// <summary>
-        /// Gets a GenericAppLogo object based on the specified icon uri.
-        /// </summary>
-        internal ToastGenericAppLogo ToastLogo
-        {
-            get
-            {
-                var image = new ToastGenericAppLogo();
-                string path = this.Image;
-
-                if (path == null || path.StartsWith("res://logo"))
-                {
-                    image.Source = string.Empty;
-                }
-                else
-                if (path.StartsWith("file:///") || path.StartsWith("http"))
-                {
-                    image.Source = path;
-                }
-                else
-                if (path.StartsWith("file://"))
-                {
-                    image.Source = path.Replace("file:/", "ms-appx:///www");
-                }
-                else
-                if (path.StartsWith("res://"))
-                {
-                    image.Source = path.Replace("res://", "ms-appx:///images");
-                }
-                else
-                if (path.StartsWith("app://"))
-                {
-                    image.Source = path.Replace("app:/", "ms-appdata://local");
-                }
-                else
-                {
-                    image.Source = string.Empty;
-                }
-
-                if (image.Source.EndsWith("?crop=none"))
-                {
-                    image.HintCrop = ToastGenericAppLogoCrop.None;
-                }
-                else
-                if (image.Source.EndsWith("?crop=cirlce"))
-                {
-                    image.HintCrop = ToastGenericAppLogoCrop.Circle;
-                }
-
-                return image;
-            }
-        }
-
-        /// <summary>
-        /// Gets the instance as an serialized xml element.
-        /// </summary>
-        /// <returns>Element with all property values set as attributes.</returns>
-        internal string Identifier
-        {
-            get
-            {
-                var node = new XmlDocument().CreateElement("options");
-
-                node.SetAttribute("id", this.ID.ToString());
-                node.SetAttribute("badge", this.Badge.ToString());
-                node.SetAttribute("at", this.At.ToString());
-
-                if (this.Title != null)
-                {
-                    node.SetAttribute("title", this.Title);
-                }
-
-                if (this.Text != null)
-                {
-                    node.SetAttribute("text", this.Text);
-                }
-
-                if (this.Sound != null)
-                {
-                    node.SetAttribute("sound", this.Sound);
-                }
-
-                if (this.Image != null)
-                {
-                    node.SetAttribute("image", this.Image);
-                }
-
-                if (this.Every != null)
-                {
-                    node.SetAttribute("every", this.Every);
-                }
-
-                if (this.Data != null)
-                {
-                    node.SetAttribute("data", this.Data);
-                }
-
-                return node.GetXml();
-            }
-        }
-
-        /// <summary>
-        /// Gets the parsed image attachments.
-        /// </summary>
-        internal List<AdaptiveImage> ImageAttachments
-        {
-            get
-            {
-                var images = new List<AdaptiveImage>();
-
-                if (this.Attachments == null)
-                {
-                    return images;
-                }
-
-                foreach (string path in this.Attachments)
-                {
-                    var image = new AdaptiveImage();
-
-                    if (path.StartsWith("file:///") || path.StartsWith("http"))
-                    {
-                        image.Source = path;
-                    }
-                    else
-                    if (path.StartsWith("file://"))
-                    {
-                        image.Source = path.Replace("file:/", "ms-appx:///www");
-                    }
-                    else
-                    if (path.StartsWith("res://"))
-                    {
-                        image.Source = path.Replace("res://", "ms-appx:///images");
-                    }
-                    else
-                    if (path.StartsWith("app://"))
-                    {
-                        image.Source = path.Replace("app:/", "ms-appdata://local");
-                    }
-
-                    if (image.Source != null)
-                    {
-                        images.Add(image);
-                    }
-                }
-
-                return images;
-            }
-        }
-
-        /// <summary>
-        /// Gets all toast buttons.
-        /// </summary>
-        internal List<ToastButton> ToastButtons
-        {
-            get
-            {
-                var buttons = new List<ToastButton>();
-
-                foreach (var action in this.Buttons)
-                {
-                    buttons.Add(action.ToastButton);
-                }
-
-                return buttons;
-            }
-        }
-
         /// <summary>
         /// Deserializes the XML string into an instance of Options.
         /// </summary>
@@ -375,12 +134,48 @@ namespace LocalNotificationProxy.LocalNotification
         }
 
         /// <summary>
-        /// If the notification shall be repeating.
+        /// Gets the instance as an serialized xml element.
         /// </summary>
-        /// <returns>True if the Every property has some value.</returns>
-        internal bool IsRepeating()
+        /// <returns>Element with all property values set as attributes.</returns>
+        public string GetXml()
         {
-            return this.Every != null && this.Every.Length > 0 && !this.Every.Equals("0");
+            var node = new XmlDocument().CreateElement("options");
+
+            node.SetAttribute("id", this.ID.ToString());
+            node.SetAttribute("badge", this.Badge.ToString());
+            node.SetAttribute("at", this.At.ToString());
+
+            if (this.Title != null)
+            {
+                node.SetAttribute("title", this.Title);
+            }
+
+            if (this.Text != null)
+            {
+                node.SetAttribute("text", this.Text);
+            }
+
+            if (this.Sound != null)
+            {
+                node.SetAttribute("sound", this.Sound);
+            }
+
+            if (this.Image != null)
+            {
+                node.SetAttribute("image", this.Image);
+            }
+
+            if (this.Every != null)
+            {
+                node.SetAttribute("every", this.Every);
+            }
+
+            if (this.Data != null)
+            {
+                node.SetAttribute("data", this.Data);
+            }
+
+            return node.GetXml();
         }
     }
 }

+ 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\Content.cs" />
     <Compile Include="LocalNotification\Options.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>