Browse Source

Support for progressBar options for Windows

Sebastián Katzer 8 years ago
parent
commit
d1e647cc01

+ 38 - 2
src/windows/LocalNotificationProxy.js

@@ -87,7 +87,20 @@ exports.schedule = function (success, error, args) {
  * @return [ Void ]
  */
 exports.update = function (success, error, args) {
-    console.warn('LocalNotification#update is not implemented.');
+    var options = [];
+
+    for (var i = 0, props, opts; i < args.length; i++) {
+        props = args[i];
+        opts  = exports.parseOptions(props);
+        options.push(opts);
+    }
+
+    impl.update(options);
+
+    for (i = 0; i < options.length; i++) {
+        exports.fireEvent('update', options[i]);
+    }
+
     success();
 };
 
@@ -362,7 +375,7 @@ exports.clone = function (obj) {
  */
 exports.parseOptions = function (obj) {
     var opts   = new LocalNotification.Options(),
-        ignore = ['actions', 'trigger'];
+        ignore = ['progressBar', 'actions', 'trigger'];
 
     for (var prop in opts) {
         if (!ignore.includes(prop) && obj[prop]) {
@@ -370,6 +383,9 @@ exports.parseOptions = function (obj) {
         }
     }
 
+    var progressBar  = exports.parseProgressBar(obj);
+    opts.progressBar = progressBar;
+
     var trigger  = exports.parseTrigger(obj);
     opts.trigger = trigger;
 
@@ -433,6 +449,26 @@ exports.parseActions = function (obj) {
     return actions;
 };
 
+/**
+ * Parse progressBar specs into instances of prefered types.
+ *
+ * @param [ Object ] obj The notification options map.
+ *
+ * @return [ LocalNotification.ProgressBar ]
+ */
+exports.parseProgressBar = function (obj) {
+    var bar  = new LocalNotification.ProgressBar(),
+        spec = obj.progressBar;
+
+    if (!spec) return bar;
+
+    for (var prop in bar) {
+        if (spec[prop]) bar[prop] = spec[prop];
+    }
+
+    return bar;
+};
+
 // Handle onclick event
 document.addEventListener('activated', function (e) {
     if (e.kind == ActivationKind.toastNotification) {

+ 15 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Builder.cs

@@ -58,6 +58,7 @@ namespace LocalNotificationProxy.LocalNotification
         {
             var toast = this.InitToast();
 
+            this.AddProgressBarToToast(toast);
             this.AddAttachmentsToToast(toast);
             this.AddActionsToToast(toast);
 
@@ -115,6 +116,20 @@ namespace LocalNotificationProxy.LocalNotification
             };
         }
 
+        /// <summary>
+        /// Adds optional progress bar to the toast.
+        /// </summary>
+        /// <param name="toast">Tho toast to extend for.</param>
+        private void AddProgressBarToToast(ToastContent toast)
+        {
+            var progressBar = this.Content.ProgressBar;
+
+            if (progressBar != null)
+            {
+                toast.Visual.BindingGeneric.Children.Add(progressBar);
+            }
+        }
+
         /// <summary>
         /// Adds attachments to the toast.
         /// </summary>

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

@@ -58,6 +58,31 @@ namespace LocalNotificationProxy.LocalNotification
             }
         }
 
+        /// <summary>
+        /// Update notifications.
+        /// </summary>
+        /// <param name="notifications">List of key-value properties</param>
+        public void Update([ReadOnlyArray] Options[] notifications)
+        {
+            foreach (Options options in notifications)
+            {
+                var bar = options.ProgressBar;
+
+                if (!bar.Enabled)
+                {
+                    continue;
+                }
+
+                var data = new NotificationData { SequenceNumber = 0 };
+
+                data.Values["progress.value"] = bar.Value.ToString();
+                data.Values["progress.status"] = bar.Status.ToString();
+                data.Values["progress.description"] = bar.Description.ToString();
+
+                ToastNotifier.Update(data, options.Id.ToString());
+            }
+        }
+
         /// <summary>
         /// Clear the notifications specified by id.
         /// </summary>

+ 24 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Notification.cs

@@ -261,6 +261,30 @@
             }
         }
 
+        /// <summary>
+        /// Gets the progress bar widget.
+        /// </summary>
+        public AdaptiveProgressBar ProgressBar
+        {
+            get
+            {
+                var bar = this.Options.ProgressBar;
+
+                if (!bar.Enabled)
+                {
+                    return null;
+                }
+
+                return new AdaptiveProgressBar()
+                {
+                    Title = bar.Title,
+                    Value = new BindableProgressBarValue("progress.value"),
+                    ValueStringOverride = new BindableString("progress.description"),
+                    Status = new BindableString("progress.status")
+                };
+            }
+        }
+
         /// <summary>
         /// Gets the date when to trigger the notification.
         /// </summary>

+ 6 - 1
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Options.cs

@@ -33,7 +33,7 @@ namespace LocalNotificationProxy.LocalNotification
         /// <summary>
         /// Gets or sets notification ID.
         /// </summary>
-        public int Id { get; set; }
+        public int Id { get; set; } = 0;
 
         /// <summary>
         /// Gets or sets notification title.
@@ -85,6 +85,11 @@ namespace LocalNotificationProxy.LocalNotification
         /// </summary>
         public IAction[] Actions { get; set; }
 
+        /// <summary>
+        /// Gets or sets the notification progress bar.
+        /// </summary>
+        public ProgressBar ProgressBar { get; set; }
+
         /// <summary>
         /// Deserializes the XML string into an instance of Options.
         /// </summary>

+ 51 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/ProgressBar.cs

@@ -0,0 +1,51 @@
+/*
+ * 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
+{
+    public sealed class ProgressBar
+    {
+        /// <summary>
+        /// Gets or sets a value indicating whether the notification has a progress bar.
+        /// </summary>
+        public bool Enabled { get; set; } = false;
+
+        /// <summary>
+        /// Gets or sets the title.
+        /// </summary>
+        public string Title { get; set; } = string.Empty;
+
+        /// <summary>
+        /// Gets or sets the value.
+        /// </summary>
+        public double Value { get; set; } = 0;
+
+        /// <summary>
+        /// Gets or sets the status.
+        /// </summary>
+        public string Status { get; set; } = string.Empty;
+
+        /// <summary>
+        /// Gets or sets the description.
+        /// </summary>
+        public string Description { get; set; } = string.Empty;
+    }
+}

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

@@ -49,6 +49,15 @@ namespace LocalNotificationProxy
             this.manager.Schedule(notifications);
         }
 
+        /// <summary>
+        /// Update notifications.
+        /// </summary>
+        /// <param name="notifications">List of key-value properties</param>
+        public void Update([ReadOnlyArray] Options[] notifications)
+        {
+            this.manager.Update(notifications);
+        }
+
         /// <summary>
         /// Clear the notifications specified by id.
         /// </summary>

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

@@ -118,6 +118,7 @@
     <Compile Include="LocalNotification\Input.cs" />
     <Compile Include="LocalNotification\Manager.cs" />
     <Compile Include="LocalNotification\Options.cs" />
+    <Compile Include="LocalNotification\ProgressBar.cs" />
     <Compile Include="LocalNotification\Trigger.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

+ 31 - 6
www/local-notification-util.js

@@ -30,11 +30,12 @@ exports._defaults = {
     sound:   'res://platform_default',
     badge:   undefined,
     data:    undefined,
-    silent:  false,
-    actions: [],
+    icon:    undefined,
     trigger: { type: 'calendar' },
+    actions: [],
     actionGroupId: undefined,
-    attachments: []
+    attachments: [],
+    progressBar: false
 };
 
 // Listener
@@ -142,6 +143,7 @@ exports.convertProperties = function (options) {
 
     this.convertTrigger(options);
     this.convertActions(options);
+    this.convertProgressBar(options);
 
     return options;
 };
@@ -202,9 +204,7 @@ exports.convertTrigger = function (options) {
     var isCal = trigger.type == 'calendar';
 
     if (isCal && !date) {
-        date = this.getValueFor(options, 'at', 'firstAt', 'date');
-        date = date || isObject && !cfg.getTime ? new Date() : options.trigger;
-        date = date || new Date();
+        date = this.getValueFor(options, 'at', 'firstAt', 'date') || new Date();
     }
 
     if (isCal) {
@@ -216,6 +216,14 @@ exports.convertTrigger = function (options) {
         trigger.every = options.every;
     }
 
+    if (!trigger.count && device.platform == 'windows') {
+        trigger.count = trigger.every ? 5 : 1;
+    }
+
+    if (trigger.every && device.platform == 'windows') {
+        trigger.every = trigger.every.toString();
+    }
+
     if (!isCal) {
         trigger.notifyOnEntry = !!trigger.notifyOnEntry;
         trigger.notifyOnExit  = trigger.notifyOnExit === true;
@@ -232,6 +240,23 @@ exports.convertTrigger = function (options) {
     return options;
 };
 
+/**
+ * Convert the passed values for the progressBar to their required type.
+ *
+ * @param [ Map ] options Set of custom values.
+ *
+ * @return [ Map ] Interaction object with trigger spec.
+ */
+exports.convertProgressBar = function (options) {
+    var cfg = options.progressBar;
+
+    if (typeof cfg === 'boolean') {
+        options.progressBar = { enabled: cfg };
+    }
+
+    return options;
+};
+
 /**
  * Create a callback function to get executed within a specific scope.
  *