Commit b01a1375 by Sebastián Katzer

Code refacturing

parent 15bb160f
...@@ -27,23 +27,23 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -27,23 +27,23 @@ namespace LocalNotificationProxy.LocalNotification
internal class Builder internal class Builder
{ {
/// <summary> /// <summary>
/// Notification properties
/// </summary>
private Options options;
/// <summary>
/// Initializes a new instance of the <see cref="Builder"/> class. /// Initializes a new instance of the <see cref="Builder"/> class.
/// </summary> /// </summary>
/// <param name="options">Notification properties to set.</param> /// <param name="options">Notification properties to set.</param>
internal Builder(Options options) internal Builder(Options options)
{ {
this.options = options; this.Content = new Content(options);
} }
/// <summary> /// <summary>
/// Gets content
/// </summary>
public Content Content { get; private set; }
/// <summary>
/// Gets options /// Gets options
/// </summary> /// </summary>
public Options Options { get => this.options; } private Options Options { get => this.Content.Options; }
/// <summary> /// <summary>
/// Build a toast notification specified by the options. /// Build a toast notification specified by the options.
...@@ -51,10 +51,24 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -51,10 +51,24 @@ namespace LocalNotificationProxy.LocalNotification
/// <returns>A fully configured toast notification instance.</returns> /// <returns>A fully configured toast notification instance.</returns>
public ScheduledToastNotification Build() 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, Launch = this.Content.GetXml(),
Audio = this.Options.ToastAudio, Audio = this.Content.Sound,
Visual = new ToastVisual() Visual = new ToastVisual()
{ {
...@@ -73,33 +87,56 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -73,33 +87,56 @@ namespace LocalNotificationProxy.LocalNotification
} }
}, },
AppLogoOverride = this.Options.ToastLogo AppLogoOverride = this.Content.Image
} }
}, },
Actions = new ToastActionsCustom() 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); 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); (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 xml = toast.GetXml();
var at = this.Options.TriggerDate; var at = this.Content.Date;
ScheduledToastNotification notification; 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); notification = new ScheduledToastNotification(xml, at, interval, 5);
} }
else else
......
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");
}
}
}
...@@ -113,6 +113,7 @@ ...@@ -113,6 +113,7 @@
<Compile Include="LocalNotificationProxy.cs" /> <Compile Include="LocalNotificationProxy.cs" />
<Compile Include="LocalNotification\Builder.cs" /> <Compile Include="LocalNotification\Builder.cs" />
<Compile Include="LocalNotification\Button.cs" /> <Compile Include="LocalNotification\Button.cs" />
<Compile Include="LocalNotification\Content.cs" />
<Compile Include="LocalNotification\Options.cs" /> <Compile Include="LocalNotification\Options.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
......
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