SyncEngine feuert sein erstes StatusChanged bereits im eigenen Konstruktor. Da SyncEngine und SyncStatusViewModel beide DI-Singletons sind und Letzterer Ersteren erst innerhalb der eigenen Factory aus dem Container holt, lief dieser erste Broadcast ab, bevor SyncStatusViewModel überhaupt abonniert hatte - der Status ging verloren, StatusText blieb bis zum nächsten Auto-Sync oder manuellen Sync beim hartcodierten Default "Kein Server konfiguriert". Fix: Konstruktor ruft nach dem Abonnieren zusätzlich einmal OnStatus(engine.Status) mit dem bereits vorhandenen aktuellen Zustand auf. Regressionstest ergänzt - dabei fehlte LehrerApp.Desktop.Tests das DisableTestParallelization-Attribut (gleicher bekannter LiteDB-BsonMapper.Global-Bug wie in den anderen Testprojekten, sobald zwei Testklassen parallel LiteDbContext konstruieren). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
53 lines
2.1 KiB
C#
53 lines
2.1 KiB
C#
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;
|
|
// SyncEngine feuert sein erstes StatusChanged bereits im eigenen Konstruktor
|
|
// (UpdateStatus) - der läuft aber schon, während wir hier noch in GetService<SyncEngine>()
|
|
// stecken, also VOR dem obigen Abonnieren. Ohne diesen Nachhol-Aufruf bliebe StatusText
|
|
// nach jedem Neustart beim Default "Kein Server konfiguriert", bis der nächste
|
|
// Auto-Sync oder Klick auf "Jetzt synchronisieren" den Text erstmals aktualisiert.
|
|
OnStatus(_engine.Status);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|