Skip to content

Commit

Permalink
First pass at bringing more structure to the code with the end goal o…
Browse files Browse the repository at this point in the history
…f being able to better facilitate testing.
  • Loading branch information
Delubear committed Sep 25, 2024
1 parent 2e93337 commit 13a72db
Show file tree
Hide file tree
Showing 45 changed files with 194 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GlucoseTray.Settings;
using GlucoseTray.Domain.Enums;

namespace GlucoseTray.Services;
namespace GlucoseTray.Domain.DisplayResults;

public class AlertService(ISettingsProxy options, IUiService uiService)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace GlucoseTray.Extensions;
using GlucoseTray.Domain.Enums;

namespace GlucoseTray.Domain.DisplayResults;

public static class GlucoseFetchResultExtensions
{
Expand Down
11 changes: 11 additions & 0 deletions GlucoseTray.Domain/DisplayResults/IUiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace GlucoseTray.Domain.DisplayResults;

public interface IUiService
{
void ShowErrorAlert(string messageBoxText, string caption);
void InitializeTrayIcon(EventHandler exitEvent);
void DisposeTrayIcon();
void CreateIcon(GlucoseResult glucoseResult);
void ShowAlert(string alertName);
void ShowCriticalAlert(string alertText, string alertName);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace GlucoseTray.Extensions;
using GlucoseTray.Domain.Enums;

namespace GlucoseTray.Domain.DisplayResults;

public static class StringExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GlucoseTray.Enums;
namespace GlucoseTray.Domain.Enums;

public enum AlertLevel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel;

namespace GlucoseTray.Enums;
namespace GlucoseTray.Domain.Enums;

public enum DexcomServerLocation
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GlucoseTray.Enums;
namespace GlucoseTray.Domain.Enums;

public enum FetchMethod
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GlucoseTray.Enums;
namespace GlucoseTray.Domain.Enums;

public enum GlucoseUnitType
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GlucoseTray.Enums;
namespace GlucoseTray.Domain.Enums;

public enum TrendResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GlucoseTray.Enums;
namespace GlucoseTray.Domain.Enums;

public enum UpDown
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace GlucoseTray.Services;
using GlucoseTray.Domain.DisplayResults;

namespace GlucoseTray.Domain.FetchResults;

public class DebugService(IUiService uiService)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GlucoseTray.Models;
namespace GlucoseTray.Domain.FetchResults;

/// <summary>
/// Class that maps to the JSON received from DexCom queries.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using GlucoseTray.Settings;
using GlucoseTray.Domain.DisplayResults;
using GlucoseTray.Domain.Enums;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Text.Json;

namespace GlucoseTray.Services;
namespace GlucoseTray.Domain.FetchResults;

public interface IDexcomService
{
Expand Down Expand Up @@ -84,7 +85,7 @@ private async Task<string> GetSessionId(string accountId)
{
var sessionIdRequestJson = JsonSerializer.Serialize(new
{
accountId = accountId,
accountId,
applicationId = "d8665ade-9673-4e27-9ff6-92db4ce13d13",
password = _options.DexcomPassword
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GlucoseTray.Settings;
using GlucoseTray.Domain.Enums;
using Microsoft.Extensions.Logging;

namespace GlucoseTray.Services;
namespace GlucoseTray.Domain.FetchResults;

public interface IGlucoseFetchService
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using GlucoseTray.Settings;
using GlucoseTray.Domain.Enums;

namespace GlucoseTray.Services;
namespace GlucoseTray.Domain.FetchResults;

internal static class GlucoseMath
public static class GlucoseMath
{
private static bool IsCriticalLow(GlucoseResult result, ISettingsProxy currentSettings)
{
if (result.MmolValue == 0) // Don't treat a zero / null / default result as critical low.
return false;
return (currentSettings.GlucoseUnit == GlucoseUnitType.MMOL && result.MmolValue <= currentSettings.CriticalLowBg)
|| (currentSettings.GlucoseUnit == GlucoseUnitType.MG && result.MgValue <= currentSettings.CriticalLowBg);
return currentSettings.GlucoseUnit == GlucoseUnitType.MMOL && result.MmolValue <= currentSettings.CriticalLowBg
|| currentSettings.GlucoseUnit == GlucoseUnitType.MG && result.MgValue <= currentSettings.CriticalLowBg;
}

internal static void CalculateValues(GlucoseResult result, double value, ISettingsProxy currentSettings)
public static void CalculateValues(GlucoseResult result, double value, ISettingsProxy currentSettings)
{
if (value == 0)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace GlucoseTray.Domain.FetchResults
{
public interface IExternalCommunicationAdapter
{
Task<string> PostApiResponseAsync(string url, string? content = null);
Task<string> GetApiResponseAsync(string url, string? content = null);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace GlucoseTray.Models;
namespace GlucoseTray.Domain.FetchResults;

/// <summary>
/// Class that maps to the JSON from NightScout queries.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
using GlucoseTray.Settings;
using GlucoseTray.Domain.DisplayResults;
using GlucoseTray.Domain.Enums;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Text.Json;

namespace GlucoseTray.Services;
namespace GlucoseTray.Domain.FetchResults;

public interface INightscoutService
{
Task<GlucoseResult> GetLatestReadingAsync();
}

public class NightscoutService : INightscoutService
public class NightscoutService(ISettingsProxy options, ILogger<NightscoutService> logger, UrlAssembler urlBuilder, IExternalCommunicationAdapter externalAdapter, DebugService debug) : INightscoutService
{
private readonly ISettingsProxy _options;
private readonly ILogger _logger;
private readonly UrlAssembler _urlBuilder;
private readonly IExternalCommunicationAdapter _externalAdapter;
private readonly DebugService _debug;

public NightscoutService(ISettingsProxy options, ILogger<NightscoutService> logger, UrlAssembler urlBuilder, IExternalCommunicationAdapter externalAdapter, DebugService debug)
{
_options = options;
_logger = logger;
_urlBuilder = urlBuilder;
_externalAdapter = externalAdapter;
_debug = debug;
}
private readonly ISettingsProxy _options = options;
private readonly ILogger _logger = logger;
private readonly UrlAssembler _urlBuilder = urlBuilder;
private readonly IExternalCommunicationAdapter _externalAdapter = externalAdapter;
private readonly DebugService _debug = debug;

public async Task<GlucoseResult> GetLatestReadingAsync()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
using GlucoseTray.Settings;
using GlucoseTray.Domain.Enums;

namespace GlucoseTray.Services;
namespace GlucoseTray.Domain.FetchResults;

public class UrlAssembler
public class UrlAssembler(ISettingsProxy options)
{
private readonly ISettingsProxy _options;

public UrlAssembler(ISettingsProxy options)
{
_options = options;
}
private readonly ISettingsProxy _options = options;

public string BuildNightscoutUrl()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace GlucoseTray.Models;
using GlucoseTray.Domain.Enums;

namespace GlucoseTray.Domain;

public class GlucoseResult
{
Expand Down
13 changes: 13 additions & 0 deletions GlucoseTray.Domain/GlucoseTray.Domain.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

namespace GlucoseTray.Settings
using GlucoseTray.Domain.Enums;

namespace GlucoseTray.Domain
{
public interface ISettingsProxy
{
Expand Down
8 changes: 8 additions & 0 deletions GlucoseTray.Domain/ITaskSchedulerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace GlucoseTray.Domain
{
public interface ITaskSchedulerService
{
bool HasTaskEnabled();
void ToggleTask(bool enable);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using GlucoseTray.Settings;
using GlucoseTray.Domain;
using GlucoseTray.Domain.FetchResults;
using Microsoft.Extensions.Logging;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

namespace GlucoseTray.Services
namespace GlucoseTray.Infrastructure
{
public interface IExternalCommunicationAdapter
{
Task<string> PostApiResponseAsync(string url, string? content = null);
Task<string> GetApiResponseAsync(string url, string? content = null);
}

public class ExternalCommunicationAdapter : IExternalCommunicationAdapter
{
private readonly IHttpClientFactory _httpClientFactory;
Expand Down
18 changes: 18 additions & 0 deletions GlucoseTray.Infrastructure/GlucoseTray.Infrastructure.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="TaskScheduler" Version="2.11.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GlucoseTray.Domain\GlucoseTray.Domain.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.Win32.TaskScheduler;
using System.IO;
using GlucoseTray.Domain;
using Microsoft.Win32.TaskScheduler;

namespace GlucoseTray.Services;
namespace GlucoseTray.Infrastructure;

public class TaskSchedulerService
public class TaskSchedulerService : ITaskSchedulerService
{
private readonly string ExecutablePath = "\"" + Environment.ProcessPath + "\"";
private readonly string TaskName = "GlucoseTray-" + Environment.UserName;
Expand Down
4 changes: 3 additions & 1 deletion GlucoseTray.Tests/AlertServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

using GlucoseTray.Models;
using GlucoseTray.Enums;
using GlucoseTray.Services;
using NSubstitute;
using GlucoseTray.Settings;
using GlucoseTray.Domain.Enums;
using GlucoseTray.Domain;
using GlucoseTray.Domain.DisplayResults;

namespace GlucoseTray.Tests;

Expand Down
3 changes: 2 additions & 1 deletion GlucoseTray.Tests/DebugServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GlucoseTray.Services;
using GlucoseTray.Domain.DisplayResults;
using GlucoseTray.Domain.FetchResults;
using NSubstitute;

namespace GlucoseTray.Tests;
Expand Down
7 changes: 4 additions & 3 deletions GlucoseTray.Tests/GlucoseFetchResultExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GlucoseTray.Enums;
using GlucoseTray.Extensions;
using GlucoseTray.Models;
using GlucoseTray.Domain;
using GlucoseTray.Domain.Enums;
using GlucoseTray.Domain.FetchResults;
using GlucoseTray.Domain.DisplayResults;

namespace GlucoseTray.Tests;

Expand Down
1 change: 1 addition & 0 deletions GlucoseTray.Tests/GlucoseTray.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GlucoseTray.Domain\GlucoseTray.Domain.csproj" />
<ProjectReference Include="..\GlucoseTray\GlucoseTray.csproj" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions GlucoseTray.Tests/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using GlucoseTray.Enums;
using GlucoseTray.Extensions;
using GlucoseTray.Domain.DisplayResults;
using GlucoseTray.Domain.Enums;

namespace GlucoseTray.Tests
{
Expand Down
14 changes: 13 additions & 1 deletion GlucoseTray.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ VisualStudioVersion = 17.0.32014.148
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GlucoseTray", "GlucoseTray\GlucoseTray.csproj", "{0A0B15BE-CA2F-4B32-A338-C8B2DF04060E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GlucoseTray.Tests", "GlucoseTray.Tests\GlucoseTray.Tests.csproj", "{2D51E5C0-484E-4152-8F1B-1C1CAD05B1D9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GlucoseTray.Tests", "GlucoseTray.Tests\GlucoseTray.Tests.csproj", "{2D51E5C0-484E-4152-8F1B-1C1CAD05B1D9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GlucoseTray.Infrastructure", "GlucoseTray.Infrastructure\GlucoseTray.Infrastructure.csproj", "{85E701C4-C9D5-4BA7-B5A0-BDA687F6A07A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GlucoseTray.Domain", "GlucoseTray.Domain\GlucoseTray.Domain.csproj", "{8341109F-EF71-4EA1-9277-FF612A577929}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +25,14 @@ Global
{2D51E5C0-484E-4152-8F1B-1C1CAD05B1D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D51E5C0-484E-4152-8F1B-1C1CAD05B1D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D51E5C0-484E-4152-8F1B-1C1CAD05B1D9}.Release|Any CPU.Build.0 = Release|Any CPU
{85E701C4-C9D5-4BA7-B5A0-BDA687F6A07A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85E701C4-C9D5-4BA7-B5A0-BDA687F6A07A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85E701C4-C9D5-4BA7-B5A0-BDA687F6A07A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85E701C4-C9D5-4BA7-B5A0-BDA687F6A07A}.Release|Any CPU.Build.0 = Release|Any CPU
{8341109F-EF71-4EA1-9277-FF612A577929}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8341109F-EF71-4EA1-9277-FF612A577929}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8341109F-EF71-4EA1-9277-FF612A577929}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8341109F-EF71-4EA1-9277-FF612A577929}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading

0 comments on commit 13a72db

Please sign in to comment.