29 lines
992 B
C#
29 lines
992 B
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using WorkTime.Models;
|
|
using WorkTime.Models.Repositories;
|
|
|
|
namespace WorkTime.Database.Repositories;
|
|
|
|
internal sealed class SettingsRepository(string filePath) : ISettingsRepository {
|
|
public async Task<Settings> LoadSettings() {
|
|
if (!File.Exists(filePath))
|
|
return new Settings();
|
|
|
|
var raw = await File.ReadAllTextAsync(filePath);
|
|
return JsonSerializer.Deserialize<Settings>(raw, SettingsSerializer.Default.Settings) ?? new Settings();
|
|
}
|
|
|
|
public async Task SaveSettings(Settings settings) {
|
|
if (File.Exists(filePath))
|
|
File.Delete(filePath);
|
|
|
|
var json = JsonSerializer.Serialize(settings, SettingsSerializer.Default.Settings);
|
|
await File.WriteAllTextAsync(filePath, json);
|
|
}
|
|
}
|
|
|
|
[JsonSourceGenerationOptions(WriteIndented = true)]
|
|
[JsonSerializable(typeof(Settings))]
|
|
internal partial class SettingsSerializer : JsonSerializerContext {}
|