Skip to content

Commit be9fd98

Browse files
committed
feat: EventsService pattern
1 parent f35dac2 commit be9fd98

10 files changed

Lines changed: 152 additions & 117 deletions

File tree

Nickvision.Application.GNOME/Views/MainWindow.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Nickvision.Application.Shared.Controllers;
44
using Nickvision.Application.Shared.Events;
55
using Nickvision.Application.Shared.Models;
6+
using Nickvision.Application.Shared.Services;
67
using Nickvision.Desktop.Application;
78
using Nickvision.Desktop.Globalization;
89
using Nickvision.Desktop.GNOME.Controls;
@@ -36,12 +37,12 @@ public class MainWindow : Adw.ApplicationWindow
3637
[Gtk.Connect("pageFiles")]
3738
private Adw.StatusPage? _pageFiles;
3839

39-
public MainWindow(IServiceProvider serviceProvider, MainWindowController controller, AppInfo appInfo, ITranslationService translationService, IGtkBuilderFactory builderFactory) : this(serviceProvider, controller, appInfo, translationService, builderFactory.Create("MainWindow"))
40+
public MainWindow(IServiceProvider serviceProvider, MainWindowController controller, AppInfo appInfo, IEventsService eventsService, ITranslationService translationService, IGtkBuilderFactory builderFactory) : this(serviceProvider, controller, appInfo, eventsService, translationService, builderFactory.Create("MainWindow"))
4041
{
4142

4243
}
4344

