Эх сурвалжийг харах

Support for input actions

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

+ 29 - 16
src/windows/LocalNotificationProxy.js

@@ -60,33 +60,34 @@ exports.request = function (success, error) {
  * @return [ Void ]
  */
 exports.schedule = function (success, error, args) {
-    var options = [], buttons = [];
+    var options = [], actions = [];
 
     for (var i = 0, props, opts; i < args.length; i++) {
         props = args[i];
         opts  = new LocalNotification.Options();
 
         for (var prop in opts) {
-            if (props[prop]) {
-                opts[prop] = props[prop];
-            }
+            if (prop != 'actions' && props[prop]) opts[prop] = props[prop];
         }
 
-        for (var j = 0, action; j < props.actions.length; j++) {
+        for (var j = 0, action, btn; j < props.actions.length; j++) {
             action = props.actions[j];
 
             if (!action.type || action.type == 'button') {
-                var button = new LocalNotification.Button();
-
-                button.id     = action.id;
-                button.title  = action.title;
-                button.launch = action.launch;
+                btn = new LocalNotification.Button();
+            } else
+            if (action.type == 'input') {
+                btn = new LocalNotification.Input();
+            }
 
-                buttons.push(button);
+            for (prop in btn) {
+                if (action[prop]) btn[prop] = action[prop];
             }
+
+            actions.push(btn);
         }
 
-        opts.buttons = buttons;
+        opts.actions = actions;
 
         options.push(opts);
     }
@@ -103,17 +104,29 @@ exports.schedule = function (success, error, args) {
  *
  * @return [ Void ]
  */
-exports.clicked = function (xml) {
+exports.clicked = function (xml, input) {
     var toast = LocalNotification.Options.parse(xml),
-        event = toast.action || 'click';
+        event = toast.action || 'click',
+        e     = { event: event };
+
+    if (input && input.size > 0) {
+        var it = input.first();
+
+        e.text = it.current.value;
+
+        while (it.hasCurrent) {
+            e[it.current.key] = it.current.value;
+            it.moveNext();
+        }
+    }
 
-    cordova.plugins.notification.local.core.fireEvent(event, toast);
+    cordova.plugins.notification.local.core.fireEvent(event, toast, e);
 };
 
 // Handle onclick event
 document.addEventListener('activated', function (e) {
     if (e.kind == ActivationKind.toastNotification) {
-        exports.clicked(e.raw.argument);
+        exports.clicked(e.raw.argument, e.raw.userInput);
     }
 }, false);
 

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

@@ -117,6 +117,11 @@ namespace LocalNotificationProxy.LocalNotification
         /// <param name="toast">Tho toast to extend for.</param>
         private void AddActions(ToastContent toast)
         {
+            foreach (var btn in this.Content.Inputs)
+            {
+                (toast.Actions as ToastActionsCustom).Inputs.Add(btn);
+            }
+
             foreach (var btn in this.Content.Buttons)
             {
                 (toast.Actions as ToastActionsCustom).Buttons.Add(btn);

+ 4 - 6
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Button.cs

@@ -21,23 +21,21 @@
 
 namespace LocalNotificationProxy.LocalNotification
 {
-    using Microsoft.Toolkit.Uwp.Notifications;
-
-    public sealed class Button
+    public sealed class Button : IAction
     {
         /// <summary>
-        /// Gets or sets button ID.
+        /// Gets or sets the ID.
         /// </summary>
         public string ID { get; set; }
 
         /// <summary>
-        /// Gets or sets button title.
+        /// Gets or sets the title.
         /// </summary>
         public string Title { get; set; }
 
         /// <summary>
         /// Gets or sets a value indicating whether to launch the app.
         /// </summary>
-        public bool Launch { get; set; }
+        public bool Launch { get; set; } = true;
     }
 }

+ 43 - 3
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Content.cs

@@ -176,19 +176,59 @@
             {
                 var buttons = new List<ToastButton>();
 
-                foreach (var action in this.Options.Buttons)
+                foreach (var action in this.Options.Actions)
                 {
-                    buttons.Add(
-                        new ToastButton(action.Title, this.Options.GetXml(action.ID))
+                    if (action is Button)
+                    {
+                        buttons.Add(new ToastButton(action.Title, this.Options.GetXml(action.ID))
                         {
                             ActivationType = action.Launch ? ToastActivationType.Foreground : ToastActivationType.Background
                         });
+                    }
+                    else if (action is Input && (action as Input).SubmitTitle != null)
+                    {
+                        var input = action as Input;
+
+                        buttons.Add(new ToastButton(input.SubmitTitle, this.Options.GetXml(input.ID))
+                        {
+                            ActivationType = input.Launch ? ToastActivationType.Foreground : ToastActivationType.Background,
+                            TextBoxId = input.ID
+                        });
+                    }
                 }
 
                 return buttons;
             }
         }
 
+        /// <summary>
+        /// Gets all toast inputs.
+        /// </summary>
+        public List<ToastTextBox> Inputs
+        {
+            get
+            {
+                var inputs = new List<ToastTextBox>();
+
+                foreach (var action in this.Options.Actions)
+                {
+                    if (!(action is Input))
+                    {
+                        continue;
+                    }
+
+                    inputs.Add(new ToastTextBox(action.ID)
+                    {
+                        Title = action.Title,
+                        PlaceholderContent = (action as Input).EmptyText,
+                        DefaultInput = (action as Input).DefaultValue
+                    });
+                }
+
+                return inputs;
+            }
+        }
+
         /// <summary>
         /// Gets the date when to trigger the notification.
         /// </summary>

+ 41 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/IAction.cs

@@ -0,0 +1,41 @@
+/*
+ * 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 interface IAction
+    {
+        /// <summary>
+        /// Gets or sets the ID.
+        /// </summary>
+        string ID { get; set; }
+
+        /// <summary>
+        /// Gets or sets the title.
+        /// </summary>
+        string Title { get; set; }
+
+        /// <summary>
+        /// Gets or sets a value indicating whether to launch the app.
+        /// </summary>
+        bool Launch { get; set; }
+    }
+}

+ 56 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Input.cs

@@ -0,0 +1,56 @@
+/*
+ * 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 Input : IAction
+    {
+        /// <summary>
+        /// Gets or sets the ID.
+        /// </summary>
+        public string ID { get; set; }
+
+        /// <summary>
+        /// Gets or sets the title.
+        /// </summary>
+        public string Title { get; set; }
+
+        /// <summary>
+        /// Gets or sets the title of the submit button.
+        /// </summary>
+        public string SubmitTitle { get; set; }
+
+        /// <summary>
+        /// Gets or sets placeholder text.
+        /// </summary>
+        public string EmptyText { get; set; }
+
+        /// <summary>
+        /// Gets or sets the default text.
+        /// </summary>
+        public string DefaultValue { get; set; }
+
+        /// <summary>
+        /// Gets or sets a value indicating whether to launch the app.
+        /// </summary>
+        public bool Launch { get; set; } = true;
+    }
+}

+ 2 - 2
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Options.cs

@@ -81,9 +81,9 @@ namespace LocalNotificationProxy.LocalNotification
         public string[] Attachments { get; set; }
 
         /// <summary>
-        /// Gets or sets the notification buttons.
+        /// Gets or sets the notification actions.
         /// </summary>
-        public Button[] Buttons { get; set; }
+        public IAction[] Actions { get; set; }
 
         /// <summary>
         /// Deserializes the XML string into an instance of Options.

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

@@ -114,6 +114,8 @@
     <Compile Include="LocalNotification\Builder.cs" />
     <Compile Include="LocalNotification\Button.cs" />
     <Compile Include="LocalNotification\Content.cs" />
+    <Compile Include="LocalNotification\IAction.cs" />
+    <Compile Include="LocalNotification\Input.cs" />
     <Compile Include="LocalNotification\Options.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

+ 4 - 10
www/local-notification-util.js

@@ -180,14 +180,10 @@ exports.convertActions = function (options) {
     if (!options.actions)
         return null;
 
-    var MAX_ACTIONS = (device.platform === 'iOS') ? 4 : 3,
-        actions     = [];
+    var actions = [];
 
-    if (options.actions.length > MAX_ACTIONS)
-        console.warn('Count of actions exceeded count of ' + MAX_ACTIONS);
-
-    for (var i = 0; i < options.actions.length && MAX_ACTIONS > 0; i++) {
-        var action = options.actions[i];
+    for (var i = 0, action; i < options.actions.length; i++) {
+        action = options.actions[i];
 
         if (!action.id) {
             console.warn(
@@ -195,11 +191,9 @@ exports.convertActions = function (options) {
             continue;
         }
 
-        action.id    = action.id.toString();
-        action.title = (action.title || action.id).toString();
+        action.id = action.id.toString();
 
         actions.push(action);
-        MAX_ACTIONS--;
     }
 
     options.category = (options.category || 'DEFAULT_GROUP').toString();