Commit ad102c3e by Sebastián Katzer

Support for input actions

parent 78bf5452
......@@ -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);
......
......@@ -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);
......
......@@ -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;
}
}
......@@ -176,13 +176,25 @@
{
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;
......@@ -190,6 +202,34 @@
}
/// <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>
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
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.
......
......@@ -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>
......
......@@ -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();
......
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