Skip to content

Commit

Permalink
Further refactorings and restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
Delubear committed Sep 26, 2024
1 parent 69e38bb commit e0cacbe
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 64 deletions.
21 changes: 21 additions & 0 deletions GlucoseTray.Domain/DependencyExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<IRunner, Runner>()
.AddScoped<DebugService, DebugService>()
.AddScoped<AlertService, AlertService>()
.AddScoped<INightscoutService, NightscoutService>()
.AddScoped<IDexcomService, DexcomService>()
.AddScoped<UrlAssembler, UrlAssembler>()
.AddScoped<IGlucoseFetchService, GlucoseFetchService>();

return services;
}
}
35 changes: 35 additions & 0 deletions GlucoseTray.Domain/Runner.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System.IO;
using System.Text.Json;
using System.Text.Json;

namespace GlucoseTray.Services;
namespace GlucoseTray.Infrastructure;

public class FileService<T>
public interface IFileService<T> where T : class
{
void WriteModelToJsonFile(T model, string file);
T? ReadModelFromFile(string file);
}

public class FileService<T> : IFileService<T> where T : class
{
public void WriteModelToJsonFile(T model, string file)
{
Expand Down
28 changes: 8 additions & 20 deletions GlucoseTray/AppContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using GlucoseTray.Domain;
using GlucoseTray.Domain.DisplayResults;
using GlucoseTray.Domain.FetchResults;
using Microsoft.Extensions.Logging;
using System.Windows.Forms;

Expand All @@ -9,44 +7,34 @@ namespace GlucoseTray;
public class AppContext : ApplicationContext
{
private readonly ILogger<AppContext> _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<AppContext> logger, IGlucoseFetchService fetchService, ISettingsProxy options, IIconService uiService, AlertService alertService, IDialogService dialogService)
public AppContext(ILogger<AppContext> 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);
}
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,13 +10,14 @@
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace GlucoseTray.Services;
namespace GlucoseTray.DisplayResults;

public class IconService(ILogger<IconService> logger, ISettingsProxy options, ITaskSchedulerService taskScheduler) : IIconService
public class IconService(ILogger<IconService> logger, ISettingsProxy options, ITaskSchedulerService taskScheduler, ISettingsWindowService settingsWindowService) : IIconService
{
private readonly ILogger<IconService> _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;
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.ComponentModel;
using System.Text.Json.Serialization;

namespace GlucoseTray.Settings;
namespace GlucoseTray.GlucoseSettings;

public class GlucoseTraySettings : INotifyPropertyChanged
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace GlucoseTray.Models;
namespace GlucoseTray.GlucoseSettings;

/// <summary>
/// Class that maps to the JSON from NightScout status.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GlucoseTraySettings> options) : ISettingsProxy
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> ValidateSettings(GlucoseTraySettings? model = null);
}

public class SettingsService(IFileService<GlucoseTraySettings> fileService) : ISettingsService
{
/// <summary>
/// If model is null, will validate from stored settings file.
Expand All @@ -18,7 +23,6 @@ public List<string> ValidateSettings(GlucoseTraySettings? model = null)

if (model is null)
{
var fileService = new FileService<GlucoseTraySettings>();
model = fileService.ReadModelFromFile(Program.SettingsFile);
if (model is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Security.Cryptography;
using System.Text;

namespace GlucoseTray.Services;
namespace GlucoseTray.GlucoseSettings;

/// <summary>
/// Used example from here: https://tekeye.uk/visual_studio/encrypt-decrypt-c-sharp-string
Expand Down
24 changes: 13 additions & 11 deletions GlucoseTray/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -50,17 +53,14 @@ private static void ConfigureServices(IConfiguration configuration, IServiceColl
.AddHttpClient()
.AddScoped<AppContext, AppContext>()
.AddScoped<IIconService, IconService>()
.AddScoped<UrlAssembler, UrlAssembler>()
.AddScoped<IDialogService, UiService>()
.AddScoped<IDialogService, DialogService>()
.AddScoped<ITaskSchedulerService, TaskSchedulerService>()
.AddScoped<INightscoutService, NightscoutService>()
.AddScoped<IDexcomService, DexcomService>()
.AddScoped<AlertService, AlertService>()
.AddScoped<IExternalCommunicationAdapter, ExternalCommunicationAdapter>()
.AddScoped<DebugService, DebugService>()
.AddScoped<ISettingsWindowService, SettingsWindowService>()
.AddScoped<ISettingsProxy, SettingsProxy>()
.AddScoped<IGlucoseFetchService, GlucoseFetchService>();
.AddScoped<ISettingsService, SettingsService>()
.AddScoped<IFileService<GlucoseTraySettings>, FileService<GlucoseTraySettings>>()
.RegisterDomainServices();
}

private static AppSettings GetAppSettings()
Expand All @@ -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<GlucoseTraySettings>();
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();
Expand Down
4 changes: 1 addition & 3 deletions GlucoseTray/Usings.cs
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion GlucoseTray/Views/SettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Windows;
using GlucoseTray.Domain.Enums;
using GlucoseTray.Settings;
using GlucoseTray.GlucoseSettings;

namespace GlucoseTray.Views.Settings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -19,18 +20,16 @@ public interface ISettingsWindowService
void Save(GlucoseTraySettings settings);
}

public class SettingsWindowService : ISettingsWindowService
public class SettingsWindowService(IFileService<GlucoseTraySettings> fileService, ISettingsService settingsService) : ISettingsWindowService
{
public void Save(GlucoseTraySettings settings)
{
var fileService = new FileService<GlucoseTraySettings>();
fileService.WriteModelToJsonFile(settings, Program.SettingsFile);
}

public (bool IsValid, IEnumerable<string> 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);
Expand Down Expand Up @@ -96,7 +95,6 @@ public IEnumerable<string> GetDexComServerLocationDescriptions()
{
try
{
var fileService = new FileService<GlucoseTraySettings>();
model = fileService.ReadModelFromFile(Program.SettingsFile);

if (model is null)
Expand Down
2 changes: 1 addition & 1 deletion GlucoseTray/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"appsettings": {
"Version": "15.1.6",
"Version": "15.1.7",
"Url": "https://github.com/Delubear/GlucoseTray"
}
}

0 comments on commit e0cacbe

Please sign in to comment.