mirror of
https://github.com/tuxdotrs/highminded.git
synced 2025-08-23 08:01:03 +05:30
feat: initial commit
This commit is contained in:
15
utils/InMemoryDB.cs
Normal file
15
utils/InMemoryDB.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Path = Avalonia.Controls.Shapes.Path;
|
||||
|
||||
namespace highminded.utils;
|
||||
|
||||
public class InMemoryDb
|
||||
{
|
||||
// Initialize Singleton Class
|
||||
InMemoryDb() { }
|
||||
|
||||
public static readonly InMemoryDb Obj = new InMemoryDb();
|
||||
|
||||
public SettingsManager<AppSettings> settingsManager = new SettingsManager<AppSettings>();
|
||||
}
|
51
utils/SetttingsManager.cs
Normal file
51
utils/SetttingsManager.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace highminded.utils;
|
||||
|
||||
public class AppSettings
|
||||
{
|
||||
public string Model { get; set; }
|
||||
public string ApiURL { get; set; }
|
||||
public string ApiKey { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class SettingsManager<T> where T : class, new()
|
||||
{
|
||||
private readonly string _settingsPath;
|
||||
public T Settings { get; private set; }
|
||||
|
||||
public SettingsManager(string appName = "highminded")
|
||||
{
|
||||
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
||||
var appFolder = Path.Combine(appData, appName);
|
||||
Directory.CreateDirectory(appFolder);
|
||||
_settingsPath = Path.Combine(appFolder, "settings.json");
|
||||
Settings = Load();
|
||||
}
|
||||
|
||||
private T Load()
|
||||
{
|
||||
if (!File.Exists(_settingsPath))
|
||||
return new T();
|
||||
|
||||
try
|
||||
{
|
||||
string json = File.ReadAllText(_settingsPath);
|
||||
return JsonSerializer.Deserialize<T>(json) ?? new T();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new T(); // Fallback to default settings if error occurs
|
||||
}
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||
string json = JsonSerializer.Serialize(Settings, options);
|
||||
File.WriteAllText(_settingsPath, json);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user