44-
private MainWindow(IServiceProvider serviceProvider, MainWindowController controller, AppInfo appInfo, ITranslationService translationService, Gtk.Builder builder) : base(new Adw.Internal.ApplicationWindowHandle(builder.GetPointer("root"), false))
45+
private MainWindow(IServiceProvider serviceProvider, MainWindowController controller, AppInfo appInfo, IEventsService eventsService, ITranslationService translationService, Gtk.Builder builder) : base(new Adw.Internal.ApplicationWindowHandle(builder.GetPointer("root"), false))
4546
{
4647
var application = serviceProvider.GetRequiredService<Adw.Application>();
4748
_serviceProvider = serviceProvider;
@@ -67,12 +68,12 @@ private MainWindow(IServiceProvider serviceProvider, MainWindowController contro
6768
_pageGreeting!.Title = _controller.Greeting;
6869
// Events
6970
OnCloseRequest += Window_OnCloseRequest;
70-
_controller.AppNotificationSent += (sender, args) => GLib.Functions.IdleAdd(0, () =>
71+
eventsService.AppNotificationSent += (sender, args) => GLib.Functions.IdleAdd(0, () =>
7172
{
7273
Controller_AppNotificationSent(sender, args);
7374
return false;
7475
});
75-
_controller.FolderChanged += Controller_FolderChanged;
76+
eventsService.FolderChanged += Controller_FolderChanged;
7677
// Drop target
7778
var dropTarget = Gtk.DropTarget.New(Gio.FileHelper.GetGType(), Gdk.DragAction.Copy);
7879
dropTarget.OnDrop += Window_OnDrop;

Nickvision.Application.Shared/Controllers/MainWindowController.cs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Nickvision.Application.Shared.Events;
2-
using Nickvision.Application.Shared.Models;
1+
using Nickvision.Application.Shared.Models;
32
using Nickvision.Application.Shared.Services;
43
using Nickvision.Desktop.Application;
54
using Nickvision.Desktop.Filesystem;
@@ -44,27 +43,6 @@ public MainWindowController(AppInfo appInfo, IFolderService folderService, IJson
4443
_appInfo.TranslationCredits = _translationService._("translation-credits");
4544
}
4645

47-
public event EventHandler<AppNotificationSentEventArgs>? AppNotificationSent
48-
{
49-
add => _notificationService.AppNotificationSent += value;
50-
51-
remove => _notificationService.AppNotificationSent -= value;
52-
}
53-
54-
public event EventHandler<FolderChangedEventArgs> FolderChanged
55-
{
56-
add => _folderService.Changed += value;
57-
58-
remove => _folderService.Changed -= value;
59-
}
60-
61-
public event EventHandler<JsonFileSavedEventArgs>? JsonFileSaved
62-
{
63-
add => _jsonFileService.Saved += value;
64-
65-
remove => _jsonFileService.Saved -= value;
66-
}
67-
6846
public bool CanShutdown => true;
6947

7048
public string Greeting => DateTime.Now.Hour switch

Nickvision.Application.Shared/Helpers/HostApplicationBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public IHostApplicationBuilder ConfigureApplication(string[] args)
2626
builder.Properties.Add("AppInfo", appInfo);
2727
builder.Services.AddSingleton(appInfo);
2828
builder.ConfigureNickvision(args);
29+
builder.Services.AddSingleton<IEventsService, EventsService>();
2930
builder.Services.AddSingleton<IFolderService, FolderService>();
3031
builder.Services.AddSingleton<MainWindowController>();
3132
builder.Services.AddTransient<PreferencesViewController>();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Nickvision.Application.Shared.Events;
2+
using Nickvision.Desktop.Filesystem;
3+
using Nickvision.Desktop.Notifications;
4+
using System;
5+
6+
namespace Nickvision.Application.Shared.Services;
7+
8+
public class EventsService : IEventsService
9+
{
10+
private readonly IFolderService _folderService;
11+
private readonly IJsonFileService _jsonFileService;
12+
private readonly INotificationService _notificationService;
13+
14+
public EventsService(IFolderService folderService, IJsonFileService jsonFileService, INotificationService notificationService)
15+
{
16+
_folderService = folderService;
17+
_jsonFileService = jsonFileService;
18+
_notificationService = notificationService;
19+
}
20+
21+
public event EventHandler<AppNotificationSentEventArgs>? AppNotificationSent
22+
{
23+
add => _notificationService.AppNotificationSent += value;
24+
25+
remove => _notificationService.AppNotificationSent -= value;
26+
}
27+
28+
public event EventHandler<FolderChangedEventArgs> FolderChanged
29+
{
30+
add => _folderService.Changed += value;
31+
32+
remove => _folderService.Changed -= value;
33+
}
34+
35+
public event EventHandler<JsonFileSavedEventArgs>? JsonFileSaved
36+
{
37+
add => _jsonFileService.Saved += value;
38+
39+
remove => _jsonFileService.Saved -= value;
40+
}
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Nickvision.Application.Shared.Events;
2+
using Nickvision.Desktop.Filesystem;
3+
using Nickvision.Desktop.Notifications;
4+
using System;
5+
6+
namespace Nickvision.Application.Shared.Services;
7+
8+
public interface IEventsService
9+
{
10+
event EventHandler<AppNotificationSentEventArgs>? AppNotificationSent;
11+
event EventHandler<FolderChangedEventArgs> FolderChanged;
12+
event EventHandler<JsonFileSavedEventArgs>? JsonFileSaved;
13+
}

Nickvision.Application.WinUI/Controls/AboutDialog.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace Nickvision.Application.WinUI.Controls;
1010

1111
public sealed partial class AboutDialog : ContentDialog
1212
{
13-
private AppInfo _appInfo;
14-
private ITranslationService _translator;
13+
private readonly AppInfo _appInfo;
14+
private readonly ITranslationService _translator;
1515

1616
public AboutDialog(AppInfo appInfo, ITranslationService translator)
1717
{

Nickvision.Application.WinUI/Views/MainWindow.xaml.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Nickvision.Application.Shared.Controllers;
1010
using Nickvision.Application.Shared.Events;
1111
using Nickvision.Application.Shared.Models;
12+
using Nickvision.Application.Shared.Services;
1213
using Nickvision.Application.WinUI.Controls;
1314
using Nickvision.Desktop.Application;
1415
using Nickvision.Desktop.Filesystem;
@@ -39,7 +40,7 @@ private enum Pages
3940
private readonly ITranslationService _translationService;
4041
private RoutedEventHandler? _notificationClickHandler;
4142

42-
public MainWindow(IServiceProvider serviceProvider, MainWindowController controller, AppInfo appInfo, ITranslationService translationService)
43+
public MainWindow(IServiceProvider serviceProvider, MainWindowController controller, AppInfo appInfo, IEventsService eventsService, ITranslationService translationService)
4344
{
4445
InitializeComponent();
4546
_serviceProvider = serviceProvider;
@@ -62,9 +63,9 @@ public MainWindow(IServiceProvider serviceProvider, MainWindowController control
6263
AppWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Tall;
6364
// Events
6465
AppWindow.Closing += Window_Closing;
65-
_controller.AppNotificationSent += (sender, args) => DispatcherQueue.TryEnqueue(() => Controller_AppNotificationSent(sender, args));
66-
_controller.FolderChanged += Controller_FolderChanged;
67-
_controller.JsonFileSaved += Controller_JsonFileSaved;
66+
eventsService.AppNotificationSent += (sender, args) => DispatcherQueue.TryEnqueue(() => Controller_AppNotificationSent(sender, args));
67+
eventsService.FolderChanged += Controller_FolderChanged;
68+
eventsService.JsonFileSaved += Controller_JsonFileSaved;
6869
// Translations
6970
AppWindow.Title = _appInfo.ShortName;
7071
TitleBar.Title = _appInfo.ShortName;

Nickvision.Application.WinUI/Views/SettingsPage.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace Nickvision.Application.WinUI.Views;
1111

1212
public sealed partial class SettingsPage : Page
1313
{
14-
private PreferencesViewController _controller;
15-
private ITranslationService _translationService;
14+
private readonly PreferencesViewController _controller;
15+
private readonly ITranslationService _translationService;
1616
private bool _constructing;
1717

1818
public SettingsPage(PreferencesViewController controller, ITranslationService translationService)

0 commit comments

Comments
 (0)