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; } } /// Speichert Sichtbarkeit und Reihenfolge der Dashboard-Kacheln lokal. public sealed class DashboardSettingsService { public static readonly string[] DefaultCardOrder = [ "today", "tasks", "calendar", "excuses", "upcoming", "corrections", "unplanned", "alerts", "attendance", "support", "groups", "examload", ]; private readonly string _configPath; public DashboardSettingsService(string appDataPath) => _configPath = Path.Combine(appDataPath, "dashboardsettings.json"); public List Load() { try { if (File.Exists(_configPath)) { var saved = JsonSerializer.Deserialize>( 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 settings) { var normalized = settings.Select((s, index) => new DashboardCardSetting { Key = s.Key, IsVisible = s.IsVisible, Order = index, }).ToList(); File.WriteAllText(_configPath, JsonSerializer.Serialize(normalized)); } }