64 lines
2.0 KiB
C#
64 lines
2.0 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 int _pendingCount;
|
|
[ObservableProperty] private int _conflictCount;
|
|
[ObservableProperty] private bool _isSyncing;
|
|
[ObservableProperty] private bool _hasConflicts;
|
|
[ObservableProperty] private bool _isServerConfigured;
|
|
|
|
public SyncStatusViewModel(SyncEngine? engine)
|
|
{
|
|
_engine = engine;
|
|
IsServerConfigured = engine is not null;
|
|
|
|
if (_engine is not null)
|
|
{
|
|
_engine.StatusChanged += OnStatusChanged;
|
|
OnStatusChanged(_engine.Status);
|
|
}
|
|
}
|
|
|
|
private void OnStatusChanged(SyncStatus status)
|
|
{
|
|
IsSyncing = status.State == SyncState.Syncing;
|
|
HasConflicts = status.ConflictCount > 0;
|
|
PendingCount = status.PendingEvents;
|
|
ConflictCount = status.ConflictCount;
|
|
|
|
StatusText = status.State switch
|
|
{
|
|
SyncState.Idle => PendingCount > 0
|
|
? $"{PendingCount} ausstehend"
|
|
: "Synchronisiert",
|
|
SyncState.Syncing => "Synchronisiere...",
|
|
SyncState.Offline => "Offline",
|
|
SyncState.Error => $"Fehler: {status.ErrorMessage}",
|
|
_ => "",
|
|
};
|
|
|
|
LastSyncText = status.LastSyncAt.HasValue
|
|
? $"Zuletzt: {status.LastSyncAt:HH:mm}"
|
|
: "Noch nie synchronisiert";
|
|
}
|
|
|
|
[RelayCommand(CanExecute = nameof(CanSyncNow))]
|
|
private async Task SyncNow()
|
|
{
|
|
if (_engine is null) return;
|
|
await _engine.SyncNowAsync(isAutomatic: false);
|
|
}
|
|
|
|
private bool CanSyncNow() => _engine is not null && !IsSyncing;
|
|
}
|