Skip to content

Commit ffdf037

Browse files
[+] EventMediator
1 parent 03cc0e9 commit ffdf037

File tree

9 files changed

+68
-25
lines changed

9 files changed

+68
-25
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using FWO.Services.EventMediator.Interfaces;
2+
3+
namespace FWO.Services.EventMediator;
4+
5+
public class EventMediator : IEventMediator
6+
{
7+
private readonly Dictionary<Type, List<Action<IEvent>>> _handlers = [];
8+
9+
public void Subscribe<TEvent>(Action<TEvent> handler) where TEvent : class, IEvent
10+
{
11+
if(!_handlers.ContainsKey(typeof(TEvent)))
12+
{
13+
_handlers[typeof(TEvent)] = [];
14+
}
15+
16+
_handlers[typeof(TEvent)].Add(e => handler((TEvent)e));
17+
}
18+
19+
public void Publish<TEvent>(TEvent @event) where TEvent : class, IEvent
20+
{
21+
if(_handlers.TryGetValue(typeof(TEvent), out var handlers))
22+
{
23+
foreach(var handler in handlers)
24+
{
25+
handler(@event);
26+
}
27+
}
28+
}
29+
}
30+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using FWO.Services.EventMediator.Interfaces;
2+
3+
namespace FWO.Services.EventMediator.Events;
4+
5+
public class CollectionChangedEvent : IEvent { }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace FWO.Services.EventMediator.Interfaces;
2+
3+
public interface IEvent{ }
4+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace FWO.Services.EventMediator.Interfaces;
2+
3+
public interface IEventMediator
4+
{
5+
void Subscribe<TEvent>(Action<TEvent> handler) where TEvent : class, IEvent;
6+
void Publish<TEvent>(TEvent @event) where TEvent : class, IEvent;
7+
}
8+

roles/lib/files/FWO.Services/ModellingAppRoleHandler.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ public class ModellingAppRoleHandler : ModellingHandlerBase
2121

2222
private ModellingManagedIdString OrigId = new();
2323

24-
public delegate void OnAppServersChangedEvent();
25-
public event OnAppServersChangedEvent OnAppServersChanged;
26-
2724
public ModellingAppRoleHandler(ApiConnection apiConnection, UserConfig userConfig, FwoOwner application,
2825
List<ModellingAppRole> appRoles, ModellingAppRole appRole, List<ModellingAppServer> availableAppServers,
2926
List<KeyValuePair<int, long>> availableNwElems, bool addMode, Action<Exception?, string, string, bool> displayMessageInUi, bool isOwner = true, bool readOnly = false)
@@ -306,7 +303,6 @@ public async Task SelectAppServersFromArea(ModellingNetworkArea? area)
306303
AppServersInArea.Add(server);
307304
}
308305
}
309-
OnAppServersChanged?.Invoke();
310306
}
311307
}
312308

roles/ui/files/FWO.UI/Pages/NetworkModelling/EditAppRole.razor

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
@using FWO.Services.EventMediator.Events
2+
@using FWO.Services.EventMediator.Interfaces
13
@using FWO.Ui.Display
24

35
@inject ApiConnection apiConnection
46
@inject UserConfig userConfig
7+
@inject IEventMediator EventMediator
58

