Files
Electron.NET/ElectronNET.API/Notification.cs

147 lines
5.0 KiB
C#
Raw Normal View History

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;
using ElectronNET.API.Interfaces;
namespace ElectronNET.API
{
/// <summary>
/// Create OS desktop notifications
/// </summary>
public sealed class Notification : INotification
{
private static Notification _notification;
2021-09-15 11:14:45 +02:00
private static readonly object _syncRoot = new();
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();
}
}
}
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>
public void Show(NotificationOptions notificationOptions)
{
2017-10-18 03:49:34 +02:00
GenerateIDsForDefinedActions(notificationOptions);
BridgeConnector.Emit("createNotification", JObject.FromObject(notificationOptions, _jsonSerializer));
}
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-09-15 11:14:45 +02:00
private static readonly JsonSerializer _jsonSerializer = new()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
};
}
}