Skip to content

Commit

Permalink
Moved more icon responsibility to the icon service.
Browse files Browse the repository at this point in the history
  • Loading branch information
Delubear committed Sep 25, 2024
1 parent 13a72db commit 0259a15
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 166 deletions.
7 changes: 4 additions & 3 deletions GlucoseTray.Domain/DisplayResults/AlertService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace GlucoseTray.Domain.DisplayResults;

public class AlertService(ISettingsProxy options, IUiService uiService)
public class AlertService(ISettingsProxy options, IIconService iconService, IDialogService dialogService)
{
private readonly ISettingsProxy _options = options;
private readonly IUiService _uiService = uiService;
private readonly IIconService _uiService = iconService;
private readonly IDialogService _dialogService = dialogService;
private AlertLevel _currentAlertLevel = AlertLevel.None;

public void AlertNotification(GlucoseResult currentGlucoseResult)
Expand Down Expand Up @@ -37,7 +38,7 @@ public void AlertNotification(GlucoseResult currentGlucoseResult)
if (criticalLowAlertTriggered)
{
if (_currentAlertLevel != AlertLevel.CriticalLow)
_uiService.ShowCriticalAlert("Critical Low Glucose Alert", "Critical Low Glucose Alert");
_dialogService.ShowCriticalAlert("Critical Low Glucose Alert", "Critical Low Glucose Alert");
_currentAlertLevel = AlertLevel.CriticalLow;
return;
}
Expand Down
7 changes: 7 additions & 0 deletions GlucoseTray.Domain/DisplayResults/IDialogService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace GlucoseTray.Domain.DisplayResults;

public interface IDialogService
{
void ShowErrorAlert(string messageBoxText, string caption);
void ShowCriticalAlert(string alertText, string alertName);
}
10 changes: 10 additions & 0 deletions GlucoseTray.Domain/DisplayResults/IIconService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace GlucoseTray.Domain.DisplayResults
{
public interface IIconService
{
void InitializeTrayIcon(EventHandler exitEvent);
void DisposeTrayIcon();
void CreateIcon(GlucoseResult glucoseResult);
void ShowAlert(string alertName);
}
}
11 changes: 0 additions & 11 deletions GlucoseTray.Domain/DisplayResults/IUiService.cs

This file was deleted.

4 changes: 2 additions & 2 deletions GlucoseTray.Domain/FetchResults/DebugService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace GlucoseTray.Domain.FetchResults;

public class DebugService(IUiService uiService)
public class DebugService(IDialogService uiService)
{
private readonly List<string> DebugText = [];
private readonly IUiService _uiService = uiService;
private readonly IDialogService _uiService = uiService;

public void AddDebugText(string text) => DebugText.Add(text);

Expand Down
56 changes: 30 additions & 26 deletions GlucoseTray.Tests/AlertServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@

using GlucoseTray.Models;
using GlucoseTray.Services;
using NSubstitute;
using GlucoseTray.Settings;
using GlucoseTray.Domain.Enums;
using GlucoseTray.Domain;
using GlucoseTray.Domain.DisplayResults;
Expand All @@ -24,8 +21,9 @@ public void AlertNotification_WhenHighAlertTriggered_ShouldShowHighAlert(Glucose
options.HighBg.Returns(alertValue);
options.GlucoseUnit.Returns(unitType);
options.StaleResultsThreshold.Returns(15);
var uiService = Substitute.For<IUiService>();
var alertService = new AlertService(options, uiService);
var dialogService = Substitute.For<IDialogService>();
var iconService = Substitute.For<IIconService>();
var alertService = new AlertService(options, iconService, dialogService);
var glucoseResult = new GlucoseResult
{
MgValue = mgValue,
Expand All @@ -37,7 +35,7 @@ public void AlertNotification_WhenHighAlertTriggered_ShouldShowHighAlert(Glucose
alertService.AlertNotification(glucoseResult);

// Assert
uiService.Received().ShowAlert("High Glucose Alert");
iconService.Received().ShowAlert("High Glucose Alert");
}

[Test]
Expand All @@ -53,8 +51,9 @@ public void AlertNotification_WhenWarningHighAlertTriggered_ShouldShowWarningHig
options.WarningHighBg.Returns(alertValue);
options.GlucoseUnit.Returns(unitType);
options.StaleResultsThreshold.Returns(15);
var uiService = Substitute.For<IUiService>();
var alertService = new AlertService(options, uiService);
var dialogService = Substitute.For<IDialogService>();
var iconService = Substitute.For<IIconService>();
var alertService = new AlertService(options, iconService, dialogService);
var glucoseResult = new GlucoseResult
{
MgValue = mgValue,
Expand All @@ -66,7 +65,7 @@ public void AlertNotification_WhenWarningHighAlertTriggered_ShouldShowWarningHig
alertService.AlertNotification(glucoseResult);

// Assert
uiService.Received().ShowAlert("Warning High Glucose Alert");
iconService.Received().ShowAlert("Warning High Glucose Alert");
}

[Test]
Expand All @@ -82,8 +81,9 @@ public void AlertNotification_WhenCriticalLowAlertTriggered_ShouldShowCriticalLo
options.CriticalLowBg.Returns(alertValue);
options.GlucoseUnit.Returns(unitType);
options.StaleResultsThreshold.Returns(15);
var uiService = Substitute.For<IUiService>();
var alertService = new AlertService(options, uiService);
var dialogService = Substitute.For<IDialogService>();
var iconService = Substitute.For<IIconService>();
var alertService = new AlertService(options, iconService, dialogService);
var glucoseResult = new GlucoseResult
{
MgValue = mgValue,
Expand All @@ -95,7 +95,7 @@ public void AlertNotification_WhenCriticalLowAlertTriggered_ShouldShowCriticalLo
alertService.AlertNotification(glucoseResult);

// Assert
uiService.Received().ShowCriticalAlert("Critical Low Glucose Alert", "Critical Low Glucose Alert");
dialogService.Received().ShowCriticalAlert("Critical Low Glucose Alert", "Critical Low Glucose Alert");
}

[Test]
Expand All @@ -112,8 +112,9 @@ public void AlertNotification_WhenLowAlertTriggered_ShouldShowLowAlert(GlucoseUn
options.GlucoseUnit.Returns(unitType);
options.StaleResultsThreshold.Returns(15);

var uiService = Substitute.For<IUiService>();
var alertService = new AlertService(options, uiService);
var dialogService = Substitute.For<IDialogService>();
var iconService = Substitute.For<IIconService>();
var alertService = new AlertService(options, iconService, dialogService);
var glucoseResult = new GlucoseResult
{
MgValue = mgValue,
Expand All @@ -125,7 +126,7 @@ public void AlertNotification_WhenLowAlertTriggered_ShouldShowLowAlert(GlucoseUn
alertService.AlertNotification(glucoseResult);

// Assert
uiService.Received().ShowAlert("Low Glucose Alert");
iconService.Received().ShowAlert("Low Glucose Alert");
}

[Test]
Expand All @@ -141,8 +142,9 @@ public void AlertNotification_WhenWarningLowAlertTriggered_ShouldShowWarningLowA
options.WarningLowBg.Returns(alertValue);
options.GlucoseUnit.Returns(unitType);
options.StaleResultsThreshold.Returns(15);
var uiService = Substitute.For<IUiService>();
var alertService = new AlertService(options, uiService);
var dialogService = Substitute.For<IDialogService>();
var iconService = Substitute.For<IIconService>();
var alertService = new AlertService(options, iconService, dialogService);
var glucoseResult = new GlucoseResult
{
MgValue = mgValue,
Expand All @@ -154,7 +156,7 @@ public void AlertNotification_WhenWarningLowAlertTriggered_ShouldShowWarningLowA
alertService.AlertNotification(glucoseResult);

// Assert
uiService.Received().ShowAlert("Warning Low Glucose Alert");
iconService.Received().ShowAlert("Warning Low Glucose Alert");
}

[Test]
Expand All @@ -166,8 +168,9 @@ public void AlertNotification_WhenNoAlertTriggered_ShouldNotShowAlert()
options.HighBg.Returns(200);
options.GlucoseUnit.Returns(GlucoseUnitType.MG);
options.StaleResultsThreshold.Returns(15);
var uiService = Substitute.For<IUiService>();
var alertService = new AlertService(options, uiService);
var dialogService = Substitute.For<IDialogService>();
var iconService = Substitute.For<IIconService>();
var alertService = new AlertService(options, iconService, dialogService);
var glucoseResult = new GlucoseResult
{
MgValue = 100,
Expand All @@ -179,8 +182,8 @@ public void AlertNotification_WhenNoAlertTriggered_ShouldNotShowAlert()
alertService.AlertNotification(glucoseResult);

// Assert
uiService.DidNotReceive().ShowAlert(Arg.Any<string>());
uiService.DidNotReceive().ShowCriticalAlert(Arg.Any<string>(), Arg.Any<string>());
iconService.DidNotReceive().ShowAlert(Arg.Any<string>());
dialogService.DidNotReceive().ShowCriticalAlert(Arg.Any<string>(), Arg.Any<string>());
}

[Test]
Expand All @@ -192,8 +195,9 @@ public void AlertNotification_WhenStale_ShouldNotShowAlert()
options.HighBg.Returns(200);
options.GlucoseUnit.Returns(GlucoseUnitType.MG);
options.StaleResultsThreshold.Returns(15);
var uiService = Substitute.For<IUiService>();
var alertService = new AlertService(options, uiService);
var dialogService = Substitute.For<IDialogService>();
var iconService = Substitute.For<IIconService>();
var alertService = new AlertService(options, iconService, dialogService);
var glucoseResult = new GlucoseResult
{
MgValue = 400,
Expand All @@ -205,7 +209,7 @@ public void AlertNotification_WhenStale_ShouldNotShowAlert()
alertService.AlertNotification(glucoseResult);

// Assert
uiService.DidNotReceive().ShowAlert(Arg.Any<string>());
uiService.DidNotReceive().ShowCriticalAlert(Arg.Any<string>(), Arg.Any<string>());
iconService.DidNotReceive().ShowAlert(Arg.Any<string>());
dialogService.DidNotReceive().ShowCriticalAlert(Arg.Any<string>(), Arg.Any<string>());
}
}
6 changes: 3 additions & 3 deletions GlucoseTray.Tests/DebugServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class DebugServiceTests
public void AddDebugText_Should_AddText()
{
// Arrange
var mockUiService = Substitute.For<IUiService>();
var mockUiService = Substitute.For<IDialogService>();
var debugService = new DebugService(mockUiService);
var text = "Test";

Expand All @@ -26,7 +26,7 @@ public void AddDebugText_Should_AddText()
public void ClearDebugText_Should_ClearText()
{
// Arrange
var mockUiService = Substitute.For<IUiService>();
var mockUiService = Substitute.For<IDialogService>();
var debugService = new DebugService(mockUiService);
var text = "Test";

Expand All @@ -43,7 +43,7 @@ public void ClearDebugText_Should_ClearText()
public void ShowDebugAlert_Should_ShowAlert()
{
// Arrange
var mockUiService = Substitute.For<IUiService>();
var mockUiService = Substitute.For<IDialogService>();
var debugService = new DebugService(mockUiService);

// Act
Expand Down
18 changes: 10 additions & 8 deletions GlucoseTray/AppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ public class AppContext : ApplicationContext
private readonly ILogger<AppContext> _logger;
private readonly ISettingsProxy _options;
private readonly IGlucoseFetchService _fetchService;
private readonly IUiService _uiService;
private readonly IIconService _iconService;
private readonly IDialogService _dialogService;
private readonly AlertService _alertService;

public AppContext(ILogger<AppContext> logger, IGlucoseFetchService fetchService, ISettingsProxy options, IUiService uiService, AlertService alertService)
public AppContext(ILogger<AppContext> logger, IGlucoseFetchService fetchService, ISettingsProxy options, IIconService uiService, AlertService alertService, IDialogService dialogService)
{
_logger = logger;
_fetchService = fetchService;
_options = options;
_uiService = uiService;
_iconService = uiService;
_alertService = alertService;
_dialogService = dialogService;

_uiService.InitializeTrayIcon(new EventHandler(Exit));
_iconService.InitializeTrayIcon(new EventHandler(Exit));
BeginCycle();
}

Expand All @@ -35,16 +37,16 @@ private async void BeginCycle()
Application.DoEvents();

GlucoseResult currentGlucoseResult = await _fetchService.GetLatestReadingsAsync();
_uiService.CreateIcon(currentGlucoseResult);
_iconService.CreateIcon(currentGlucoseResult);
_alertService.AlertNotification(currentGlucoseResult);

await Task.Delay(_options.PollingThresholdTimeSpan);
}
catch (Exception e)
{
_uiService.ShowErrorAlert($"ERROR: {e}", "ERROR");
_dialogService.ShowErrorAlert($"ERROR: {e}", "ERROR");
_logger.LogError(e, "An error occurred while fetching the latest glucose readings.");
_uiService.DisposeTrayIcon();
_iconService.DisposeTrayIcon();
Environment.Exit(0);
}
}
Expand All @@ -53,7 +55,7 @@ private async void BeginCycle()
private void Exit(object? sender, EventArgs e)
{
_logger.LogInformation("Exiting application.");
_uiService.DisposeTrayIcon();
_iconService.DisposeTrayIcon();
Application.ExitThread();
Application.Exit();
}
Expand Down
2 changes: 1 addition & 1 deletion GlucoseTray/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private static void ConfigureServices(IConfiguration configuration, IServiceColl
.AddScoped<AppContext, AppContext>()
.AddScoped<IIconService, IconService>()
.AddScoped<UrlAssembler, UrlAssembler>()
.AddScoped<IUiService, UiService>()
.AddScoped<IDialogService, UiService>()
.AddScoped<ITaskSchedulerService, TaskSchedulerService>()
.AddScoped<INightscoutService, NightscoutService>()
.AddScoped<IDexcomService, DexcomService>()
Expand Down
9 changes: 0 additions & 9 deletions GlucoseTray/Services/IIconService.cs

This file was deleted.

Loading

0 comments on commit 0259a15

Please sign in to comment.