浏览代码

Added ids(), triggeredIds() and scheduledIds() for Windows

Sebastián Katzer 8 年之前
父节点
当前提交
25a4cdcd5c

+ 42 - 0
src/windows/LocalNotificationProxy.js

@@ -97,6 +97,48 @@ exports.schedule = function (success, error, args) {
     success();
 };
 
+/**
+ * List of all notification ids.
+ *
+ * @param [ Function ] success Success callback
+ * @param [ Function ] error   Error callback
+ *
+ * @return [ Void ]
+ */
+exports.ids = function (success, error) {
+    var ids = impl.ids() || [];
+
+    success(ids);
+};
+
+/**
+ * List of all scheduled notification ids.
+ *
+ * @param [ Function ] success Success callback
+ * @param [ Function ] error   Error callback
+ *
+ * @return [ Void ]
+ */
+exports.scheduledIds = function (success, error) {
+    var ids = impl.scheduledIds() || [];
+
+    success(ids);
+};
+
+/**
+ * List of all triggered notification ids.
+ *
+ * @param [ Function ] success Success callback
+ * @param [ Function ] error   Error callback
+ *
+ * @return [ Void ]
+ */
+exports.triggeredIds = function (success, error) {
+    var ids = impl.triggeredIds() || [];
+
+    success(ids);
+};
+
 /**
  * Inform the user through the click event that a notification was clicked.
  *

+ 8 - 8
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Builder.cs

@@ -32,13 +32,13 @@ namespace LocalNotificationProxy.LocalNotification
         /// <param name="options">Notification properties to set.</param>
         internal Builder(Options options)
         {
-            this.Content = new Content(options);
+            this.Content = new Notification(options);
         }
 
         /// <summary>
         /// Gets content
         /// </summary>
-        public Content Content { get; private set; }
+        public Notification Content { get; private set; }
 
         /// <summary>
         /// Gets options
@@ -53,10 +53,10 @@ namespace LocalNotificationProxy.LocalNotification
         {
             var toast = this.InitToast();
 
-            this.AddAttachments(toast);
-            this.AddActions(toast);
+            this.AddAttachmentsToToast(toast);
+            this.AddActionsToToast(toast);
 
-            return this.GetNotification(toast);
+            return this.ConvertToastToNotification(toast);
         }
 
         /// <summary>
@@ -103,7 +103,7 @@ namespace LocalNotificationProxy.LocalNotification
         /// Adds attachments to the toast.
         /// </summary>
         /// <param name="toast">Tho toast to extend for.</param>
-        private void AddAttachments(ToastContent toast)
+        private void AddAttachmentsToToast(ToastContent toast)
         {
             foreach (var image in this.Content.Attachments)
             {
@@ -115,7 +115,7 @@ namespace LocalNotificationProxy.LocalNotification
         /// Adds buttons and input fields to the toast.
         /// </summary>
         /// <param name="toast">Tho toast to extend for.</param>
-        private void AddActions(ToastContent toast)
+        private void AddActionsToToast(ToastContent toast)
         {
             foreach (var btn in this.Content.Inputs)
             {
@@ -133,7 +133,7 @@ namespace LocalNotificationProxy.LocalNotification
         /// </summary>
         /// <param name="toast">The toast to convert.</param>
         /// <returns>A notification ready to schedule.</returns>
-        private ScheduledToastNotification GetNotification(ToastContent toast)
+        private ScheduledToastNotification ConvertToastToNotification(ToastContent toast)
         {
             var xml = toast.GetXml();
             var at = this.Content.Date;

+ 113 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Manager.cs

@@ -0,0 +1,113 @@
+/*
+ * 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.Collections.Generic;
+    using System.Runtime.InteropServices.WindowsRuntime;
+    using Windows.UI.Notifications;
+
+    internal class Manager
+    {
+        /// <summary>
+        /// Gets a value indicating whether the permission to schedule notifications is enabled.
+        /// </summary>
+        /// <returns>True if settings are enabled</returns>
+        public bool Enabled => ToastNotifier.Setting == NotificationSetting.Enabled;
+
+        /// <summary>
+        /// Gets the default toast notifier.
+        /// </summary>
+        internal static ToastNotifier ToastNotifier => ToastNotificationManager.CreateToastNotifier();
+
+        /// <summary>
+        /// Schedule notifications.
+        /// </summary>
+        /// <param name="notifications">List of key-value properties</param>
+        public void Schedule([ReadOnlyArray] Options[] notifications)
+        {
+            foreach (Options options in notifications)
+            {
+                var toast = new Builder(options).Build();
+                ToastNotifier.AddToSchedule(toast);
+            }
+        }
+
+        /// <summary>
+        /// Gets all notifications by id.
+        /// </summary>
+        /// <returns>List of ids</returns>
+        public List<int> GetIds()
+        {
+            return this.GetIdsByType(Notification.Type.All);
+        }
+
+        /// <summary>
+        /// Gets all notifications by id for given type.
+        /// </summary>
+        /// <param name="type">The type of notification.</param>
+        /// <returns>List of ids</returns>
+        public List<int> GetIdsByType(Notification.Type type)
+        {
+            var ids = new List<int>();
+
+            if (type == Notification.Type.All || type == Notification.Type.Scheduled)
+            {
+                var toasts = ToastNotifier.GetScheduledToastNotifications();
+
+                foreach (var toast in toasts)
+                {
+                    ids.Add(int.Parse(toast.Tag));
+                }
+            }
+
+            if (type == Notification.Type.All || type == Notification.Type.Triggered)
+            {
+                var toasts = ToastNotificationManager.History.GetHistory();
+
+                foreach (var toast in toasts)
+                {
+                    ids.Add(int.Parse(toast.Tag));
+                }
+            }
+
+            return ids;
+        }
+
+        /// <summary>
+        /// Gets the notification by ID.
+        /// </summary>
+        /// <param name="id">The ID of the notification to find.</param>
+        /// <returns>The found instance or null.</returns>
+        private Notification Get(string id)
+        {
+            foreach (var toast in ToastNotifier.GetScheduledToastNotifications())
+            {
+                if (toast.Id == id)
+                {
+                    return new Notification(toast);
+                }
+            }
+
+            return null;
+        }
+    }
+}

+ 20 - 6
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Content.cs → src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Notification.cs

@@ -3,28 +3,42 @@
     using System;
     using System.Collections.Generic;
     using Microsoft.Toolkit.Uwp.Notifications;
-    using Windows.Data.Xml.Dom;
+    using Windows.UI.Notifications;
 
-    internal class Content
+    internal class Notification
     {
         /// <summary>
-        /// Initializes a new instance of the <see cref="Content"/> class.
+        /// Initializes a new instance of the <see cref="Notification"/> class.
         /// </summary>
         /// <param name="options">The options hash map from JS side.</param>
-        public Content(Options options)
+        public Notification(Options options)
         {
             this.Options = options;
         }
 
         /// <summary>
-        /// Initializes a new instance of the <see cref="Content"/> class.
+        /// Initializes a new instance of the <see cref="Notification"/> class.
         /// </summary>
         /// <param name="xml">The options as a xml string.</param>
-        public Content(string xml)
+        public Notification(string xml)
         {
             this.Options = Options.Parse(xml);
         }
 
+        /// <summary>
+        /// Initializes a new instance of the <see cref="Notification"/> class.
+        /// </summary>
+        /// <param name="toast">The options as a toast object.</param>
+        public Notification(ScheduledToastNotification toast)
+        {
+            this.Options = Options.Parse(toast.Content.GetXml());
+        }
+
+        public enum Type
+        {
+            All, Scheduled, Triggered
+        }
+
         public Options Options { get; private set; }
 
         /// <summary>

+ 31 - 9
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotificationProxy.cs

@@ -23,19 +23,18 @@ namespace LocalNotificationProxy
 {
     using System.Runtime.InteropServices.WindowsRuntime;
     using LocalNotification;
-    using Windows.UI.Notifications;
-    using Windows.UI.WebUI;
 
     public sealed class LocalNotificationProxy
     {
+        private Manager manager = new Manager();
+
         /// <summary>
         /// Check permission to schedule notifications.
         /// </summary>
         /// <returns>True if settings are enabled</returns>
         public bool HasPermission()
         {
-            NotificationSetting settings = ToastNotificationManager.CreateToastNotifier().Setting;
-            return settings == NotificationSetting.Enabled;
+            return this.manager.Enabled;
         }
 
         /// <summary>
@@ -44,11 +43,34 @@ namespace LocalNotificationProxy
         /// <param name="notifications">List of key-value properties</param>
         public void Schedule([ReadOnlyArray] Options[] notifications)
         {
-            foreach (Options options in notifications)
-            {
-                ScheduledToastNotification notification = new Builder(options).Build();
-                ToastNotificationManager.CreateToastNotifier().AddToSchedule(notification);
-            }
+            this.manager.Schedule(notifications);
+        }
+
+        /// <summary>
+        /// List of all notifiation by id.
+        /// </summary>
+        /// <returns>List of numbers</returns>
+        public int[] Ids()
+        {
+            return this.manager.GetIds().ToArray();
+        }
+
+        /// <summary>
+        /// List of all scheduled notifiation by id.
+        /// </summary>
+        /// <returns>List of numbers</returns>
+        public int[] ScheduledIds()
+        {
+            return this.manager.GetIdsByType(Notification.Type.Scheduled).ToArray();
+        }
+
+        /// <summary>
+        /// List of all triggered notifiation by id.
+        /// </summary>
+        /// <returns>List of numbers</returns>
+        public int[] TriggeredIds()
+        {
+            return this.manager.GetIdsByType(Notification.Type.Triggered).ToArray();
         }
     }
 }

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

@@ -113,9 +113,10 @@
     <Compile Include="LocalNotificationProxy.cs" />
     <Compile Include="LocalNotification\Builder.cs" />
     <Compile Include="LocalNotification\Button.cs" />
-    <Compile Include="LocalNotification\Content.cs" />
+    <Compile Include="LocalNotification\Notification.cs" />
     <Compile Include="LocalNotification\IAction.cs" />
     <Compile Include="LocalNotification\Input.cs" />
+    <Compile Include="LocalNotification\Manager.cs" />
     <Compile Include="LocalNotification\Options.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>