Skip to content

Commit

Permalink
Made file and settings service no longer static.
Browse files Browse the repository at this point in the history
  • Loading branch information
Delubear committed Sep 25, 2024
1 parent 9f35212 commit c91599c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
3 changes: 2 additions & 1 deletion GlucoseTray/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ private static bool LoadApplicationSettings()
{
Environment.SetEnvironmentVariable("windir", Environment.GetEnvironmentVariable("SystemRoot"), EnvironmentVariableTarget.User);
SettingsFile = Application.UserAppDataPath + @"\glucose_tray_settings.json";
if (!File.Exists(SettingsFile) || SettingsService.ValidateSettings().Count != 0)
var setttingsService = new SettingsService();
if (!File.Exists(SettingsFile) || setttingsService.ValidateSettings().Count != 0)
{
var settingsWindow = new SettingsWindow(new SettingsWindowService());
if (settingsWindow.ShowDialog() != true) // Did not want to setup application.
Expand Down
6 changes: 3 additions & 3 deletions GlucoseTray/Services/FileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

namespace GlucoseTray.Services;

public static class FileService<T>
public class FileService<T>
{
public static void WriteModelToJsonFile(T model, string file)
public void WriteModelToJsonFile(T model, string file)
{
using var sw = File.CreateText(file);
var options = new JsonSerializerOptions { WriteIndented = true };
var json = JsonSerializer.Serialize(model, options);
sw.Write(json);
}

public static T? ReadModelFromFile(string file)
public T? ReadModelFromFile(string file)
{
T? model = default;
try
Expand Down
9 changes: 5 additions & 4 deletions GlucoseTray/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

namespace GlucoseTray.Services;

public static class SettingsService
public class SettingsService
{
/// <summary>
/// If model is null, will validate from stored settings file.
/// </summary>
/// <param name="model"></param>
public static List<string> ValidateSettings(GlucoseTraySettings? model = null)
public List<string> ValidateSettings(GlucoseTraySettings? model = null)
{
var errors = new List<string>();

if (model is null)
{
model = FileService<GlucoseTraySettings>.ReadModelFromFile(Program.SettingsFile);
var fileService = new FileService<GlucoseTraySettings>();
model = fileService.ReadModelFromFile(Program.SettingsFile);
if (model is null)
{
errors.Add("File is Invalid");
Expand Down Expand Up @@ -44,7 +45,7 @@ public static List<string> ValidateSettings(GlucoseTraySettings? model = null)
return errors;
}

private static string ValidateNightScout(GlucoseTraySettings model)
private string ValidateNightScout(GlucoseTraySettings model)
{
if (string.IsNullOrWhiteSpace(model.NightscoutUrl))
return "Nightscout Url is missing";
Expand Down
9 changes: 6 additions & 3 deletions GlucoseTray/Services/SettingsWindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ public class SettingsWindowService : ISettingsWindowService
{
public void Save(GlucoseTraySettings settings)
{
FileService<GlucoseTraySettings>.WriteModelToJsonFile(settings, Program.SettingsFile);
var fileService = new FileService<GlucoseTraySettings>();
fileService.WriteModelToJsonFile(settings, Program.SettingsFile);
}

public (bool IsValid, IEnumerable<string> Errors) IsValid(GlucoseTraySettings settings)
{
var errors = SettingsService.ValidateSettings(settings);
var setttingsService = new SettingsService();
var errors = setttingsService.ValidateSettings(settings);
if (errors.Any())
return (false, errors);
return (true, errors);
Expand Down Expand Up @@ -92,7 +94,8 @@ public IEnumerable<string> GetDexComServerLocationDescriptions()
{
try
{
model = FileService<GlucoseTraySettings>.ReadModelFromFile(Program.SettingsFile);
var fileService = new FileService<GlucoseTraySettings>();
model = fileService.ReadModelFromFile(Program.SettingsFile);

if (model is null)
MessageBox.Show("Unable to load existing settings due to a bad file.");
Expand Down

0 comments on commit c91599c

Please sign in to comment.