Commit ad102c3e by Sebastián Katzer

Support for input actions

parent 78bf5452
...@@ -60,33 +60,34 @@ exports.request = function (success, error) { ...@@ -60,33 +60,34 @@ exports.request = function (success, error) {
* @return [ Void ] * @return [ Void ]
*/ */
exports.schedule = function (success, error, args) { exports.schedule = function (success, error, args) {
var options = [], buttons = []; var options = [], actions = [];
for (var i = 0, props, opts; i < args.length; i++) { for (var i = 0, props, opts; i < args.length; i++) {
props = args[i]; props = args[i];
opts = new LocalNotification.Options(); opts = new LocalNotification.Options();
for (var prop in opts) { for (var prop in opts) {
if (props[prop]) { if (prop != 'actions' && props[prop]) opts[prop] = 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]; action = props.actions[j];
if (!action.type || action.type == 'button') { if (!action.type || action.type == 'button') {
var button = new LocalNotification.Button(); btn = new LocalNotification.Button();
} else
button.id = action.id; if (action.type == 'input') {
button.title = action.title; btn = new LocalNotification.Input();
button.launch = action.launch; }
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); options.push(opts);
} }
...@@ -103,17 +104,29 @@ exports.schedule = function (success, error, args) { ...@@ -103,17 +104,29 @@ exports.schedule = function (success, error, args) {
* *
* @return [ Void ] * @return [ Void ]
*/ */
exports.clicked = function (xml) { exports.clicked = function (xml, input) {
var toast = LocalNotification.Options.parse(xml), 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 // Handle onclick event
document.addEventListener('activated', function (e) { document.addEventListener('activated', function (e) {
if (e.kind == ActivationKind.toastNotification) { if (e.kind == ActivationKind.toastNotification) {
exports.clicked(e.raw.argument); exports.clicked(e.raw.argument, e.raw.userInput);
} }
}, false); }, false);
......
...@@ -117,6 +117,11 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -117,6 +117,11 @@ namespace LocalNotificationProxy.LocalNotification
/// <param name="toast">Tho toast to extend for.</param> /// <param name="toast">Tho toast to extend for.</param>
private void AddActions(ToastContent toast) 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) foreach (var btn in this.Content.Buttons)
{ {
(toast.Actions as ToastActionsCustom).Buttons.Add(btn); (toast.Actions as ToastActionsCustom).Buttons.Add(btn);
......
...@@ -21,23 +21,21 @@ ...@@ -21,23 +21,21 @@
namespace LocalNotificationProxy.LocalNotification namespace LocalNotificationProxy.LocalNotification
{ {
using Microsoft.Toolkit.Uwp.Notifications; public sealed class Button : IAction
public sealed class Button
{ {
/// <summary> /// <summary>
/// Gets or sets button ID. /// Gets or sets the ID.
/// </summary> /// </summary>
public string ID { get; set; } public string ID { get; set; }
/// <summary> /// <summary>
/// Gets or sets button title. /// Gets or sets the title.
/// </summary> /// </summary>
public string Title { get; set; } public string Title { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether to launch the app. /// Gets or sets a value indicating whether to launch the app.
/// </summary> /// </summary>
public bool Launch { get; set; } public bool Launch { get; set; } = true;
} }
} }
...@@ -176,20 +176,60 @@ ...@@ -176,20 +176,60 @@
{ {
var buttons = new List<ToastButton>(); var buttons = new List<ToastButton>();
foreach (var action in this.Options.Buttons) foreach (var action in this.Options.Actions)
{ {
buttons.Add( if (action is Button)
new ToastButton(action.Title, this.Options.GetXml(action.ID)) {
buttons.Add(new ToastButton(action.Title, this.Options.GetXml(action.ID))
{ {
ActivationType = action.Launch ? ToastActivationType.Foreground : ToastActivationType.Background 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; return buttons;
} }
} }
/// <summary> /// <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. /// Gets the date when to trigger the notification.
/// </summary> /// </summary>
public DateTime Date public DateTime Date
......
/*
* 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; }
}
}
/*
* 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;
}
}
...@@ -81,9 +81,9 @@ namespace LocalNotificationProxy.LocalNotification ...@@ -81,9 +81,9 @@ namespace LocalNotificationProxy.LocalNotification
public string[] Attachments { get; set; } public string[] Attachments { get; set; }
/// <summary> /// <summary>
/// Gets or sets the notification buttons. /// Gets or sets the notification actions.
/// </summary> /// </summary>
public Button[] Buttons { get; set; } public IAction[] Actions { get; set; }
/// <summary> /// <summary>
/// Deserializes the XML string into an instance of Options. /// Deserializes the XML string into an instance of Options.
......
...@@ -114,6 +114,8 @@ ...@@ -114,6 +114,8 @@
<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\Content.cs" />
<Compile Include="LocalNotification\IAction.cs" />
<Compile Include="LocalNotification\Input.cs" />
<Compile Include="LocalNotification\Options.cs" /> <Compile Include="LocalNotification\Options.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
......
...@@ -180,14 +180,10 @@ exports.convertActions = function (options) { ...@@ -180,14 +180,10 @@ exports.convertActions = function (options) {
if (!options.actions) if (!options.actions)
return null; return null;
var MAX_ACTIONS = (device.platform === 'iOS') ? 4 : 3, var actions = [];
actions = [];
if (options.actions.length > MAX_ACTIONS) for (var i = 0, action; i < options.actions.length; i++) {
console.warn('Count of actions exceeded count of ' + MAX_ACTIONS); action = options.actions[i];
for (var i = 0; i < options.actions.length && MAX_ACTIONS > 0; i++) {
var action = options.actions[i];
if (!action.id) { if (!action.id) {
console.warn( console.warn(
...@@ -196,10 +192,8 @@ exports.convertActions = function (options) { ...@@ -196,10 +192,8 @@ exports.convertActions = function (options) {
} }
action.id = action.id.toString(); action.id = action.id.toString();
action.title = (action.title || action.id).toString();
actions.push(action); actions.push(action);
MAX_ACTIONS--;
} }
options.category = (options.category || 'DEFAULT_GROUP').toString(); options.category = (options.category || 'DEFAULT_GROUP').toString();
......
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