From c91599c6a9d901a5695be66a3fafbef5bfd1627f Mon Sep 17 00:00:00 2001 From: Chris Baker Date: Wed, 25 Sep 2024 09:32:38 -0500 Subject: [PATCH] Made file and settings service no longer static. --- GlucoseTray/Program.cs | 3 ++- GlucoseTray/Services/FileService.cs | 6 +++--- GlucoseTray/Services/SettingsService.cs | 9 +++++---- GlucoseTray/Services/SettingsWindowService.cs | 9 ++++++--- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/GlucoseTray/Program.cs b/GlucoseTray/Program.cs index 50baa66..4012d83 100644 --- a/GlucoseTray/Program.cs +++ b/GlucoseTray/Program.cs @@ -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. diff --git a/GlucoseTray/Services/FileService.cs b/GlucoseTray/Services/FileService.cs index 712f648..046b827 100644 --- a/GlucoseTray/Services/FileService.cs +++ b/GlucoseTray/Services/FileService.cs @@ -3,9 +3,9 @@ namespace GlucoseTray.Services; -public static class FileService +public class FileService { - 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 }; @@ -13,7 +13,7 @@ public static void WriteModelToJsonFile(T model, string file) sw.Write(json); } - public static T? ReadModelFromFile(string file) + public T? ReadModelFromFile(string file) { T? model = default; try diff --git a/GlucoseTray/Services/SettingsService.cs b/GlucoseTray/Services/SettingsService.cs index 85b5247..9d6bba4 100644 --- a/GlucoseTray/Services/SettingsService.cs +++ b/GlucoseTray/Services/SettingsService.cs @@ -4,19 +4,20 @@ namespace GlucoseTray.Services; -public static class SettingsService +public class SettingsService { /// /// If model is null, will validate from stored settings file. /// /// - public static List ValidateSettings(GlucoseTraySettings? model = null) + public List ValidateSettings(GlucoseTraySettings? model = null) { var errors = new List(); if (model is null) { - model = FileService.ReadModelFromFile(Program.SettingsFile); + var fileService = new FileService(); + model = fileService.ReadModelFromFile(Program.SettingsFile); if (model is null) { errors.Add("File is Invalid"); @@ -44,7 +45,7 @@ public static List 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"; diff --git a/GlucoseTray/Services/SettingsWindowService.cs b/GlucoseTray/Services/SettingsWindowService.cs index 2204ec2..4782628 100644 --- a/GlucoseTray/Services/SettingsWindowService.cs +++ b/GlucoseTray/Services/SettingsWindowService.cs @@ -21,12 +21,14 @@ public class SettingsWindowService : ISettingsWindowService { public void Save(GlucoseTraySettings settings) { - FileService.WriteModelToJsonFile(settings, Program.SettingsFile); + var fileService = new FileService(); + fileService.WriteModelToJsonFile(settings, Program.SettingsFile); } public (bool IsValid, IEnumerable 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); @@ -92,7 +94,8 @@ public IEnumerable GetDexComServerLocationDescriptions() { try { - model = FileService.ReadModelFromFile(Program.SettingsFile); + var fileService = new FileService(); + model = fileService.ReadModelFromFile(Program.SettingsFile); if (model is null) MessageBox.Show("Unable to load existing settings due to a bad file.");