Add club settings and use club name in PDF exports
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using System.Text.Json;
|
||||
using Duempelkas.App.Services;
|
||||
|
||||
namespace Duempelkas.Infrastructure.Services;
|
||||
|
||||
public class SettingsService : ISettingsService
|
||||
{
|
||||
private static readonly string SettingsPath = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
||||
"Duempelkas", "settings.json");
|
||||
|
||||
private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true };
|
||||
|
||||
public async Task<string?> GetClubNameAsync()
|
||||
{
|
||||
var settings = await LoadAsync();
|
||||
return settings.ClubName;
|
||||
}
|
||||
|
||||
public async Task SetClubNameAsync(string? clubName)
|
||||
{
|
||||
var settings = await LoadAsync();
|
||||
settings.ClubName = string.IsNullOrWhiteSpace(clubName) ? null : clubName.Trim();
|
||||
await SaveAsync(settings);
|
||||
}
|
||||
|
||||
private static async Task<AppSettings> LoadAsync()
|
||||
{
|
||||
if (!File.Exists(SettingsPath))
|
||||
return new AppSettings();
|
||||
|
||||
try
|
||||
{
|
||||
var json = await File.ReadAllTextAsync(SettingsPath);
|
||||
return JsonSerializer.Deserialize<AppSettings>(json) ?? new AppSettings();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new AppSettings();
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task SaveAsync(AppSettings settings)
|
||||
{
|
||||
var dir = Path.GetDirectoryName(SettingsPath)!;
|
||||
Directory.CreateDirectory(dir);
|
||||
var json = JsonSerializer.Serialize(settings, JsonOptions);
|
||||
await File.WriteAllTextAsync(SettingsPath, json);
|
||||
}
|
||||
|
||||
private class AppSettings
|
||||
{
|
||||
public string? ClubName { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user