| | | 1 | | using Avalonia.Controls; |
| | | 2 | | using Avalonia.Threading; |
| | | 3 | | using CommunityToolkit.Mvvm.ComponentModel; |
| | | 4 | | using CommunityToolkit.Mvvm.Input; |
| | | 5 | | using LOCKnet.Core.DataAbstractions; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.Security; |
| | | 8 | | |
| | | 9 | | namespace LOCKnet.App.ViewModels; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// ViewModel für die Credential-Liste (Hauptansicht nach dem Login). |
| | | 13 | | /// </summary> |
| | | 14 | | public partial class CredentialListViewModel : ViewModelBase |
| | | 15 | | { |
| | | 16 | | public event EventHandler? LockRequested; |
| | | 17 | | public event EventHandler<CredentialRecord?>? AddEditRequested; |
| | | 18 | | public event EventHandler? TutorialRequested; |
| | | 19 | | |
| | 3 | 20 | | private IReadOnlyList<CredentialRecord> _allCredentials = []; |
| | | 21 | | private DispatcherTimer? _countdownTimer; |
| | | 22 | | |
| | | 23 | | [ObservableProperty] |
| | 3 | 24 | | private ObservableCollection<CredentialRecord> _credentials = []; |
| | | 25 | | |
| | | 26 | | [ObservableProperty] |
| | | 27 | | [NotifyCanExecuteChangedFor(nameof(EditCommand))] |
| | | 28 | | [NotifyCanExecuteChangedFor(nameof(DeleteCommand))] |
| | | 29 | | [NotifyCanExecuteChangedFor(nameof(CopyPasswordCommand))] |
| | | 30 | | private CredentialRecord? _selectedCredential; |
| | | 31 | | |
| | | 32 | | [ObservableProperty] |
| | 3 | 33 | | private string _searchText = string.Empty; |
| | | 34 | | |
| | | 35 | | [ObservableProperty] |
| | 3 | 36 | | private string _statusMessage = string.Empty; |
| | | 37 | | |
| | | 38 | | [ObservableProperty] |
| | 3 | 39 | | private string _storageCleanupMessage = string.Empty; |
| | | 40 | | |
| | | 41 | | [ObservableProperty] |
| | | 42 | | private bool _isStorageCleanupPending; |
| | | 43 | | |
| | | 44 | | [ObservableProperty] |
| | | 45 | | private bool _isRetryingStorageCleanup; |
| | | 46 | | |
| | | 47 | | [ObservableProperty] |
| | 3 | 48 | | private string _lockTimerText = string.Empty; |
| | | 49 | | |
| | 3 | 50 | | public CredentialListViewModel() |
| | 3 | 51 | | { |
| | 3 | 52 | | Refresh(); |
| | 3 | 53 | | RefreshStorageCleanupState(); |
| | 3 | 54 | | StartCountdownTimer(); |
| | 3 | 55 | | } |
| | | 56 | | |
| | | 57 | | // ── Countdown ───────────────────────────────────────────────────────────── |
| | | 58 | | |
| | | 59 | | private void StartCountdownTimer() |
| | 3 | 60 | | { |
| | 3 | 61 | | _countdownTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) }; |
| | 3 | 62 | | _countdownTimer.Tick += (_, _) => UpdateLockTimer(); |
| | 3 | 63 | | _countdownTimer.Start(); |
| | 3 | 64 | | UpdateLockTimer(); |
| | 3 | 65 | | } |
| | | 66 | | |
| | | 67 | | private void UpdateLockTimer() |
| | 6 | 68 | | { |
| | 6 | 69 | | var monitor = AppServices.Current.ActivityMonitor; |
| | 6 | 70 | | if (!monitor.IsRunning) |
| | 4 | 71 | | { |
| | 4 | 72 | | LockTimerText = string.Empty; |
| | 4 | 73 | | return; |
| | | 74 | | } |
| | 2 | 75 | | var elapsed = DateTimeOffset.UtcNow - monitor.LastActivity; |
| | 2 | 76 | | var remaining = monitor.Timeout - elapsed; |
| | 2 | 77 | | if (remaining <= TimeSpan.Zero) |
| | 1 | 78 | | { |
| | 1 | 79 | | LockTimerText = "⏱ 0:00"; |
| | 1 | 80 | | return; |
| | | 81 | | } |
| | 1 | 82 | | LockTimerText = $"⏱ {(int)remaining.TotalMinutes}:{remaining.Seconds:D2}"; |
| | 6 | 83 | | } |
| | | 84 | | |
| | | 85 | | |
| | | 86 | | public void Refresh() |
| | 5 | 87 | | { |
| | | 88 | | try |
| | 5 | 89 | | { |
| | 5 | 90 | | _allCredentials = AppServices.Current.CredentialService.GetAll(); |
| | 4 | 91 | | ApplyFilter(); |
| | 4 | 92 | | } |
| | 1 | 93 | | catch (Exception ex) |
| | 1 | 94 | | { |
| | 1 | 95 | | StatusMessage = ex.Message; |
| | 1 | 96 | | } |
| | | 97 | | finally |
| | 5 | 98 | | { |
| | 5 | 99 | | RefreshStorageCleanupState(); |
| | 5 | 100 | | } |
| | 5 | 101 | | } |
| | | 102 | | |
| | 2 | 103 | | partial void OnSearchTextChanged(string value) => ApplyFilter(); |
| | | 104 | | |
| | | 105 | | private void ApplyFilter() |
| | 6 | 106 | | { |
| | 6 | 107 | | var filtered = string.IsNullOrWhiteSpace(SearchText) |
| | 6 | 108 | | ? _allCredentials |
| | 6 | 109 | | : _allCredentials.Where(c => |
| | 9 | 110 | | c.Title.Contains(SearchText, StringComparison.OrdinalIgnoreCase) || |
| | 9 | 111 | | (c.Username?.Contains(SearchText, StringComparison.OrdinalIgnoreCase) ?? false) || |
| | 9 | 112 | | (c.Url?.Contains(SearchText, StringComparison.OrdinalIgnoreCase) ?? false)); |
| | | 113 | | |
| | 6 | 114 | | Credentials = new ObservableCollection<CredentialRecord>(filtered); |
| | 6 | 115 | | } |
| | | 116 | | |
| | | 117 | | // ── Commands ────────────────────────────────────────────────────────────── |
| | | 118 | | |
| | | 119 | | [RelayCommand] |
| | 1 | 120 | | private void Add() => AddEditRequested?.Invoke(this, null); |
| | | 121 | | |
| | | 122 | | [RelayCommand(CanExecute = nameof(HasSelection))] |
| | 1 | 123 | | private void Edit() => AddEditRequested?.Invoke(this, SelectedCredential); |
| | | 124 | | |
| | | 125 | | [RelayCommand(CanExecute = nameof(HasSelection))] |
| | | 126 | | private void Delete() |
| | 2 | 127 | | { |
| | 2 | 128 | | if (SelectedCredential is null) return; |
| | | 129 | | try |
| | 2 | 130 | | { |
| | 2 | 131 | | AppServices.Current.CredentialService.Remove(SelectedCredential.Id); |
| | 1 | 132 | | StatusMessage = $"\"{SelectedCredential.Title}\" gelöscht."; |
| | 1 | 133 | | Refresh(); |
| | 1 | 134 | | } |
| | 1 | 135 | | catch (Exception ex) |
| | 1 | 136 | | { |
| | 1 | 137 | | StatusMessage = ex.Message; |
| | 1 | 138 | | } |
| | 2 | 139 | | } |
| | | 140 | | |
| | | 141 | | [RelayCommand(CanExecute = nameof(HasSelection))] |
| | | 142 | | private void CopyPassword() |
| | 3 | 143 | | { |
| | 3 | 144 | | if (SelectedCredential is null) return; |
| | 3 | 145 | | if (SelectedCredential.CredentialType == CredentialType.BackupCodes) |
| | 0 | 146 | | { |
| | 0 | 147 | | StatusMessage = "Backup-Codes bitte in der Bearbeitungsansicht kopieren."; |
| | 0 | 148 | | return; |
| | | 149 | | } |
| | | 150 | | try |
| | 3 | 151 | | { |
| | 3 | 152 | | var pw = AppServices.Current.CredentialService.GetPassword(SelectedCredential.Id); |
| | 2 | 153 | | if (pw is null) |
| | 1 | 154 | | { |
| | 1 | 155 | | StatusMessage = "Passwort nicht gefunden."; |
| | 1 | 156 | | return; |
| | | 157 | | } |
| | | 158 | | |
| | 1 | 159 | | var text = SecureStringToString(pw); |
| | 1 | 160 | | var clipboard = Avalonia.Application.Current?.ApplicationLifetime is Avalonia.Controls.ApplicationLifetimes.IClass |
| | 1 | 161 | | if (clipboard is not null) _ = clipboard.SetTextAsync(text); |
| | 1 | 162 | | StatusMessage = "Passwort kopiert."; |
| | | 163 | | |
| | | 164 | | // Clipboard nach 30s leeren |
| | 1 | 165 | | _ = Task.Delay(TimeSpan.FromSeconds(30)).ContinueWith(_ => |
| | 1 | 166 | | Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(async () => { if (clipboard is not null) await clipboard.Clea |
| | 1 | 167 | | } |
| | 1 | 168 | | catch (Exception ex) |
| | 1 | 169 | | { |
| | 1 | 170 | | StatusMessage = ex.Message; |
| | 1 | 171 | | } |
| | 3 | 172 | | } |
| | | 173 | | |
| | | 174 | | [RelayCommand] |
| | 1 | 175 | | private void ShowTutorial() => TutorialRequested?.Invoke(this, EventArgs.Empty); |
| | | 176 | | |
| | | 177 | | [RelayCommand(CanExecute = nameof(CanRetryStorageCleanup))] |
| | | 178 | | private async Task RetryStorageCleanupAsync() |
| | 1 | 179 | | { |
| | 1 | 180 | | if (!IsStorageCleanupPending || IsRetryingStorageCleanup) |
| | 1 | 181 | | return; |
| | | 182 | | |
| | | 183 | | try |
| | 0 | 184 | | { |
| | 0 | 185 | | IsRetryingStorageCleanup = true; |
| | 0 | 186 | | var info = await Task.Run(() => AppServices.Current.MasterKeyManager.RetryPendingStorageCompaction()); |
| | 0 | 187 | | RefreshStorageCleanupState(); |
| | 0 | 188 | | StatusMessage = info.IsPending ? string.Empty : info.UserMessage; |
| | 0 | 189 | | } |
| | 0 | 190 | | catch (Exception ex) |
| | 0 | 191 | | { |
| | 0 | 192 | | StatusMessage = ex.Message; |
| | 0 | 193 | | } |
| | | 194 | | finally |
| | 0 | 195 | | { |
| | 0 | 196 | | IsRetryingStorageCleanup = false; |
| | 0 | 197 | | RetryStorageCleanupCommand.NotifyCanExecuteChanged(); |
| | 0 | 198 | | } |
| | 1 | 199 | | } |
| | | 200 | | |
| | | 201 | | [RelayCommand] |
| | | 202 | | private void Lock() |
| | 1 | 203 | | { |
| | 1 | 204 | | AppServices.Current.ActivityMonitor.Stop(); |
| | 1 | 205 | | AppServices.Current.SessionManager.Lock(); |
| | 1 | 206 | | LockRequested?.Invoke(this, EventArgs.Empty); |
| | 1 | 207 | | } |
| | | 208 | | |
| | 0 | 209 | | private bool HasSelection() => SelectedCredential is not null; |
| | | 210 | | |
| | 1 | 211 | | private bool CanRetryStorageCleanup() => IsStorageCleanupPending && !IsRetryingStorageCleanup; |
| | | 212 | | |
| | | 213 | | // ── Helpers ─────────────────────────────────────────────────────────────── |
| | | 214 | | |
| | | 215 | | private void RefreshStorageCleanupState() |
| | 8 | 216 | | { |
| | 8 | 217 | | var info = AppServices.Current.MasterKeyManager.GetStorageCompactionInfo(); |
| | 8 | 218 | | IsStorageCleanupPending = info.IsPending; |
| | 8 | 219 | | StorageCleanupMessage = info.UserMessage; |
| | 8 | 220 | | RetryStorageCleanupCommand.NotifyCanExecuteChanged(); |
| | 8 | 221 | | } |
| | | 222 | | |
| | | 223 | | private static string SecureStringToString(SecureString s) |
| | 1 | 224 | | { |
| | 1 | 225 | | var ptr = System.Runtime.InteropServices.Marshal.SecureStringToGlobalAllocUnicode(s); |
| | 2 | 226 | | try { return System.Runtime.InteropServices.Marshal.PtrToStringUni(ptr) ?? string.Empty; } |
| | 3 | 227 | | finally { System.Runtime.InteropServices.Marshal.ZeroFreeGlobalAllocUnicode(ptr); } |
| | 1 | 228 | | } |
| | | 229 | | } |