using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using cfs_tracker.Data; using cfs_tracker.Services; namespace cfs_tracker.ViewModels; public partial class OptionsViewModel : ObservableObject { private readonly HealthEntryRepository _healthRepository; private readonly AppearanceRepository _appearanceRepository; private readonly AppearanceService _appearanceService; [ObservableProperty] private string deleteConfirmationText = string.Empty; [ObservableProperty] private string statusText = string.Empty; public ObservableCollection AppearanceColors { get; } = new(); public ObservableCollection StatusTexts { get; } = new(); public IAsyncRelayCommand DeleteAllDataCommand { get; } public IAsyncRelayCommand SaveAppearanceCommand { get; } public IAsyncRelayCommand ResetAppearanceCommand { get; } public OptionsViewModel( HealthEntryRepository healthRepository, AppearanceRepository appearanceRepository, AppearanceService appearanceService) { _healthRepository = healthRepository; _appearanceRepository = appearanceRepository; _appearanceService = appearanceService; DeleteAllDataCommand = new AsyncRelayCommand(DeleteAllDataAsync, CanDeleteAllData); SaveAppearanceCommand = new AsyncRelayCommand(SaveAppearanceAsync); ResetAppearanceCommand = new AsyncRelayCommand(ResetAppearanceAsync); } partial void OnDeleteConfirmationTextChanged(string value) { DeleteAllDataCommand.NotifyCanExecuteChanged(); } public async Task LoadAsync() { await LoadAppearanceAsync(); } private bool CanDeleteAllData() { return string.Equals(DeleteConfirmationText?.Trim(), "löschen", StringComparison.OrdinalIgnoreCase); } private async Task DeleteAllDataAsync() { await _healthRepository.DeleteAllAsync(); DeleteConfirmationText = string.Empty; StatusText = "Alle gespeicherten Einträge wurden gelöscht."; } private async Task LoadAppearanceAsync() { AppearanceColors.Clear(); StatusTexts.Clear(); var all = await _appearanceRepository.GetAllAsync(); AddColor(all, "AppBackground", "App-Hintergrund"); AddColor(all, "PanelBackground", "Panel-Hintergrund"); AddColor(all, "PanelBackground2", "Sekundäre Fläche"); AddColor(all, "PanelBorder", "Panel-Rand"); AddColor(all, "TextPrimary", "Haupttext"); AddColor(all, "TextMuted", "Gedimmter Text"); AddColor(all, "StateGreen", "Statusfarbe Grün"); AddColor(all, "StateYellow", "Statusfarbe Gelb"); AddColor(all, "StateRed", "Statusfarbe Rot"); AddColor(all, "StateGray", "Statusfarbe Grau"); AddText(all, "StatusGreenText", "Text für Grün"); AddText(all, "StatusYellowText", "Text für Gelb"); AddText(all, "StatusRedText", "Text für Rot"); AddText(all, "StatusGrayText", "Text für Grau"); } private void AddColor(List all, string key, string title) { var value = all.FirstOrDefault(x => x.Key == key)?.Value ?? string.Empty; AppearanceColors.Add(new AppearanceColorItemViewModel { Key = key, Title = title, Value = value }); } private void AddText(List all, string key, string title) { var value = all.FirstOrDefault(x => x.Key == key)?.Value ?? string.Empty; StatusTexts.Add(new AppearanceTextItemViewModel { Key = key, Title = title, Value = value }); } private async Task SaveAppearanceAsync() { foreach (var item in AppearanceColors) await _appearanceService.SaveColorAsync(item.Key, item.Value); foreach (var item in StatusTexts) await _appearanceService.SaveTextAsync(item.Key, item.Value); StatusText = "Aussehen wurde gespeichert."; } private async Task ResetAppearanceAsync() { await _appearanceService.ResetAsync(); await LoadAppearanceAsync(); StatusText = "Aussehen wurde auf Standard zurückgesetzt."; } }