Skip to content

Commit

Permalink
Add StaticActivityPublisher (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrwiss committed Mar 6, 2020
1 parent 4e1c27a commit 5ac6615
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Notifier/Notifier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="UI\Events\ActivityEventArgs.cs" />
<Compile Include="UI\Events\StaticActivityPublisher.cs" />
<Compile Include="UI\Windows\NotifierTrayIcon.cs" />
<Compile Include="UI\Windows\NotificationWindow.xaml.cs">
<DependentUpon>NotificationWindow.xaml</DependentUpon>
Expand Down
28 changes: 28 additions & 0 deletions Notifier/UI/Events/ActivityEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wokhan.WindowsFirewallNotifier.Notifier.Helpers;

namespace Wokhan.WindowsFirewallNotifier.Notifier.UI.Events
{
public class ActivityEventArgs : EventArgs
{
public ActivityEnum Activity { get; set; }

public CurrentConn CurrentConnection { get; set; }

public DateTime TimeStamp { get; } = DateTime.Now;

public enum ActivityEnum
{
Allow, Block, AllowTemp, BlockTemp
}
public ActivityEventArgs(ActivityEnum activity, CurrentConn currentConnection)
{
Activity = activity;
CurrentConnection = currentConnection;
}
}
}
58 changes: 58 additions & 0 deletions Notifier/UI/Events/StaticActivityPublisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wokhan.WindowsFirewallNotifier.Notifier.Helpers;

namespace Wokhan.WindowsFirewallNotifier.Notifier.UI.Events
{
public interface IActivitySender
{
string Name { get; set; }
}

/*
* Static activity event publisher.
* See:
* https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-publish-events-that-conform-to-net-framework-guidelines
*/
public static class StaticActivityPublisher
{
public delegate void ActivityEventHandler<TActivityEventArgs>(IActivitySender sender, ActivityEventArgs args);

public static event ActivityEventHandler<ActivityEventArgs> ActivityEvent;

public static void Publish(IActivitySender sender, ActivityEventArgs.ActivityEnum activity, CurrentConn currentConnection)
{
ActivityEventArgs args = new ActivityEventArgs(activity, currentConnection);
OnRaiseActivityEvent(sender, args);
}

public static void Subscribe(ActivityEventHandler<ActivityEventArgs> eventHandler)
{
ActivityEvent += eventHandler;
}

// Wrap event invocations inside a protected virtual method
// to allow derived classes to override the event invocation behavior
private static void OnRaiseActivityEvent(IActivitySender sender, ActivityEventArgs args)
{
//// Make a temporary copy of the event to avoid possibility of
//// a race condition if the last subscriber unsubscribes
//// immediately after the null check and before the event is raised.
//EventHandler<ActivityEventArgs> handler = RaiseActivityEvent;

//// Event will be null if there are no subscribers
//if (handler != null)
//{
// //// Format the string to send inside the CustomEventArgs parameter
// //e.Message += $" at {DateTime.Now}";

// // Use the () operator to raise the event.
// handler(sender, e);
//}
ActivityEvent?.Invoke(sender, args);
}
}
}

0 comments on commit 5ac6615

Please sign in to comment.