Browse Source

Implemented clear, clearAll, cancel and cancelAll for Windows

Sebastián Katzer 8 years ago
parent
commit
f3f4e4d5aa

+ 38 - 0
src/windows/LocalNotificationProxy.js

@@ -101,6 +101,25 @@ exports.schedule = function (success, error, args) {
     success();
 };
 
+/**
+ * Clear the notifications specified by id.
+ *
+ * @param [ Function ] success Success callback
+ * @param [ Function ] error   Error callback
+ * @param [ Array ]    args    Interface arguments
+ *
+ * @return [ Void ]
+ */
+exports.clear = function (success, error, args) {
+    var toasts = impl.clear(args) || [];
+
+    for (var i = 0; i < toasts.length; i++) {
+        exports.fireEvent('clear', toasts[i]);
+    }
+
+    success();
+};
+
 /**
  * Clear all notifications.
  *
@@ -115,6 +134,25 @@ exports.clearAll = function (success, error) {
     success();
 };
 
+/**
+ * Cancel the notifications specified by id.
+ *
+ * @param [ Function ] success Success callback
+ * @param [ Function ] error   Error callback
+ * @param [ Array ]    args    Interface arguments
+ *
+ * @return [ Void ]
+ */
+exports.cancel = function (success, error, args) {
+    var toasts = impl.cancel(args) || [];
+
+    for (var i = 0; i < toasts.length; i++) {
+        exports.fireEvent('cancel', toasts[i]);
+    }
+
+    success();
+};
+
 /**
  * Cancel all notifications.
  *

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

@@ -51,6 +51,96 @@ namespace LocalNotificationProxy.LocalNotification
             }
         }
 
+        /// <summary>
+        /// Clear the notifications specified by id.
+        /// </summary>
+        /// <param name="ids">The IDs of the notification to clear.</param>
+        /// <returns>The cleared notifications.</returns>
+        public List<Options> Clear(int[] ids)
+        {
+            var triggered = ToastNotificationManager.History.GetHistory();
+            var tags = new List<int>(ids);
+            var toasts = new List<Options>();
+
+            foreach (var toast in triggered)
+            {
+                var id = int.Parse(toast.Tag);
+
+                if (tags.Contains(id))
+                {
+                    tags.Remove(id);
+                    toasts.Add(new Notification(toast).Options);
+                    ToastNotificationManager.History.Remove(toast.Tag);
+                }
+
+                if (tags.Count == 0)
+                {
+                    break;
+                }
+            }
+
+            return toasts;
+        }
+
+        /// <summary>
+        /// Clear all notifications.
+        /// </summary>
+        public void ClearAll()
+        {
+            ToastNotificationManager.History.Clear();
+        }
+
+        /// <summary>
+        /// Cancel the notifications specified by id.
+        /// </summary>
+        /// <param name="ids">The IDs of the notification to clear.</param>
+        /// <returns>The cleared notifications.</returns>
+        public List<Options> Cancel(int[] ids)
+        {
+            var scheduled = ToastNotifier.GetScheduledToastNotifications();
+            var tags = new List<int>(ids);
+            var toasts = new List<Options>();
+
+            foreach (var toast in scheduled)
+            {
+                var id = int.Parse(toast.Tag);
+
+                if (tags.Contains(id))
+                {
+                    tags.Remove(id);
+                    toasts.Add(new Notification(toast).Options);
+                    ToastNotifier.RemoveFromSchedule(toast);
+                }
+
+                if (tags.Count == 0)
+                {
+                    break;
+                }
+            }
+
+            if (tags.Count > 0)
+            {
+                toasts.AddRange(this.Clear(tags.ToArray()));
+            }
+
+            return toasts;
+        }
+
+        /// <summary>
+        /// Cancel all notifications.
+        /// </summary>
+        public void CancelAll()
+        {
+            var toasts = ToastNotifier.GetScheduledToastNotifications();
+
+            foreach (var toast in toasts)
+            {
+                ToastNotifier.RemoveFromSchedule(toast);
+            }
+
+            this.ClearAll();
+        }
+
         /// <summary>
         /// Gets all notifications by id.
         /// </summary>

+ 36 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotificationProxy.cs

@@ -49,6 +49,42 @@ namespace LocalNotificationProxy
             this.manager.Schedule(notifications);
         }
 
+        /// <summary>
+        /// Clear the notifications specified by id.
+        /// </summary>
+        /// <param name="ids">The IDs of the notification to clear.</param>
+        /// <returns>The cleared notifications.</returns>
+        public Options[] Clear([ReadOnlyArray] int[] ids)
+        {
+            return this.manager.Clear(ids).ToArray();
+        }
+
+        /// <summary>
+        /// Clear all notifications.
+        /// </summary>
+        public void ClearAll()
+        {
+            this.manager.ClearAll();
+        }
+
+        /// <summary>
+        /// Cancel the notifications specified by id.
+        /// </summary>
+        /// <param name="ids">The IDs of the notification to clear.</param>
+        /// <returns>The cleared notifications.</returns>
+        public Options[] Cancel([ReadOnlyArray] int[] ids)
+        {
+            return this.manager.Cancel(ids).ToArray();
+        }
+
+        /// <summary>
+        /// Cancel all notifications.
+        /// </summary>
+        public void CancelAll()
+        {
+            this.manager.CancelAll();
+        }
+
         /// <summary>
         /// Gets the type of the notification specified by ID.
         /// </summary>