| | | 1 | | using Chronicis.Client.Abstractions; |
| | | 2 | | using Chronicis.Client.Components.Dialogs; |
| | | 3 | | using Chronicis.Client.Services; |
| | | 4 | | using Chronicis.Shared.DTOs; |
| | | 5 | | using Chronicis.Shared.Enums; |
| | | 6 | | using Chronicis.Shared.Extensions; |
| | | 7 | | using MudBlazor; |
| | | 8 | | |
| | | 9 | | namespace Chronicis.Client.ViewModels; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// ViewModel for the CampaignDetail page. |
| | | 13 | | /// Manages loading, editing, saving, and arc creation for a single campaign. |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class CampaignDetailViewModel : ViewModelBase |
| | | 16 | | { |
| | | 17 | | private readonly ICampaignApiService _campaignApi; |
| | | 18 | | private readonly IArcApiService _arcApi; |
| | | 19 | | private readonly IWorldApiService _worldApi; |
| | | 20 | | private readonly IAuthService _authService; |
| | | 21 | | private readonly ITreeStateService _treeState; |
| | | 22 | | private readonly IBreadcrumbService _breadcrumbService; |
| | | 23 | | private readonly IAppNavigator _navigator; |
| | | 24 | | private readonly IUserNotifier _notifier; |
| | | 25 | | private readonly IPageTitleService _titleService; |
| | | 26 | | private readonly IDialogService _dialogService; |
| | | 27 | | private readonly ILogger<CampaignDetailViewModel> _logger; |
| | | 28 | | |
| | 20 | 29 | | private bool _isLoading = true; |
| | | 30 | | private bool _isSaving; |
| | | 31 | | private bool _isTogglingActive; |
| | | 32 | | private bool _hasUnsavedChanges; |
| | | 33 | | private bool _summaryExpanded; |
| | | 34 | | private CampaignDetailDto? _campaign; |
| | 20 | 35 | | private List<ArcDto> _arcs = new(); |
| | 20 | 36 | | private string _editName = string.Empty; |
| | 20 | 37 | | private string _editDescription = string.Empty; |
| | 20 | 38 | | private string _editPrivateNotes = string.Empty; |
| | 20 | 39 | | private List<BreadcrumbItem> _breadcrumbs = new(); |
| | | 40 | | private bool _isCurrentUserGm; |
| | | 41 | | private bool _isCurrentUserWorldOwner; |
| | | 42 | | |
| | 20 | 43 | | public CampaignDetailViewModel( |
| | 20 | 44 | | ICampaignApiService campaignApi, |
| | 20 | 45 | | IArcApiService arcApi, |
| | 20 | 46 | | IWorldApiService worldApi, |
| | 20 | 47 | | IAuthService authService, |
| | 20 | 48 | | ITreeStateService treeState, |
| | 20 | 49 | | IBreadcrumbService breadcrumbService, |
| | 20 | 50 | | IAppNavigator navigator, |
| | 20 | 51 | | IUserNotifier notifier, |
| | 20 | 52 | | IPageTitleService titleService, |
| | 20 | 53 | | IDialogService dialogService, |
| | 20 | 54 | | ILogger<CampaignDetailViewModel> logger) |
| | | 55 | | { |
| | 20 | 56 | | _campaignApi = campaignApi; |
| | 20 | 57 | | _arcApi = arcApi; |
| | 20 | 58 | | _worldApi = worldApi; |
| | 20 | 59 | | _authService = authService; |
| | 20 | 60 | | _treeState = treeState; |
| | 20 | 61 | | _breadcrumbService = breadcrumbService; |
| | 20 | 62 | | _navigator = navigator; |
| | 20 | 63 | | _notifier = notifier; |
| | 20 | 64 | | _titleService = titleService; |
| | 20 | 65 | | _dialogService = dialogService; |
| | 20 | 66 | | _logger = logger; |
| | 20 | 67 | | } |
| | | 68 | | |
| | 41 | 69 | | public bool IsLoading { get => _isLoading; private set => SetField(ref _isLoading, value); } |
| | 12 | 70 | | public bool IsSaving { get => _isSaving; private set => SetField(ref _isSaving, value); } |
| | 15 | 71 | | public bool IsTogglingActive { get => _isTogglingActive; private set => SetField(ref _isTogglingActive, value); } |
| | 56 | 72 | | public bool HasUnsavedChanges { get => _hasUnsavedChanges; private set => SetField(ref _hasUnsavedChanges, value); } |
| | 6 | 73 | | public bool SummaryExpanded { get => _summaryExpanded; set => SetField(ref _summaryExpanded, value); } |
| | 58 | 74 | | public CampaignDetailDto? Campaign { get => _campaign; private set => SetField(ref _campaign, value); } |
| | 21 | 75 | | public List<ArcDto> Arcs { get => _arcs; private set => SetField(ref _arcs, value); } |
| | 19 | 76 | | public List<BreadcrumbItem> Breadcrumbs { get => _breadcrumbs; private set => SetField(ref _breadcrumbs, value); } |
| | 77 | 77 | | public bool IsCurrentUserGM { get => _isCurrentUserGm; private set => SetField(ref _isCurrentUserGm, value); } |
| | 65 | 78 | | public bool IsCurrentUserWorldOwner { get => _isCurrentUserWorldOwner; private set => SetField(ref _isCurrentUserWor |
| | 29 | 79 | | public bool CanManageCampaignDetails => IsCurrentUserGM || IsCurrentUserWorldOwner; |
| | 6 | 80 | | public bool CanViewPrivateNotes => CanManageCampaignDetails; |
| | | 81 | | |
| | | 82 | | public string EditName |
| | | 83 | | { |
| | 6 | 84 | | get => _editName; |
| | | 85 | | set |
| | | 86 | | { |
| | 17 | 87 | | if (SetField(ref _editName, value)) |
| | 17 | 88 | | HasUnsavedChanges = true; |
| | 17 | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | public string EditDescription |
| | | 93 | | { |
| | 11 | 94 | | get => _editDescription; |
| | | 95 | | set |
| | | 96 | | { |
| | 16 | 97 | | if (SetField(ref _editDescription, value)) |
| | 14 | 98 | | HasUnsavedChanges = true; |
| | 16 | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | public string EditPrivateNotes |
| | | 103 | | { |
| | 3 | 104 | | get => _editPrivateNotes; |
| | | 105 | | set |
| | | 106 | | { |
| | 17 | 107 | | if (SetField(ref _editPrivateNotes, value) && CanManageCampaignDetails) |
| | 1 | 108 | | HasUnsavedChanges = true; |
| | 17 | 109 | | } |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary>Loads the campaign and all related data for the given <paramref name="campaignId"/>.</summary> |
| | | 113 | | public async Task LoadAsync(Guid campaignId) |
| | | 114 | | { |
| | | 115 | | IsLoading = true; |
| | | 116 | | |
| | | 117 | | try |
| | | 118 | | { |
| | | 119 | | IsCurrentUserGM = false; |
| | | 120 | | IsCurrentUserWorldOwner = false; |
| | | 121 | | |
| | | 122 | | var campaign = await _campaignApi.GetCampaignAsync(campaignId); |
| | | 123 | | if (campaign == null) |
| | | 124 | | { |
| | | 125 | | _navigator.NavigateTo("/dashboard", replace: true); |
| | | 126 | | return; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | Campaign = campaign; |
| | | 130 | | EditName = campaign.Name; |
| | | 131 | | EditDescription = campaign.Description ?? string.Empty; |
| | | 132 | | EditPrivateNotes = campaign.PrivateNotes ?? string.Empty; |
| | | 133 | | HasUnsavedChanges = false; |
| | | 134 | | |
| | | 135 | | Arcs = await _arcApi.GetArcsByCampaignAsync(campaignId); |
| | | 136 | | |
| | | 137 | | var world = await _worldApi.GetWorldAsync(campaign.WorldId); |
| | | 138 | | Breadcrumbs = world != null |
| | | 139 | | ? _breadcrumbService.ForCampaign(campaign, world) |
| | | 140 | | : new List<BreadcrumbItem> |
| | | 141 | | { |
| | | 142 | | new("Dashboard", href: "/dashboard"), |
| | | 143 | | new(campaign.Name, href: null, disabled: true) |
| | | 144 | | }; |
| | | 145 | | |
| | | 146 | | await ResolveCurrentUserRoleAsync(world); |
| | | 147 | | |
| | | 148 | | await _titleService.SetTitleAsync(campaign.Name); |
| | | 149 | | _treeState.ExpandPathToAndSelect(campaignId); |
| | | 150 | | } |
| | | 151 | | catch (Exception ex) |
| | | 152 | | { |
| | | 153 | | _logger.LogErrorSanitized(ex, "Error loading campaign {CampaignId}", campaignId); |
| | | 154 | | _notifier.Error($"Failed to load campaign: {ex.Message}"); |
| | | 155 | | } |
| | | 156 | | finally |
| | | 157 | | { |
| | | 158 | | IsLoading = false; |
| | | 159 | | } |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <summary>Toggles the active state of the campaign.</summary> |
| | | 163 | | public async Task OnActiveToggleAsync(bool isActive) |
| | | 164 | | { |
| | | 165 | | if (_campaign == null || !CanManageCampaignDetails || IsTogglingActive) |
| | | 166 | | return; |
| | | 167 | | |
| | | 168 | | IsTogglingActive = true; |
| | | 169 | | |
| | | 170 | | try |
| | | 171 | | { |
| | | 172 | | if (isActive) |
| | | 173 | | { |
| | | 174 | | var success = await _campaignApi.ActivateCampaignAsync(_campaign.Id); |
| | | 175 | | if (success) |
| | | 176 | | { |
| | | 177 | | _campaign.IsActive = true; |
| | | 178 | | RaisePropertyChanged(nameof(Campaign)); |
| | | 179 | | _notifier.Success("Campaign set as active"); |
| | | 180 | | } |
| | | 181 | | else |
| | | 182 | | { |
| | | 183 | | _notifier.Error("Failed to activate campaign"); |
| | | 184 | | } |
| | | 185 | | } |
| | | 186 | | else |
| | | 187 | | { |
| | | 188 | | _notifier.Info("To deactivate, set another campaign as active"); |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | catch (Exception ex) |
| | | 192 | | { |
| | | 193 | | _logger.LogErrorSanitized(ex, "Error toggling campaign active state"); |
| | | 194 | | _notifier.Error($"Error: {ex.Message}"); |
| | | 195 | | } |
| | | 196 | | finally |
| | | 197 | | { |
| | | 198 | | IsTogglingActive = false; |
| | | 199 | | } |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | /// <summary>Persists name and description changes to the API.</summary> |
| | | 203 | | public async Task SaveAsync() |
| | | 204 | | { |
| | | 205 | | if (_campaign == null || !CanManageCampaignDetails || IsSaving) |
| | | 206 | | return; |
| | | 207 | | |
| | | 208 | | IsSaving = true; |
| | | 209 | | |
| | | 210 | | try |
| | | 211 | | { |
| | | 212 | | var updateDto = new CampaignUpdateDto |
| | | 213 | | { |
| | | 214 | | Name = EditName.Trim(), |
| | | 215 | | Description = string.IsNullOrWhiteSpace(EditDescription) ? null : EditDescription.Trim(), |
| | | 216 | | PrivateNotes = string.IsNullOrWhiteSpace(EditPrivateNotes) ? null : EditPrivateNotes |
| | | 217 | | }; |
| | | 218 | | |
| | | 219 | | var updated = await _campaignApi.UpdateCampaignAsync(_campaign.Id, updateDto); |
| | | 220 | | if (updated != null) |
| | | 221 | | { |
| | | 222 | | _campaign.Name = updated.Name; |
| | | 223 | | _campaign.Description = updated.Description; |
| | | 224 | | _campaign.PrivateNotes = string.IsNullOrWhiteSpace(EditPrivateNotes) ? null : EditPrivateNotes; |
| | | 225 | | HasUnsavedChanges = false; |
| | | 226 | | |
| | | 227 | | await _treeState.RefreshAsync(); |
| | | 228 | | await _titleService.SetTitleAsync(EditName); |
| | | 229 | | _notifier.Success("Campaign saved"); |
| | | 230 | | } |
| | | 231 | | } |
| | | 232 | | catch (Exception ex) |
| | | 233 | | { |
| | | 234 | | _logger.LogErrorSanitized(ex, "Error saving campaign"); |
| | | 235 | | _notifier.Error($"Failed to save: {ex.Message}"); |
| | | 236 | | } |
| | | 237 | | finally |
| | | 238 | | { |
| | | 239 | | IsSaving = false; |
| | | 240 | | } |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | /// <summary>Opens the create-arc dialog and navigates to the new arc on success.</summary> |
| | | 244 | | public async Task CreateArcAsync() |
| | | 245 | | { |
| | | 246 | | if (_campaign == null || !IsCurrentUserGM) |
| | | 247 | | return; |
| | | 248 | | |
| | | 249 | | var parameters = new DialogParameters { { "CampaignId", _campaign.Id } }; |
| | | 250 | | var dialog = await _dialogService.ShowAsync<CreateArcDialog>("New Arc", parameters); |
| | | 251 | | var result = await dialog.Result; |
| | | 252 | | |
| | | 253 | | if (result != null && !result.Canceled && result.Data is ArcDto arc) |
| | | 254 | | { |
| | | 255 | | await _treeState.RefreshAsync(); |
| | | 256 | | await LoadAsync(_campaign.Id); |
| | | 257 | | _navigator.NavigateTo($"/arc/{arc.Id}"); |
| | | 258 | | _notifier.Success("Arc created"); |
| | | 259 | | } |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | /// <summary>Navigates to the arc detail page.</summary> |
| | 1 | 263 | | public void NavigateToArc(Guid arcId) => _navigator.NavigateTo($"/arc/{arcId}"); |
| | | 264 | | |
| | | 265 | | private async Task ResolveCurrentUserRoleAsync(WorldDetailDto? world) |
| | | 266 | | { |
| | | 267 | | IsCurrentUserGM = false; |
| | | 268 | | IsCurrentUserWorldOwner = false; |
| | | 269 | | |
| | | 270 | | if (world?.Members == null) |
| | | 271 | | { |
| | | 272 | | return; |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | var user = await _authService.GetCurrentUserAsync(); |
| | | 276 | | if (user == null || string.IsNullOrWhiteSpace(user.Email)) |
| | | 277 | | { |
| | | 278 | | return; |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | var member = world.Members.FirstOrDefault(m => |
| | | 282 | | m.Email.Equals(user.Email, StringComparison.OrdinalIgnoreCase)); |
| | | 283 | | |
| | | 284 | | if (member == null) |
| | | 285 | | { |
| | | 286 | | return; |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | IsCurrentUserGM = member.Role == WorldRole.GM; |
| | | 290 | | IsCurrentUserWorldOwner = member.UserId == world.OwnerId; |
| | | 291 | | } |
| | | 292 | | } |