SimpleMessagingService

Log | Files | Refs | README

SettingsInterface.cs (1414B)


      1 using System.Diagnostics;
      2 
      3 namespace Client.Utilities;
      4 
      5 public interface ISettingsInterface
      6 {
      7     Task<T?> LoadSettingsAsync<T>();
      8     Task SaveSettingsAsync<T>(T settings);
      9 }
     10 
     11 public class SettingsInterface : ISettingsInterface
     12 {
     13     private readonly string _settingsPath = "settings.json";
     14 
     15     public async Task<T?> LoadSettingsAsync<T>()
     16     {
     17         try
     18         {
     19             var json = await System.IO.File.ReadAllTextAsync(_settingsPath);
     20             return System.Text.Json.JsonSerializer.Deserialize<T>(json);
     21         }
     22         catch (Exception e)
     23         {
     24             Debug.WriteLine(e.Message);
     25             return default;
     26         }
     27     }
     28 
     29     public async Task SaveSettingsAsync<T>(T settings)
     30     {
     31         try
     32         {
     33             var json = System.Text.Json.JsonSerializer.Serialize(settings);
     34             await System.IO.File.WriteAllTextAsync(_settingsPath, json);
     35         }
     36         catch (Exception e)
     37         {
     38             Debug.WriteLine(e.Message);
     39         }
     40     }
     41 }
     42 
     43 public class Settings
     44 {
     45     public List<Server> Servers { get; set; } = new List<Server>();
     46     public Guid? SelectedServer { get; set; }
     47     public int PollingInterval { get; set; } = 1000;
     48 }
     49 
     50 public class Server
     51 {
     52     public Guid serverId { get; set; } = Guid.NewGuid();
     53     public string? Name { get; set; }
     54     public string? User { get; set; }
     55     public Guid? Key { get; set; }
     56     public Uri? Url { get; set; }
     57 }