Files
LehrerApp/LehrerApp.Core/Services/DashboardSettingsService.cs
T

63 lines
2.4 KiB
C#

using System.Text.Json;
namespace LehrerApp.Core.Services;
public sealed class DashboardCardSetting
{
public string Key { get; set; } = "";
public bool IsVisible { get; set; } = true;
public int Order { get; set; }
}
/// <summary>Speichert Sichtbarkeit und Reihenfolge der Dashboard-Kacheln lokal.</summary>
public sealed class DashboardSettingsService
{
public static readonly string[] DefaultCardOrder =
[
"today", "tasks", "calendar", "excuses", "upcoming",
"corrections", "alerts", "attendance", "support", "groups",
];
private readonly string _configPath;
public DashboardSettingsService(string appDataPath) =>
_configPath = Path.Combine(appDataPath, "dashboardsettings.json");
public List<DashboardCardSetting> Load()
{
try
{
if (File.Exists(_configPath))
{
var saved = JsonSerializer.Deserialize<List<DashboardCardSetting>>(
File.ReadAllText(_configPath)) ?? [];
var byKey = saved
.Where(s => DefaultCardOrder.Contains(s.Key))
.GroupBy(s => s.Key).ToDictionary(g => g.Key, g => g.First());
return DefaultCardOrder.Select((key, defaultOrder) => byKey.TryGetValue(key, out var item)
? new DashboardCardSetting { Key = key, IsVisible = item.IsVisible, Order = item.Order }
: new DashboardCardSetting { Key = key, IsVisible = true, Order = defaultOrder })
.OrderBy(s => s.Order).ThenBy(s => Array.IndexOf(DefaultCardOrder, s.Key))
.Select((s, index) => new DashboardCardSetting
{ Key = s.Key, IsVisible = s.IsVisible, Order = index })
.ToList();
}
}
catch { /* beschädigte Konfiguration -> Standardreihenfolge */ }
return DefaultCardOrder.Select((key, index) => new DashboardCardSetting
{ Key = key, IsVisible = true, Order = index }).ToList();
}
public void Save(IEnumerable<DashboardCardSetting> settings)
{
var normalized = settings.Select((s, index) => new DashboardCardSetting
{
Key = s.Key,
IsVisible = s.IsVisible,
Order = index,
}).ToList();
File.WriteAllText(_configPath, JsonSerializer.Serialize(normalized));
}
}