| | | 1 | | using Chronicis.Client.Abstractions; |
| | | 2 | | using Chronicis.Client.Components.World; |
| | | 3 | | using Chronicis.Client.Services; |
| | | 4 | | using Chronicis.Shared.DTOs; |
| | | 5 | | using Chronicis.Shared.Extensions; |
| | | 6 | | using MudBlazor; |
| | | 7 | | |
| | | 8 | | namespace Chronicis.Client.ViewModels; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// ViewModel managing document CRUD for a world's Resources tab. |
| | | 12 | | /// </summary> |
| | | 13 | | public sealed class WorldDocumentsViewModel : ViewModelBase |
| | | 14 | | { |
| | | 15 | | private readonly IWorldApiService _worldApi; |
| | | 16 | | private readonly ITreeStateService _treeState; |
| | | 17 | | private readonly IUserNotifier _notifier; |
| | | 18 | | private readonly IDialogService _dialogService; |
| | | 19 | | private readonly IAppNavigator _navigator; |
| | | 20 | | private readonly ILogger<WorldDocumentsViewModel> _logger; |
| | | 21 | | |
| | | 22 | | private Guid _worldId; |
| | 32 | 23 | | private List<WorldDocumentDto> _documents = new(); |
| | | 24 | | private Guid? _editingDocumentId; |
| | 32 | 25 | | private string _editDocumentTitle = string.Empty; |
| | 32 | 26 | | private string _editDocumentDescription = string.Empty; |
| | | 27 | | private bool _isSavingDocument; |
| | | 28 | | |
| | 32 | 29 | | public WorldDocumentsViewModel( |
| | 32 | 30 | | IWorldApiService worldApi, |
| | 32 | 31 | | ITreeStateService treeState, |
| | 32 | 32 | | IUserNotifier notifier, |
| | 32 | 33 | | IDialogService dialogService, |
| | 32 | 34 | | IAppNavigator navigator, |
| | 32 | 35 | | ILogger<WorldDocumentsViewModel> logger) |
| | | 36 | | { |
| | 32 | 37 | | _worldApi = worldApi; |
| | 32 | 38 | | _treeState = treeState; |
| | 32 | 39 | | _notifier = notifier; |
| | 32 | 40 | | _dialogService = dialogService; |
| | 32 | 41 | | _navigator = navigator; |
| | 32 | 42 | | _logger = logger; |
| | 32 | 43 | | } |
| | | 44 | | |
| | | 45 | | public List<WorldDocumentDto> Documents |
| | | 46 | | { |
| | 7 | 47 | | get => _documents; |
| | 18 | 48 | | private set => SetField(ref _documents, value); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public Guid? EditingDocumentId |
| | | 52 | | { |
| | 10 | 53 | | get => _editingDocumentId; |
| | 6 | 54 | | private set => SetField(ref _editingDocumentId, value); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public string EditDocumentTitle |
| | | 58 | | { |
| | 4 | 59 | | get => _editDocumentTitle; |
| | 7 | 60 | | set => SetField(ref _editDocumentTitle, value); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public string EditDocumentDescription |
| | | 64 | | { |
| | 3 | 65 | | get => _editDocumentDescription; |
| | 6 | 66 | | set => SetField(ref _editDocumentDescription, value); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public bool IsSavingDocument |
| | | 70 | | { |
| | 1 | 71 | | get => _isSavingDocument; |
| | 4 | 72 | | private set => SetField(ref _isSavingDocument, value); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary>Loads documents for the specified world.</summary> |
| | | 76 | | public async Task LoadAsync(Guid worldId) |
| | | 77 | | { |
| | | 78 | | _worldId = worldId; |
| | | 79 | | Documents = await _worldApi.GetWorldDocumentsAsync(worldId); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public async Task OpenUploadDialogAsync(Guid worldId) |
| | | 83 | | { |
| | | 84 | | var parameters = new DialogParameters { { "WorldId", worldId } }; |
| | | 85 | | var options = new DialogOptions { MaxWidth = MaxWidth.Small, FullWidth = true }; |
| | | 86 | | |
| | | 87 | | var dialog = await _dialogService.ShowAsync<WorldDocumentUploadDialog>("Upload Document", parameters, options); |
| | | 88 | | var result = await dialog.Result; |
| | | 89 | | |
| | | 90 | | if (result != null && !result.Canceled) |
| | | 91 | | { |
| | | 92 | | Documents = await _worldApi.GetWorldDocumentsAsync(worldId); |
| | | 93 | | await _treeState.RefreshAsync(); |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | public void StartEditDocument(WorldDocumentDto document) |
| | | 98 | | { |
| | 4 | 99 | | EditingDocumentId = document.Id; |
| | 4 | 100 | | EditDocumentTitle = document.Title; |
| | 4 | 101 | | EditDocumentDescription = document.Description ?? string.Empty; |
| | 4 | 102 | | } |
| | | 103 | | |
| | | 104 | | public void CancelDocumentEdit() |
| | | 105 | | { |
| | 1 | 106 | | EditingDocumentId = null; |
| | 1 | 107 | | EditDocumentTitle = string.Empty; |
| | 1 | 108 | | EditDocumentDescription = string.Empty; |
| | 1 | 109 | | } |
| | | 110 | | |
| | | 111 | | public async Task SaveDocumentEditAsync() |
| | | 112 | | { |
| | | 113 | | if (EditingDocumentId == null) |
| | | 114 | | return; |
| | | 115 | | |
| | | 116 | | IsSavingDocument = true; |
| | | 117 | | |
| | | 118 | | try |
| | | 119 | | { |
| | | 120 | | var updateDto = new WorldDocumentUpdateDto |
| | | 121 | | { |
| | | 122 | | Title = EditDocumentTitle, |
| | | 123 | | Description = EditDocumentDescription |
| | | 124 | | }; |
| | | 125 | | |
| | | 126 | | var updated = await _worldApi.UpdateDocumentAsync(_worldId, EditingDocumentId.Value, updateDto); |
| | | 127 | | |
| | | 128 | | if (updated != null) |
| | | 129 | | { |
| | | 130 | | var doc = _documents.FirstOrDefault(d => d.Id == EditingDocumentId.Value); |
| | | 131 | | if (doc != null) |
| | | 132 | | { |
| | | 133 | | doc.Title = updated.Title; |
| | | 134 | | doc.Description = updated.Description; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | EditingDocumentId = null; |
| | | 138 | | EditDocumentTitle = string.Empty; |
| | | 139 | | EditDocumentDescription = string.Empty; |
| | | 140 | | |
| | | 141 | | await _treeState.RefreshAsync(); |
| | | 142 | | _notifier.Success("Document updated"); |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | catch (Exception ex) |
| | | 146 | | { |
| | | 147 | | _logger.LogErrorSanitized(ex, "Error updating document {DocumentId}", EditingDocumentId!.Value); |
| | | 148 | | _notifier.Error($"Failed to update document: {ex.Message}"); |
| | | 149 | | } |
| | | 150 | | finally |
| | | 151 | | { |
| | | 152 | | IsSavingDocument = false; |
| | | 153 | | } |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | public async Task DeleteDocumentAsync(Guid documentId) |
| | | 157 | | { |
| | | 158 | | var doc = _documents.FirstOrDefault(d => d.Id == documentId); |
| | | 159 | | if (doc == null) |
| | | 160 | | return; |
| | | 161 | | |
| | | 162 | | try |
| | | 163 | | { |
| | | 164 | | var success = await _worldApi.DeleteDocumentAsync(_worldId, documentId); |
| | | 165 | | |
| | | 166 | | if (success) |
| | | 167 | | { |
| | | 168 | | var updated = new List<WorldDocumentDto>(_documents); |
| | | 169 | | updated.Remove(doc); |
| | | 170 | | Documents = updated; |
| | | 171 | | await _treeState.RefreshAsync(); |
| | | 172 | | _notifier.Success("Document deleted"); |
| | | 173 | | } |
| | | 174 | | else |
| | | 175 | | { |
| | | 176 | | _notifier.Error("Failed to delete document"); |
| | | 177 | | } |
| | | 178 | | } |
| | | 179 | | catch (Exception ex) |
| | | 180 | | { |
| | | 181 | | _logger.LogErrorSanitized(ex, "Error deleting document {DocumentId}", documentId); |
| | | 182 | | _notifier.Error($"Error deleting document: {ex.Message}"); |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | public async Task DownloadDocumentAsync(Guid documentId) |
| | | 187 | | { |
| | | 188 | | try |
| | | 189 | | { |
| | | 190 | | var download = await _worldApi.DownloadDocumentAsync(documentId); |
| | | 191 | | |
| | | 192 | | if (download == null) |
| | | 193 | | { |
| | | 194 | | _notifier.Error("Failed to get download URL"); |
| | | 195 | | return; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | _navigator.NavigateTo(download.DownloadUrl); |
| | | 199 | | _notifier.Success($"Opening {download.FileName}"); |
| | | 200 | | } |
| | | 201 | | catch (Exception ex) |
| | | 202 | | { |
| | | 203 | | _logger.LogErrorSanitized(ex, "Error downloading document {DocumentId}", documentId); |
| | | 204 | | _notifier.Error($"Error downloading document: {ex.Message}"); |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | /// <summary>Returns a MudBlazor icon string for the given MIME content type.</summary> |
| | | 209 | | public static string GetDocumentIcon(string contentType) => |
| | 8 | 210 | | contentType.ToLowerInvariant() switch |
| | 8 | 211 | | { |
| | 1 | 212 | | "application/pdf" => Icons.Material.Filled.PictureAsPdf, |
| | 1 | 213 | | "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => Icons.Material.Filled.Descripti |
| | 1 | 214 | | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => Icons.Material.Filled.TableChart, |
| | 1 | 215 | | "application/vnd.openxmlformats-officedocument.presentationml.presentation" => Icons.Material.Filled.Slidesh |
| | 1 | 216 | | "text/plain" => Icons.Material.Filled.TextSnippet, |
| | 1 | 217 | | "text/markdown" => Icons.Material.Filled.Article, |
| | 3 | 218 | | string ct when ct.StartsWith("image/") => Icons.Material.Filled.Image, |
| | 1 | 219 | | _ => Icons.Material.Filled.InsertDriveFile |
| | 8 | 220 | | }; |
| | | 221 | | |
| | | 222 | | /// <summary>Formats a byte count as a human-readable file size string.</summary> |
| | | 223 | | public static string FormatFileSize(long bytes) |
| | | 224 | | { |
| | 5 | 225 | | string[] sizes = { "B", "KB", "MB", "GB" }; |
| | 5 | 226 | | double len = bytes; |
| | 5 | 227 | | int order = 0; |
| | 12 | 228 | | while (len >= 1024 && order < sizes.Length - 1) |
| | | 229 | | { |
| | 7 | 230 | | order++; |
| | 7 | 231 | | len /= 1024; |
| | | 232 | | } |
| | | 233 | | |
| | 5 | 234 | | return $"{len:0.##} {sizes[order]}"; |
| | | 235 | | } |
| | | 236 | | } |