From e0cacbe2a01ae6e192ccd299a2d715ff34bd9501 Mon Sep 17 00:00:00 2001 From: Chris Baker Date: Thu, 26 Sep 2024 08:59:55 -0500 Subject: [PATCH] Further refactorings and restructuring --- GlucoseTray.Domain/DependencyExtensions.cs | 21 +++++++++++ GlucoseTray.Domain/Runner.cs | 35 +++++++++++++++++++ .../FileService.cs | 13 ++++--- GlucoseTray/AppContext.cs | 28 +++++---------- .../DialogService.cs} | 4 +-- .../IconService.cs | 12 ++++--- .../GlucoseTraySettings.cs | 2 +- .../NightScoutStatus.cs | 2 +- .../SettingsProxy.cs | 5 ++- .../SettingsService.cs | 12 ++++--- .../StringEncryptionService.cs | 2 +- GlucoseTray/Program.cs | 24 +++++++------ GlucoseTray/Usings.cs | 4 +-- GlucoseTray/Views/SettingsWindow.xaml.cs | 2 +- .../SettingsWindowService.cs | 12 +++---- GlucoseTray/appsettings.json | 2 +- 16 files changed, 116 insertions(+), 64 deletions(-) create mode 100644 GlucoseTray.Domain/DependencyExtensions.cs create mode 100644 GlucoseTray.Domain/Runner.cs rename {GlucoseTray/Services => GlucoseTray.Infrastructure}/FileService.cs (67%) rename GlucoseTray/{Services/UiService.cs => DisplayResults/DialogService.cs} (82%) rename GlucoseTray/{Services => DisplayResults}/IconService.cs (94%) rename GlucoseTray/{Settings => GlucoseSettings}/GlucoseTraySettings.cs (99%) rename GlucoseTray/{Models => GlucoseSettings}/NightScoutStatus.cs (91%) rename GlucoseTray/{Settings => GlucoseSettings}/SettingsProxy.cs (98%) rename GlucoseTray/{Services => GlucoseSettings}/SettingsService.cs (92%) rename GlucoseTray/{Services => GlucoseSettings}/StringEncryptionService.cs (98%) rename GlucoseTray/{Services => Views}/SettingsWindowService.cs (91%) diff --git a/GlucoseTray.Domain/DependencyExtensions.cs b/GlucoseTray.Domain/DependencyExtensions.cs new file mode 100644 index 0000000..8bf4698 --- /dev/null +++ b/GlucoseTray.Domain/DependencyExtensions.cs @@ -0,0 +1,21 @@ +using GlucoseTray.Domain.DisplayResults; +using GlucoseTray.Domain.FetchResults; +using Microsoft.Extensions.DependencyInjection; + +namespace GlucoseTray.Domain; + +public static class DependencyExtensions +{ + public static IServiceCollection RegisterDomainServices(this IServiceCollection services) + { + services.AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped(); + + return services; + } +} diff --git a/GlucoseTray.Domain/Runner.cs b/GlucoseTray.Domain/Runner.cs new file mode 100644 index 0000000..0f1b521 --- /dev/null +++ b/GlucoseTray.Domain/Runner.cs @@ -0,0 +1,35 @@ +using GlucoseTray.Domain.DisplayResults; +using GlucoseTray.Domain.FetchResults; + +namespace GlucoseTray.Domain; + +public interface IRunner +{ + void Initialize(EventHandler exitHandler); + Task DoWorkAsync(); + void HandleShutdown(Exception? e = null); +} + +public class Runner(IGlucoseFetchService fetchService, IIconService uiService, AlertService alertService, IDialogService dialogService) : IRunner +{ + private readonly IGlucoseFetchService _fetchService = fetchService; + private readonly IIconService _iconService = uiService; + private readonly IDialogService _dialogService = dialogService; + private readonly AlertService _alertService = alertService; + + public void Initialize(EventHandler exitHandler) => _iconService.InitializeTrayIcon(exitHandler); + + public async Task DoWorkAsync() + { + GlucoseResult currentGlucoseResult = await _fetchService.GetLatestReadingsAsync(); + _iconService.CreateIcon(currentGlucoseResult); + _alertService.AlertNotification(currentGlucoseResult); + } + + public void HandleShutdown(Exception? e = null) + { + if (e is not null) + _dialogService.ShowErrorAlert($"ERROR: {e}", "ERROR"); + _iconService.DisposeTrayIcon(); + } +} diff --git a/GlucoseTray/Services/FileService.cs b/GlucoseTray.Infrastructure/FileService.cs similarity index 67% rename from GlucoseTray/Services/FileService.cs rename to GlucoseTray.Infrastructure/FileService.cs index 046b827..71a33eb 100644 --- a/GlucoseTray/Services/FileService.cs +++ b/GlucoseTray.Infrastructure/FileService.cs @@ -1,9 +1,14 @@ -using System.IO; -using System.Text.Json; +using System.Text.Json; -namespace GlucoseTray.Services; +namespace GlucoseTray.Infrastructure; -public class FileService +public interface IFileService where T : class +{ + void WriteModelToJsonFile(T model, string file); + T? ReadModelFromFile(string file); +} + +public class FileService : IFileService where T : class { public void WriteModelToJsonFile(T model, string file) { diff --git a/GlucoseTray/AppContext.cs b/GlucoseTray/AppContext.cs index d26bbc7..31d7f74 100644 --- a/GlucoseTray/AppContext.cs +++ b/GlucoseTray/AppContext.cs @@ -1,6 +1,4 @@ using GlucoseTray.Domain; -using GlucoseTray.Domain.DisplayResults; -using GlucoseTray.Domain.FetchResults; using Microsoft.Extensions.Logging; using System.Windows.Forms; @@ -9,44 +7,34 @@ namespace GlucoseTray; public class AppContext : ApplicationContext { private readonly ILogger _logger; + private readonly IRunner _runner; private readonly ISettingsProxy _options; - private readonly IGlucoseFetchService _fetchService; - private readonly IIconService _iconService; - private readonly IDialogService _dialogService; - private readonly AlertService _alertService; - public AppContext(ILogger logger, IGlucoseFetchService fetchService, ISettingsProxy options, IIconService uiService, AlertService alertService, IDialogService dialogService) + public AppContext(ILogger logger, IRunner runner, ISettingsProxy options) { _logger = logger; - _fetchService = fetchService; + _runner = runner; _options = options; - _iconService = uiService; - _alertService = alertService; - _dialogService = dialogService; - _iconService.InitializeTrayIcon(new EventHandler(Exit)); BeginCycle(); } private async void BeginCycle() { + _runner.Initialize(new EventHandler(Exit)); + while (true) { try { Application.DoEvents(); - - GlucoseResult currentGlucoseResult = await _fetchService.GetLatestReadingsAsync(); - _iconService.CreateIcon(currentGlucoseResult); - _alertService.AlertNotification(currentGlucoseResult); - + await _runner.DoWorkAsync(); await Task.Delay(_options.PollingThresholdTimeSpan); } catch (Exception e) { - _dialogService.ShowErrorAlert($"ERROR: {e}", "ERROR"); _logger.LogError(e, "An error occurred while fetching the latest glucose readings."); - _iconService.DisposeTrayIcon(); + _runner.HandleShutdown(e); Environment.Exit(0); } } @@ -55,7 +43,7 @@ private async void BeginCycle() private void Exit(object? sender, EventArgs e) { _logger.LogInformation("Exiting application."); - _iconService.DisposeTrayIcon(); + _runner.HandleShutdown(); Application.ExitThread(); Application.Exit(); } diff --git a/GlucoseTray/Services/UiService.cs b/GlucoseTray/DisplayResults/DialogService.cs similarity index 82% rename from GlucoseTray/Services/UiService.cs rename to GlucoseTray/DisplayResults/DialogService.cs index d2e3608..5977353 100644 --- a/GlucoseTray/Services/UiService.cs +++ b/GlucoseTray/DisplayResults/DialogService.cs @@ -1,9 +1,9 @@ using GlucoseTray.Domain.DisplayResults; using System.Windows.Forms; -namespace GlucoseTray.Services; +namespace GlucoseTray.DisplayResults; -public class UiService() : IDialogService +public class DialogService() : IDialogService { public void ShowErrorAlert(string messageBoxText, string caption) => MessageBox.Show(messageBoxText, caption, MessageBoxButtons.OK, MessageBoxIcon.Error); diff --git a/GlucoseTray/Services/IconService.cs b/GlucoseTray/DisplayResults/IconService.cs similarity index 94% rename from GlucoseTray/Services/IconService.cs rename to GlucoseTray/DisplayResults/IconService.cs index 2d91669..d403883 100644 --- a/GlucoseTray/Services/IconService.cs +++ b/GlucoseTray/DisplayResults/IconService.cs @@ -1,6 +1,7 @@ using GlucoseTray.Domain; using GlucoseTray.Domain.DisplayResults; using GlucoseTray.Domain.Enums; +using GlucoseTray.Views; using GlucoseTray.Views.Settings; using Microsoft.Extensions.Logging; using System.ComponentModel; @@ -9,13 +10,14 @@ using System.Runtime.InteropServices; using System.Windows.Forms; -namespace GlucoseTray.Services; +namespace GlucoseTray.DisplayResults; -public class IconService(ILogger logger, ISettingsProxy options, ITaskSchedulerService taskScheduler) : IIconService +public class IconService(ILogger logger, ISettingsProxy options, ITaskSchedulerService taskScheduler, ISettingsWindowService settingsWindowService) : IIconService { private readonly ILogger _logger = logger; private readonly ISettingsProxy _options = options; private readonly ITaskSchedulerService _taskScheduler = taskScheduler; + private readonly ISettingsWindowService _settingsWindowService = settingsWindowService; private readonly float _standardOffset = -10f; private readonly int _defaultFontSize = 40; private readonly int _smallerFontSize = 38; @@ -95,7 +97,7 @@ private void ChangeSettings(object? sender, EventArgs e) { if (!SettingsFormIsOpen) { - var settingsWindow = new SettingsWindow(new SettingsWindowService()); + var settingsWindow = new SettingsWindow(_settingsWindowService); SettingsFormIsOpen = true; settingsWindow.ShowDialog(); SettingsFormIsOpen = false; @@ -105,9 +107,9 @@ private void ChangeSettings(object? sender, EventArgs e) [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern bool DestroyIcon(nint handle); - public static void DestroyMyIcon(nint handle) => DestroyIcon(handle); + private static void DestroyMyIcon(nint handle) => DestroyIcon(handle); - public Brush SetColor(double val) + private Brush SetColor(double val) { if (_options.IsDarkMode) { diff --git a/GlucoseTray/Settings/GlucoseTraySettings.cs b/GlucoseTray/GlucoseSettings/GlucoseTraySettings.cs similarity index 99% rename from GlucoseTray/Settings/GlucoseTraySettings.cs rename to GlucoseTray/GlucoseSettings/GlucoseTraySettings.cs index 3a5b3b3..a641f09 100644 --- a/GlucoseTray/Settings/GlucoseTraySettings.cs +++ b/GlucoseTray/GlucoseSettings/GlucoseTraySettings.cs @@ -2,7 +2,7 @@ using System.ComponentModel; using System.Text.Json.Serialization; -namespace GlucoseTray.Settings; +namespace GlucoseTray.GlucoseSettings; public class GlucoseTraySettings : INotifyPropertyChanged { diff --git a/GlucoseTray/Models/NightScoutStatus.cs b/GlucoseTray/GlucoseSettings/NightScoutStatus.cs similarity index 91% rename from GlucoseTray/Models/NightScoutStatus.cs rename to GlucoseTray/GlucoseSettings/NightScoutStatus.cs index 7599c9a..ce83ffb 100644 --- a/GlucoseTray/Models/NightScoutStatus.cs +++ b/GlucoseTray/GlucoseSettings/NightScoutStatus.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace GlucoseTray.Models; +namespace GlucoseTray.GlucoseSettings; /// /// Class that maps to the JSON from NightScout status. diff --git a/GlucoseTray/Settings/SettingsProxy.cs b/GlucoseTray/GlucoseSettings/SettingsProxy.cs similarity index 98% rename from GlucoseTray/Settings/SettingsProxy.cs rename to GlucoseTray/GlucoseSettings/SettingsProxy.cs index dc8eb46..b8e73d2 100644 --- a/GlucoseTray/Settings/SettingsProxy.cs +++ b/GlucoseTray/GlucoseSettings/SettingsProxy.cs @@ -1,9 +1,8 @@ - -using GlucoseTray.Domain; +using GlucoseTray.Domain; using GlucoseTray.Domain.Enums; using Microsoft.Extensions.Options; -namespace GlucoseTray.Settings; +namespace GlucoseTray.GlucoseSettings; public class SettingsProxy(IOptionsMonitor options) : ISettingsProxy { diff --git a/GlucoseTray/Services/SettingsService.cs b/GlucoseTray/GlucoseSettings/SettingsService.cs similarity index 92% rename from GlucoseTray/Services/SettingsService.cs rename to GlucoseTray/GlucoseSettings/SettingsService.cs index 55d0682..f9dbe31 100644 --- a/GlucoseTray/Services/SettingsService.cs +++ b/GlucoseTray/GlucoseSettings/SettingsService.cs @@ -2,11 +2,16 @@ using System.Net.Http.Headers; using System.Text.Json; using GlucoseTray.Domain.Enums; -using GlucoseTray.Settings; +using GlucoseTray.Infrastructure; -namespace GlucoseTray.Services; +namespace GlucoseTray.GlucoseSettings; -public class SettingsService +public interface ISettingsService +{ + List ValidateSettings(GlucoseTraySettings? model = null); +} + +public class SettingsService(IFileService fileService) : ISettingsService { /// /// If model is null, will validate from stored settings file. @@ -18,7 +23,6 @@ public List ValidateSettings(GlucoseTraySettings? model = null) if (model is null) { - var fileService = new FileService(); model = fileService.ReadModelFromFile(Program.SettingsFile); if (model is null) { diff --git a/GlucoseTray/Services/StringEncryptionService.cs b/GlucoseTray/GlucoseSettings/StringEncryptionService.cs similarity index 98% rename from GlucoseTray/Services/StringEncryptionService.cs rename to GlucoseTray/GlucoseSettings/StringEncryptionService.cs index 83d90bb..3b4a6a5 100644 --- a/GlucoseTray/Services/StringEncryptionService.cs +++ b/GlucoseTray/GlucoseSettings/StringEncryptionService.cs @@ -2,7 +2,7 @@ using System.Security.Cryptography; using System.Text; -namespace GlucoseTray.Services; +namespace GlucoseTray.GlucoseSettings; /// /// Used example from here: https://tekeye.uk/visual_studio/encrypt-decrypt-c-sharp-string diff --git a/GlucoseTray/Program.cs b/GlucoseTray/Program.cs index f264714..bc854a3 100644 --- a/GlucoseTray/Program.cs +++ b/GlucoseTray/Program.cs @@ -1,8 +1,11 @@ -using GlucoseTray.Domain; +using GlucoseTray.DisplayResults; +using GlucoseTray.Domain; using GlucoseTray.Domain.DisplayResults; using GlucoseTray.Domain.FetchResults; +using GlucoseTray.GlucoseSettings; using GlucoseTray.Infrastructure; using GlucoseTray.Settings; +using GlucoseTray.Views; using GlucoseTray.Views.Settings; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -50,17 +53,14 @@ private static void ConfigureServices(IConfiguration configuration, IServiceColl .AddHttpClient() .AddScoped() .AddScoped() - .AddScoped() - .AddScoped() + .AddScoped() .AddScoped() - .AddScoped() - .AddScoped() - .AddScoped() .AddScoped() - .AddScoped() .AddScoped() .AddScoped() - .AddScoped(); + .AddScoped() + .AddScoped, FileService>() + .RegisterDomainServices(); } private static AppSettings GetAppSettings() @@ -74,10 +74,12 @@ private static bool LoadApplicationSettings() { Environment.SetEnvironmentVariable("windir", Environment.GetEnvironmentVariable("SystemRoot"), EnvironmentVariableTarget.User); SettingsFile = Application.UserAppDataPath + @"\glucose_tray_settings.json"; - var setttingsService = new SettingsService(); - if (!File.Exists(SettingsFile) || setttingsService.ValidateSettings().Count != 0) + var fileService = new FileService(); + var settingsService = new SettingsService(fileService); + if (!File.Exists(SettingsFile) || settingsService.ValidateSettings().Count != 0) { - var settingsWindow = new SettingsWindow(new SettingsWindowService()); + var settingsWindowService = new SettingsWindowService(fileService, settingsService); + var settingsWindow = new SettingsWindow(settingsWindowService); if (settingsWindow.ShowDialog() != true) // Did not want to setup application. { Application.Exit(); diff --git a/GlucoseTray/Usings.cs b/GlucoseTray/Usings.cs index 76e9319..48b8b14 100644 --- a/GlucoseTray/Usings.cs +++ b/GlucoseTray/Usings.cs @@ -1,5 +1,3 @@ -global using GlucoseTray.Models; -global using GlucoseTray.Services; -global using System; +global using System; global using System.Collections.Generic; global using System.Threading.Tasks; \ No newline at end of file diff --git a/GlucoseTray/Views/SettingsWindow.xaml.cs b/GlucoseTray/Views/SettingsWindow.xaml.cs index b8d1369..c2226fa 100644 --- a/GlucoseTray/Views/SettingsWindow.xaml.cs +++ b/GlucoseTray/Views/SettingsWindow.xaml.cs @@ -1,6 +1,6 @@ using System.Windows; using GlucoseTray.Domain.Enums; -using GlucoseTray.Settings; +using GlucoseTray.GlucoseSettings; namespace GlucoseTray.Views.Settings; diff --git a/GlucoseTray/Services/SettingsWindowService.cs b/GlucoseTray/Views/SettingsWindowService.cs similarity index 91% rename from GlucoseTray/Services/SettingsWindowService.cs rename to GlucoseTray/Views/SettingsWindowService.cs index 799ce2b..71887e6 100644 --- a/GlucoseTray/Services/SettingsWindowService.cs +++ b/GlucoseTray/Views/SettingsWindowService.cs @@ -3,9 +3,10 @@ using System.Linq; using System.Windows; using GlucoseTray.Domain.Enums; -using GlucoseTray.Settings; +using GlucoseTray.GlucoseSettings; +using GlucoseTray.Infrastructure; -namespace GlucoseTray.Services; +namespace GlucoseTray.Views; public interface ISettingsWindowService { @@ -19,18 +20,16 @@ public interface ISettingsWindowService void Save(GlucoseTraySettings settings); } -public class SettingsWindowService : ISettingsWindowService +public class SettingsWindowService(IFileService fileService, ISettingsService settingsService) : ISettingsWindowService { public void Save(GlucoseTraySettings settings) { - var fileService = new FileService(); fileService.WriteModelToJsonFile(settings, Program.SettingsFile); } public (bool IsValid, IEnumerable Errors) IsValid(GlucoseTraySettings settings) { - var setttingsService = new SettingsService(); - var errors = setttingsService.ValidateSettings(settings); + var errors = settingsService.ValidateSettings(settings); if (errors.Any()) return (false, errors); return (true, errors); @@ -96,7 +95,6 @@ public IEnumerable GetDexComServerLocationDescriptions() { try { - var fileService = new FileService(); model = fileService.ReadModelFromFile(Program.SettingsFile); if (model is null) diff --git a/GlucoseTray/appsettings.json b/GlucoseTray/appsettings.json index ddfcaab..a473f1f 100644 --- a/GlucoseTray/appsettings.json +++ b/GlucoseTray/appsettings.json @@ -1,6 +1,6 @@ { "appsettings": { - "Version": "15.1.6", + "Version": "15.1.7", "Url": "https://github.com/Delubear/GlucoseTray" } } \ No newline at end of file