69
@if (AppRoleHandler != null && Display)
710
{
811
<PopUp Title="@(AppRoleHandler.ReadOnly ? "" : (AppRoleHandler.AddMode ? userConfig.GetText("add_app_role") : userConfig.GetText("edit_app_role")))"
9-
Size="@(AppRoleHandler.ReadOnly ? PopupSize.Medium : PopupSize.Large)" Show="@Display" OnClose="Close">
12+
Size="@(AppRoleHandler.ReadOnly ? PopupSize.Medium : PopupSize.Large)" Show="@Display" OnClose="Close">
1013
<Body>
1114
<div class="row mt-2">
1215
@if (!AppRoleHandler.ReadOnly)
@@ -26,7 +29,7 @@
2629
{
2730
<div class="col-sm-8">
2831
<Dropdown ElementType="ModellingNetworkArea" SelectedElement="AppRoleHandler.ActAppRole.Area" ElementToString="@(a => a.Display() + DisplayObjectCount(a))"
29-
SelectedElementChanged="OnSelectedAreaChanged" Elements="areas" Nullable="true">
32+
SelectedElementChanged="OnSelectedAreaChanged" Elements="areas" Nullable="true">
3033
<ElementTemplate Context="area">
3134
@((MarkupString)DisplayAreaWithObjectCount(area))
3235
</ElementTemplate>
@@ -92,9 +95,9 @@
9295
{
9396
<OrderByDropdown CssClass="bg-secondary" @ref="orderByDropdown" TCollectionItem="ModellingAppServer" Collection="ModellingAppServerWrapper.Resolve(AppRoleHandler.ActAppRole.AppServers).Concat(AppRoleHandler.AppServerToAdd).ToList()" CollectionReordered="OnCollectionReordered" ElementProperties="orderByDropdownProperties" />
9497
<div class="dropzone-scrollable dropzone bg-secondary"
95-
ondragover="event.preventDefault();"
96-
ondragstart="event.dataTransfer.setData('', event.target.id);"
97-
@ondrop="HandleServerDrop">
98+
ondragover="event.preventDefault();"
99+
ondragstart="event.dataTransfer.setData('', event.target.id);"
100+
@ondrop="HandleServerDrop">
98101
<EditList ElementType="ModellingAppServer" Elements="ModellingAppServerWrapper.Resolve(AppRoleHandler.ActAppRole.AppServers)" ElementsToAdd="AppRoleHandler.AppServerToAdd" ElementsToDelete="AppRoleHandler.AppServerToDelete" StdLayout="false" ElementsRemoved="OnAppServerRemovedFromEditList">
99102
<Display>
100103
<div class="row">
@@ -136,7 +139,7 @@
136139
</PopUp>
137140
<InProgress Display="workInProgress" />
138141
<Confirm @bind-Display="ShowAreaChangeConfirmation" AllowedRoles="@Roles.Modeller" RenderMessageAsHtml="true"
139-
Message="@(userConfig.GetText("nwareachange_clear_app_servers"))" PerformAction="AreaChangeConfirmation" Title="@(userConfig.GetText("confirm"))" />
142+
Message="@(userConfig.GetText("nwareachange_clear_app_servers"))" PerformAction="AreaChangeConfirmation" Title="@(userConfig.GetText("confirm"))" />
140143
}
141144
@code
142145
{
@@ -161,6 +164,7 @@
161164
[Parameter]
162165
public Func<Task> RefreshParent { get; set; } = DefaultInit.DoNothing;
163166

167+
private readonly static CollectionChangedEvent OnCollectionChanged = new();
164168

165169
private ModellingDnDContainer Container { get; set; } = new();
166170
int sidebarLeftWidth = GlobalConst.kObjLibraryWidth;
@@ -291,6 +295,9 @@
291295

292296
workInProgress = true;
293297
await AppRoleHandler.InitAppRole(newArea);
298+
299+
EventMediator.Publish<CollectionChangedEvent>(OnCollectionChanged);
300+
294301
workInProgress = false;
295302
}
296303
}

roles/ui/files/FWO.UI/Pages/NetworkModelling/EditAppRoleLeftSide.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<div class="bg-secondary">
1313
<div id="app-role-library-ordered-draggable-list" class="col-sm-12 ">
1414
<div class="d-flex align-items-center">
15-
<OrderByDropdown CssClass="@(orderByDropdownWidthClass)" @ref="orderByDropdown" AppRoleHandler="AppRoleHandler" TCollectionItem="ModellingAppServer" Collection="AppRoleHandler.AppServersInArea" CollectionReordered="OnCollectionReordered" ElementProperties="orderByDropdownProperties" />
15+
<OrderByDropdown CssClass="@(orderByDropdownWidthClass)" @ref="orderByDropdown" TCollectionItem="ModellingAppServer" Collection="AppRoleHandler.AppServersInArea" CollectionReordered="OnCollectionReordered" ElementProperties="orderByDropdownProperties" />
1616
@if (selectedAppServers.Count > 0)
1717
{
1818
<div class="col-sm-2 p-2">

roles/ui/files/FWO.UI/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
using Microsoft.AspNetCore.Components.Authorization;
1010
using Microsoft.AspNetCore.Components.Server.Circuits;
1111
using RestSharp;
12-
using System.Diagnostics;
13-
using PuppeteerSharp;
12+
using FWO.Services.EventMediator.Interfaces;
13+
using FWO.Services.EventMediator;
1414

1515

1616
// Implicitly call static constructor so background lock process is started
@@ -38,6 +38,7 @@
3838
builder.Services.AddScoped<AuthenticationStateProvider, AuthStateProvider>();
3939
builder.Services.AddScoped<CircuitHandler, CircuitHandlerService>();
4040
builder.Services.AddScoped<KeyboardInputService, KeyboardInputService>();
41+
builder.Services.AddSingleton<IEventMediator, EventMediator>();
4142

4243
string ApiUri = ConfigFile.ApiServerUri;
4344
string MiddlewareUri = ConfigFile.MiddlewareServerUri;

roles/ui/files/FWO.UI/Shared/OrderByDropdown.razor

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
@using FWO.Basics.Comparer
2+
@using FWO.Services.EventMediator.Interfaces
3+
@using FWO.Services.EventMediator.Events
24
@using FWO.Ui.Data
35
@using FWO.Data.Modelling
46
@using NetTools
57
@using System.Linq.Expressions
68
@using System.Net
79
@using System.Reflection
10+
@inject IEventMediator EventMediator
811

912
@typeparam TCollectionItem
1013

@@ -47,8 +50,6 @@
4750
[Parameter]
4851
public string? CssClass { get; set; } = "";
4952

50-
[Parameter]
51-
public ModellingAppRoleHandler? AppRoleHandler { get; set; }
5253

5354
/// <summary>
5455
/// Backing field of property 'SelectedProperty'.
@@ -117,21 +118,12 @@
117118
CssClass = "";
118119
}
119120

120-
if (AppRoleHandler is not null)
121-
{
122-
AppRoleHandler.OnAppServersChanged += OnAppServersChanged;
123-
}
121+
EventMediator.Subscribe<CollectionChangedEvent>(new Action<CollectionChangedEvent>(_ => ReorderCollection()));
124122

125123
initialized = true;
126124
}
127125
}
128126

129-
private void OnAppServersChanged()
130-
{
131-
if(Collection is not null && Collection.Count > 0)
132-
ReorderCollection();
133-
}
134-
135127
/// <summary>
136128
/// Reordering the collection by the selected property and in the selected mode.
137129
/// </summary>

0 commit comments

Comments
 (0)