using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using LehrerApp.Sync; using LehrerApp.Sync.Models; namespace LehrerApp.Desktop.ViewModels; public partial class SyncStatusViewModel : ObservableObject { private readonly SyncEngine? _engine; [ObservableProperty] private string _statusText = "Kein Server konfiguriert"; [ObservableProperty] private string _lastSyncText = ""; [ObservableProperty] private bool _isSyncing; [ObservableProperty] private bool _isServerConfigured; [ObservableProperty] private int _pendingCount; public SyncStatusViewModel(SyncEngine? engine) { _engine = engine; IsServerConfigured = engine is not null; if (_engine is not null) _engine.StatusChanged += OnStatus; } private void OnStatus(SyncStatus s) { IsSyncing = s.State == SyncState.Syncing; PendingCount = s.PendingEvents; StatusText = s.State switch { SyncState.Idle => PendingCount > 0 ? $"{PendingCount} ausstehend" : "Synchronisiert", SyncState.Syncing => "Synchronisiere…", SyncState.Offline => "Offline", SyncState.Error => $"Fehler: {s.ErrorMessage}", _ => "", }; LastSyncText = s.LastSyncAt.HasValue ? $"Zuletzt: {s.LastSyncAt:HH:mm}" : "Noch nie"; } [RelayCommand(CanExecute = nameof(CanSync))] private async Task SyncNow() { if (_engine is not null) await _engine.SyncNowAsync(); } private bool CanSync() => _engine is not null && !IsSyncing; }