| | | 1 | | using Blazored.LocalStorage; |
| | | 2 | | using Chronicis.Client.Models; |
| | | 3 | | using Chronicis.Client.Services.Tree; |
| | | 4 | | using Chronicis.Shared.DTOs; |
| | | 5 | | using Chronicis.Shared.Enums; |
| | | 6 | | |
| | | 7 | | namespace Chronicis.Client.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Service for managing the navigation tree state. |
| | | 11 | | /// Builds a hierarchical tree with Worlds, Virtual Groups, Campaigns, Arcs, and Articles. |
| | | 12 | | /// |
| | | 13 | | /// This is a facade that delegates to internal components: |
| | | 14 | | /// - TreeDataBuilder: Builds the tree structure from API data |
| | | 15 | | /// - TreeUiState: Manages expansion, selection, search, and persistence |
| | | 16 | | /// - TreeMutations: Handles create, move, delete, and update operations |
| | | 17 | | /// </summary> |
| | | 18 | | public class TreeStateService : ITreeStateService |
| | | 19 | | { |
| | | 20 | | private readonly ILogger<TreeStateService> _logger; |
| | | 21 | | |
| | | 22 | | // Internal delegated components |
| | | 23 | | private readonly TreeDataBuilder _dataBuilder; |
| | | 24 | | private readonly TreeUiState _uiState; |
| | | 25 | | private readonly TreeMutations _mutations; |
| | | 26 | | |
| | | 27 | | // Shared state |
| | 0 | 28 | | private TreeNodeIndex _nodeIndex = new(); |
| | 0 | 29 | | private List<ArticleTreeDto> _cachedArticles = new(); |
| | | 30 | | private bool _isLoading; |
| | | 31 | | private bool _isInitialized; |
| | | 32 | | |
| | 0 | 33 | | public TreeStateService( |
| | 0 | 34 | | IArticleApiService articleApi, |
| | 0 | 35 | | IWorldApiService worldApi, |
| | 0 | 36 | | ICampaignApiService campaignApi, |
| | 0 | 37 | | IArcApiService arcApi, |
| | 0 | 38 | | IAppContextService appContext, |
| | 0 | 39 | | ILocalStorageService localStorage, |
| | 0 | 40 | | ILogger<TreeStateService> logger) |
| | | 41 | | { |
| | 0 | 42 | | _logger = logger; |
| | | 43 | | |
| | | 44 | | // Create internal components |
| | 0 | 45 | | _dataBuilder = new TreeDataBuilder(articleApi, worldApi, campaignApi, arcApi, logger); |
| | 0 | 46 | | _uiState = new TreeUiState(localStorage, logger); |
| | 0 | 47 | | _mutations = new TreeMutations(articleApi, appContext, logger); |
| | | 48 | | |
| | | 49 | | // Wire up refresh callback for mutations |
| | 0 | 50 | | _mutations.SetRefreshCallback(RefreshAsync); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | // ============================================ |
| | | 54 | | // State Properties |
| | | 55 | | // ============================================ |
| | | 56 | | |
| | 0 | 57 | | public IReadOnlyList<TreeNode> RootNodes => _nodeIndex.RootNodes; |
| | 0 | 58 | | public Guid? SelectedNodeId => _uiState.SelectedNodeId; |
| | 0 | 59 | | public string SearchQuery => _uiState.SearchQuery; |
| | 0 | 60 | | public bool IsSearchActive => _uiState.IsSearchActive; |
| | 0 | 61 | | public bool IsLoading => _isLoading; |
| | 0 | 62 | | public bool ShouldFocusTitle { get; set; } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Exposes the cached article list for other services to consume. |
| | | 66 | | /// Avoids duplicate API calls from Dashboard, etc. |
| | | 67 | | /// </summary> |
| | 0 | 68 | | public IReadOnlyList<ArticleTreeDto> CachedArticles => _cachedArticles; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Indicates whether the tree has been initialized and CachedArticles is populated. |
| | | 72 | | /// </summary> |
| | 0 | 73 | | public bool HasCachedData => _isInitialized && _cachedArticles.Any(); |
| | | 74 | | |
| | | 75 | | public event Action? OnStateChanged; |
| | | 76 | | |
| | 0 | 77 | | private void NotifyStateChanged() => OnStateChanged?.Invoke(); |
| | | 78 | | |
| | | 79 | | // ============================================ |
| | | 80 | | // Initialization |
| | | 81 | | // ============================================ |
| | | 82 | | |
| | | 83 | | public async Task InitializeAsync() |
| | | 84 | | { |
| | 0 | 85 | | if (_isInitialized) |
| | 0 | 86 | | return; |
| | | 87 | | |
| | 0 | 88 | | _isLoading = true; |
| | 0 | 89 | | NotifyStateChanged(); |
| | | 90 | | |
| | | 91 | | try |
| | | 92 | | { |
| | | 93 | | // Build the tree |
| | 0 | 94 | | var buildResult = await _dataBuilder.BuildTreeAsync(); |
| | 0 | 95 | | _nodeIndex = buildResult.NodeIndex; |
| | 0 | 96 | | _cachedArticles = buildResult.CachedArticles; |
| | | 97 | | |
| | | 98 | | // Update internal components with new node index |
| | 0 | 99 | | _uiState.SetNodeIndex(_nodeIndex); |
| | 0 | 100 | | _mutations.SetNodeIndex(_nodeIndex); |
| | | 101 | | |
| | | 102 | | // Restore persisted state |
| | 0 | 103 | | await _uiState.RestoreExpandedStateFromStorageAsync(); |
| | | 104 | | |
| | 0 | 105 | | _isInitialized = true; |
| | | 106 | | |
| | | 107 | | // Handle pending selection (if ExpandPathToAndSelect was called before init) |
| | 0 | 108 | | var pendingId = _uiState.ConsumePendingSelection(); |
| | 0 | 109 | | if (pendingId.HasValue) |
| | | 110 | | { |
| | 0 | 111 | | _uiState.ExpandPathToAndSelect(pendingId.Value, _isInitialized); |
| | | 112 | | } |
| | 0 | 113 | | } |
| | 0 | 114 | | catch (Exception ex) |
| | | 115 | | { |
| | 0 | 116 | | _logger.LogError(ex, "Failed to initialize tree"); |
| | 0 | 117 | | _nodeIndex = new TreeNodeIndex(); |
| | 0 | 118 | | _cachedArticles = new List<ArticleTreeDto>(); |
| | 0 | 119 | | } |
| | | 120 | | finally |
| | | 121 | | { |
| | 0 | 122 | | _isLoading = false; |
| | 0 | 123 | | NotifyStateChanged(); |
| | | 124 | | } |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | public async Task RefreshAsync() |
| | | 128 | | { |
| | | 129 | | // Save current state before refresh |
| | 0 | 130 | | var previouslyExpanded = new HashSet<Guid>(_uiState.ExpandedNodeIds); |
| | 0 | 131 | | var previousSelection = _uiState.SelectedNodeId; |
| | | 132 | | |
| | 0 | 133 | | _isLoading = true; |
| | 0 | 134 | | NotifyStateChanged(); |
| | | 135 | | |
| | | 136 | | try |
| | | 137 | | { |
| | | 138 | | // Rebuild the tree |
| | 0 | 139 | | var buildResult = await _dataBuilder.BuildTreeAsync(); |
| | 0 | 140 | | _nodeIndex = buildResult.NodeIndex; |
| | 0 | 141 | | _cachedArticles = buildResult.CachedArticles; |
| | | 142 | | |
| | | 143 | | // Update internal components with new node index |
| | 0 | 144 | | _uiState.SetNodeIndex(_nodeIndex); |
| | 0 | 145 | | _mutations.SetNodeIndex(_nodeIndex); |
| | | 146 | | |
| | | 147 | | // Restore expanded state |
| | 0 | 148 | | _uiState.RestoreExpandedNodesPreserving(previouslyExpanded); |
| | | 149 | | |
| | | 150 | | // Restore selection AND ensure path is expanded |
| | 0 | 151 | | if (previousSelection.HasValue && _nodeIndex.ContainsNode(previousSelection.Value)) |
| | | 152 | | { |
| | 0 | 153 | | _uiState.ExpandPathToAndSelect(previousSelection.Value, _isInitialized); |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | // Re-apply search filter if active |
| | 0 | 157 | | if (_uiState.IsSearchActive) |
| | | 158 | | { |
| | 0 | 159 | | _uiState.ApplySearchFilter(); |
| | | 160 | | } |
| | 0 | 161 | | } |
| | 0 | 162 | | catch (Exception ex) |
| | | 163 | | { |
| | 0 | 164 | | _logger.LogError(ex, "Failed to refresh tree"); |
| | 0 | 165 | | } |
| | | 166 | | finally |
| | | 167 | | { |
| | 0 | 168 | | _isLoading = false; |
| | 0 | 169 | | NotifyStateChanged(); |
| | | 170 | | } |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | // ============================================ |
| | | 174 | | // Node Operations (delegated to TreeUiState) |
| | | 175 | | // ============================================ |
| | | 176 | | |
| | | 177 | | public void ExpandNode(Guid nodeId) |
| | | 178 | | { |
| | 0 | 179 | | if (_uiState.ExpandNode(nodeId)) |
| | | 180 | | { |
| | 0 | 181 | | NotifyStateChanged(); |
| | | 182 | | } |
| | 0 | 183 | | } |
| | | 184 | | |
| | | 185 | | public void CollapseNode(Guid nodeId) |
| | | 186 | | { |
| | 0 | 187 | | if (_uiState.CollapseNode(nodeId)) |
| | | 188 | | { |
| | 0 | 189 | | NotifyStateChanged(); |
| | | 190 | | } |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | public void ToggleNode(Guid nodeId) |
| | | 194 | | { |
| | 0 | 195 | | if (_uiState.ToggleNode(nodeId)) |
| | | 196 | | { |
| | 0 | 197 | | NotifyStateChanged(); |
| | | 198 | | } |
| | 0 | 199 | | } |
| | | 200 | | |
| | | 201 | | public void SelectNode(Guid nodeId) |
| | | 202 | | { |
| | 0 | 203 | | _uiState.SelectNode(nodeId); |
| | 0 | 204 | | NotifyStateChanged(); |
| | 0 | 205 | | } |
| | | 206 | | |
| | | 207 | | public void ExpandPathToAndSelect(Guid nodeId) |
| | | 208 | | { |
| | 0 | 209 | | _uiState.ExpandPathToAndSelect(nodeId, _isInitialized); |
| | 0 | 210 | | NotifyStateChanged(); |
| | 0 | 211 | | } |
| | | 212 | | |
| | | 213 | | // ============================================ |
| | | 214 | | // CRUD Operations (delegated to TreeMutations) |
| | | 215 | | // ============================================ |
| | | 216 | | |
| | | 217 | | public async Task<Guid?> CreateRootArticleAsync() |
| | | 218 | | { |
| | 0 | 219 | | var newId = await _mutations.CreateRootArticleAsync(); |
| | | 220 | | |
| | 0 | 221 | | if (newId.HasValue) |
| | | 222 | | { |
| | | 223 | | // Select the new node (refresh already happened via callback) |
| | 0 | 224 | | _uiState.SelectNode(newId.Value); |
| | 0 | 225 | | ShouldFocusTitle = true; |
| | 0 | 226 | | NotifyStateChanged(); |
| | | 227 | | } |
| | | 228 | | |
| | 0 | 229 | | return newId; |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | public async Task<Guid?> CreateChildArticleAsync(Guid parentId) |
| | | 233 | | { |
| | 0 | 234 | | var newId = await _mutations.CreateChildArticleAsync(parentId); |
| | | 235 | | |
| | 0 | 236 | | if (newId.HasValue) |
| | | 237 | | { |
| | | 238 | | // Expand parent and select new node (refresh already happened via callback) |
| | 0 | 239 | | _uiState.ExpandNode(parentId); |
| | 0 | 240 | | _uiState.SelectNode(newId.Value); |
| | 0 | 241 | | ShouldFocusTitle = true; |
| | 0 | 242 | | NotifyStateChanged(); |
| | | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | return newId; |
| | 0 | 246 | | } |
| | | 247 | | |
| | | 248 | | public async Task<bool> DeleteArticleAsync(Guid articleId) |
| | | 249 | | { |
| | 0 | 250 | | var wasSelected = _uiState.SelectedNodeId == articleId; |
| | 0 | 251 | | var success = await _mutations.DeleteArticleAsync(articleId); |
| | | 252 | | |
| | 0 | 253 | | if (success && wasSelected) |
| | | 254 | | { |
| | 0 | 255 | | _uiState.ClearSelection(); |
| | | 256 | | } |
| | | 257 | | |
| | 0 | 258 | | NotifyStateChanged(); |
| | 0 | 259 | | return success; |
| | 0 | 260 | | } |
| | | 261 | | |
| | | 262 | | public async Task<bool> MoveArticleAsync(Guid articleId, Guid? newParentId) |
| | | 263 | | { |
| | 0 | 264 | | var success = await _mutations.MoveArticleAsync(articleId, newParentId); |
| | | 265 | | // Refresh and notification handled by callback |
| | 0 | 266 | | return success; |
| | 0 | 267 | | } |
| | | 268 | | |
| | | 269 | | public void UpdateNodeDisplay(Guid nodeId, string title, string? iconEmoji) |
| | | 270 | | { |
| | 0 | 271 | | if (_mutations.UpdateNodeDisplay(nodeId, title, iconEmoji)) |
| | | 272 | | { |
| | 0 | 273 | | NotifyStateChanged(); |
| | | 274 | | } |
| | 0 | 275 | | } |
| | | 276 | | |
| | | 277 | | public void UpdateNodeVisibility(Guid nodeId, ArticleVisibility visibility) |
| | | 278 | | { |
| | 0 | 279 | | if (_mutations.UpdateNodeVisibility(nodeId, visibility)) |
| | | 280 | | { |
| | 0 | 281 | | NotifyStateChanged(); |
| | | 282 | | } |
| | 0 | 283 | | } |
| | | 284 | | |
| | | 285 | | // ============================================ |
| | | 286 | | // Search/Filter (delegated to TreeUiState) |
| | | 287 | | // ============================================ |
| | | 288 | | |
| | | 289 | | public void SetSearchQuery(string query) |
| | | 290 | | { |
| | 0 | 291 | | _uiState.SetSearchQuery(query); |
| | 0 | 292 | | NotifyStateChanged(); |
| | 0 | 293 | | } |
| | | 294 | | |
| | | 295 | | public void ClearSearch() |
| | | 296 | | { |
| | 0 | 297 | | _uiState.ClearSearch(); |
| | 0 | 298 | | NotifyStateChanged(); |
| | 0 | 299 | | } |
| | | 300 | | |
| | | 301 | | // ============================================ |
| | | 302 | | // Persistence (delegated to TreeUiState) |
| | | 303 | | // ============================================ |
| | | 304 | | |
| | 0 | 305 | | public IReadOnlySet<Guid> GetExpandedNodeIds() => _uiState.GetExpandedNodeIds(); |
| | | 306 | | |
| | | 307 | | public void RestoreExpandedNodes(IEnumerable<Guid> nodeIds) |
| | | 308 | | { |
| | 0 | 309 | | _uiState.RestoreExpandedNodes(nodeIds); |
| | 0 | 310 | | NotifyStateChanged(); |
| | 0 | 311 | | } |
| | | 312 | | |
| | | 313 | | // ============================================ |
| | | 314 | | // Node Lookup |
| | | 315 | | // ============================================ |
| | | 316 | | |
| | | 317 | | public bool TryGetNode(Guid nodeId, out TreeNode? node) |
| | | 318 | | { |
| | 0 | 319 | | return _nodeIndex.TryGetNode(nodeId, out node); |
| | | 320 | | } |
| | | 321 | | } |