2017-10-15 17:03:07 +02:00
|
|
|
|
using ElectronNET.API.Entities;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
2017-10-18 03:49:34 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2017-10-15 17:03:07 +02:00
|
|
|
|
|
|
|
|
|
|
namespace ElectronNET.API
|
|
|
|
|
|
{
|
2017-10-24 21:43:27 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create OS desktop notifications
|
|
|
|
|
|
/// </summary>
|
2017-10-15 17:03:07 +02:00
|
|
|
|
public sealed class Notification
|
|
|
|
|
|
{
|
|
|
|
|
|
private static Notification _notification;
|
2021-09-15 11:14:45 +02:00
|
|
|
|
private static readonly object _syncRoot = new();
|
2017-10-15 17:03:07 +02:00
|
|
|
|
|
|
|
|
|
|
internal Notification() { }
|
|
|
|
|
|
|
|
|
|
|
|
internal static Notification Instance
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_notification == null)
|
|
|
|
|
|
{
|
2017-11-04 00:16:14 +01:00
|
|
|
|
lock (_syncRoot)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_notification == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_notification = new Notification();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-15 17:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _notification;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-15 11:14:45 +02:00
|
|
|
|
private static readonly List<NotificationOptions> _notificationOptions = new();
|
2017-10-18 03:49:34 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create OS desktop notifications
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="notificationOptions"></param>
|
2017-10-15 17:03:07 +02:00
|
|
|
|
public void Show(NotificationOptions notificationOptions)
|
|
|
|
|
|
{
|
2017-10-18 03:49:34 +02:00
|
|
|
|
GenerateIDsForDefinedActions(notificationOptions);
|
|
|
|
|
|
|
2021-08-26 16:08:11 +02:00
|
|
|
|
BridgeConnector.Emit("createNotification", JObject.FromObject(notificationOptions, _jsonSerializer));
|
2017-10-15 17:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-18 03:49:34 +02:00
|
|
|
|
private static void GenerateIDsForDefinedActions(NotificationOptions notificationOptions)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool isActionDefined = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (notificationOptions.OnShow != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
notificationOptions.ShowID = Guid.NewGuid().ToString();
|
|
|
|
|
|
isActionDefined = true;
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("NotificationEventShow");
|
|
|
|
|
|
BridgeConnector.On<string>("NotificationEventShow", (id) => {
|
|
|
|
|
|
_notificationOptions.Single(x => x.ShowID == id).OnShow();
|
2017-10-18 03:49:34 +02:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (notificationOptions.OnClick != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
notificationOptions.ClickID = Guid.NewGuid().ToString();
|
|
|
|
|
|
isActionDefined = true;
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("NotificationEventClick");
|
|
|
|
|
|
BridgeConnector.On<string>("NotificationEventClick", (id) => {
|
|
|
|
|
|
_notificationOptions.Single(x => x.ClickID == id).OnClick();
|
2017-10-18 03:49:34 +02:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (notificationOptions.OnClose != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
notificationOptions.CloseID = Guid.NewGuid().ToString();
|
|
|
|
|
|
isActionDefined = true;
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("NotificationEventClose");
|
|
|
|
|
|
BridgeConnector.On<string>("NotificationEventClose", (id) => {
|
2017-10-18 03:49:34 +02:00
|
|
|
|
_notificationOptions.Single(x => x.CloseID == id.ToString()).OnClose();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (notificationOptions.OnReply != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
notificationOptions.ReplyID = Guid.NewGuid().ToString();
|
|
|
|
|
|
isActionDefined = true;
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("NotificationEventReply");
|
|
|
|
|
|
BridgeConnector.On<string[]>("NotificationEventReply", (args) => {
|
|
|
|
|
|
_notificationOptions.Single(x => x.ReplyID == args[0].ToString()).OnReply(args[1].ToString());
|
2017-10-18 03:49:34 +02:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (notificationOptions.OnAction != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
notificationOptions.ActionID = Guid.NewGuid().ToString();
|
|
|
|
|
|
isActionDefined = true;
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("NotificationEventAction");
|
|
|
|
|
|
BridgeConnector.On<string[]>("NotificationEventAction", (args) => {
|
|
|
|
|
|
_notificationOptions.Single(x => x.ReplyID == args[0].ToString()).OnAction(args[1].ToString());
|
2017-10-18 03:49:34 +02:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (isActionDefined)
|
|
|
|
|
|
{
|
|
|
|
|
|
_notificationOptions.Add(notificationOptions);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Whether or not desktop notifications are supported on the current system.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public Task<bool> IsSupportedAsync()
|
|
|
|
|
|
{
|
2021-08-17 16:28:07 +02:00
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
2017-10-18 03:49:34 +02:00
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.On<bool>("notificationIsSupportedComplete", (isSupported) =>
|
2017-10-18 03:49:34 +02:00
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("notificationIsSupportedComplete");
|
2021-07-12 21:33:35 +02:00
|
|
|
|
taskCompletionSource.SetResult(isSupported);
|
2017-10-18 03:49:34 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("notificationIsSupported");
|
2017-10-18 03:49:34 +02:00
|
|
|
|
|
|
|
|
|
|
return taskCompletionSource.Task;
|
|
|
|
|
|
}
|
2021-08-26 16:08:11 +02:00
|
|
|
|
|
2021-09-15 11:14:45 +02:00
|
|
|
|
private static readonly JsonSerializer _jsonSerializer = new()
|
2021-08-26 16:08:11 +02:00
|
|
|
|
{
|
|
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
|
|
|
|
DefaultValueHandling = DefaultValueHandling.Ignore
|
|
|
|
|
|
};
|
2017-10-15 17:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|