Commit 3cb3b51e by Sebastián Katzer

Add support for action groups for Windows

parent 5759483e
......@@ -68,7 +68,6 @@ exports.ready = function () {
*/
exports.check = function (success, error) {
var granted = impl.hasPermission();
success(granted);
};
......@@ -213,7 +212,6 @@ exports.cancelAll = function (success, error) {
*/
exports.type = function (success, error, args) {
var type = impl.type(args[0]);
success(type);
};
......@@ -222,40 +220,12 @@ exports.type = function (success, error, args) {
*
* @param [ Function ] success Success callback
* @param [ Function ] error Error callback
* @param [ Array ] args Interface arguments
*
* @return [ Void ]
*/
exports.ids = function (success, error) {
var ids = impl.ids() || [];
success(Array.from(ids));
};
/**
* List of all scheduled notification ids.
*
* @param [ Function ] success Success callback
* @param [ Function ] error Error callback
*
* @return [ Void ]
*/
exports.scheduledIds = function (success, error) {
var ids = impl.scheduledIds() || [];
success(Array.from(ids));
};
/**
* List of all triggered notification ids.
*
* @param [ Function ] success Success callback
* @param [ Function ] error Error callback
*
* @return [ Void ]
*/
exports.triggeredIds = function (success, error) {
var ids = impl.triggeredIds() || [];
exports.ids = function (success, error, args) {
var ids = impl.ids(args[0]) || [];
success(Array.from(ids));
};
......@@ -270,7 +240,6 @@ exports.triggeredIds = function (success, error) {
*/
exports.notification = function (success, error, args) {
var obj = impl.notification(args[0]);
success(exports.clone(obj));
};
......@@ -284,37 +253,42 @@ exports.notification = function (success, error, args) {
* @return [ Void ]
*/
exports.notifications = function (success, error, args) {
var objs = impl.notifications(args) || [];
var objs = impl.notifications(args[0], args[1]) || [];
success(exports.cloneAll(objs));
};
/**
* List of all scheduled notifications.
*
* @param [ Function ] success Success callback
* @param [ Function ] error Error callback
*
* @return [ Void ]
*/
exports.scheduledNotifications = function (success, error) {
var objs = impl.scheduledNotifications() || [];
success(exports.cloneAll(objs));
};
/**
* List of all triggered notifications.
* Manage action groups.
*
* @param [ Function ] success Success callback
* @param [ Function ] error Error callback
* @param [ Array ] args Interface arguments
*
* @return [ Void ]
*/
exports.triggeredNotifications = function (success, error) {
var objs = impl.triggeredNotifications() || [];
exports.actions = function (success, error, args) {
var ActionGroup = LocalNotification.ActionGroup,
code = args[0],
id = args[1],
res = [],
opts, group;
switch (code) {
case 0:
opts = exports.parseOptions(args[2]);
group = new ActionGroup(id, opts.actions);
ActionGroup.register(group);
break;
case 1:
ActionGroup.unregister(id);
break;
case 2:
res.push(ActionGroup.isRegistered(id));
break;
}
success(exports.cloneAll(objs));
success.apply(this, res);
};
/**
......@@ -375,6 +349,10 @@ exports.fireEvent = function (event, toast, data) {
exports.cloneAll = function (objs) {
var clones = [];
if (!Array.isArray(objs)) {
objs = Array.from(objs);
}
for (var obj of objs) {
clones.push(exports.clone(obj));
}
......@@ -488,11 +466,17 @@ exports.parseEvery = function (spec) {
* @return [ Array<LocalNotification.Action> ]
*/
exports.parseActions = function (obj) {
var actions = [], btn;
var spec = obj.actions,
actions = [], btn;
if (!obj.actions) return actions;
if (!spec) return actions;
if (typeof spec === 'string') {
var group = LocalNotification.ActionGroup.lookup(spec);
return group ? group.actions : actions;
}
for (var action of obj.actions) {
for (var action of spec) {
if (!action.type || action.type == 'button') {
btn = new LocalNotification.Toast.Button();
} else
......
/*
* 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
{
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using global::LocalNotificationProxy.LocalNotification.Toast;
public sealed class ActionGroup
{
/// <summary>
/// Saves all groups for later lookup.
/// </summary>
private static Dictionary<string, ActionGroup> groups = new Dictionary<string, ActionGroup>();
/// <summary>
/// Initializes a new instance of the <see cref="ActionGroup"/> class.
/// </summary>
/// <param name="id">The ID of the action group.</param>
/// <param name="actions">The list of actions to group for.</param>
public ActionGroup(string id, [ReadOnlyArray] IAction[] actions)
{
this.Id = id;
this.Actions = actions;
}
/// <summary>
/// Gets or sets the action group ID.
/// </summary>
public string Id { get; set; }
/// <summary>
/// Gets or sets the notification actions.
/// </summary>
public IAction[] Actions { get; set; }
/// <summary>
/// Lookup the action groups with the specified group id.
/// </summary>
/// <param name="id">The ID of the action group to find.</param>
/// <returns>Null if no group was found.</returns>
public static ActionGroup Lookup(string id)
{
return groups[id];
}
/// <summary>
/// Register the provided set of actions under the specified group id.
/// </summary>
/// <param name="group">The action group to register.</param>
public static void Register(ActionGroup group)
{
groups.Add(group.Id, group);
}
/// <summary>
/// Unregister the action group.
/// </summary>
/// <param name="id">The id of the action group to remove.</param>
public static void Unregister(string id)
{
groups.Remove(id);
}
/// <summary>
/// Check if a action group with that id is registered.
/// </summary>
/// <param name="id">The id of the action group to check for.</param>
/// <returns>True if a group with the ID could be found.</returns>
public static bool IsRegistered(string id)
{
return groups.ContainsKey(id);
}
}
}
......@@ -21,8 +21,8 @@
namespace LocalNotificationProxy.LocalNotification
{
using Microsoft.Toolkit.Uwp.Notifications;
using System.Diagnostics;
using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications;
internal class Builder
......
......@@ -107,28 +107,26 @@ namespace LocalNotificationProxy
/// <summary>
/// List of all notifiation by id.
/// </summary>
/// <param name="code">The type of the notifications to return for.</param>
/// <returns>List of numbers</returns>
public int[] Ids()
public int[] Ids(int code)
{
return this.manager.GetIds().ToArray();
}
var type = Notification.Type.Unknown;
/// <summary>
/// List of all scheduled notifiation by id.
/// </summary>
/// <returns>List of numbers</returns>
public int[] ScheduledIds()
switch (code)
{
return this.manager.GetIdsByType(Notification.Type.Scheduled).ToArray();
case 0:
type = Notification.Type.All;
break;
case 1:
type = Notification.Type.Scheduled;
break;
case 2:
type = Notification.Type.Triggered;
break;
}
/// <summary>
/// List of all triggered notifiation by id.
/// </summary>
/// <returns>List of numbers</returns>
public int[] TriggeredIds()
{
return this.manager.GetIdsByType(Notification.Type.Triggered).ToArray();
return this.manager.GetIdsByType(type).ToArray();
}
#pragma warning disable SA1300 // Element must begin with upper-case letter
......@@ -148,34 +146,29 @@ namespace LocalNotificationProxy
/// <summary>
/// List of (all) notifiation specified by id.
/// </summary>
/// <param name="code">The type of the notifications to return for.</param>
/// <param name="ids">Optional list of IDs to find.</param>
/// <returns>List of options instances</returns>
public Options[] Notifications([ReadOnlyArray] int[] ids)
{
if (ids == null || ids.Length == 0)
{
return this.manager.GetOptions().ToArray();
}
public Options[] Notifications(int code, [ReadOnlyArray] int[] ids)
{
var type = Notification.Type.Unknown;
switch (code)
{
case 0:
type = Notification.Type.All;
break;
case 1:
type = Notification.Type.Scheduled;
break;
case 2:
type = Notification.Type.Triggered;
break;
case 3:
return this.manager.GetOptions(ids).ToArray();
}
/// <summary>
/// List of all scheduled notifiation.
/// </summary>
/// <returns>List of options instances</returns>
public Options[] ScheduledNotifications()
{
return this.manager.GetOptionsByType(Notification.Type.Scheduled).ToArray();
}
/// <summary>
/// List of all triggered notifiation.
/// </summary>
/// <returns>List of options instances</returns>
public Options[] TriggeredNotifications()
{
return this.manager.GetOptionsByType(Notification.Type.Triggered).ToArray();
return this.manager.GetOptionsByType(type).ToArray();
}
}
}
......@@ -111,6 +111,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="LocalNotificationProxy.cs" />
<Compile Include="LocalNotification\ActionGroup.cs" />
<Compile Include="LocalNotification\Builder.cs" />
<Compile Include="LocalNotification\Request.cs" />
<Compile Include="LocalNotification\Toast\Button.cs" />
......
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