diff --git a/UI/ViewModels/HierarchyTreeViewModel.cs b/UI/ViewModels/HierarchyTreeViewModel.cs index 80f5d40..1d9154c 100644 --- a/UI/ViewModels/HierarchyTreeViewModel.cs +++ b/UI/ViewModels/HierarchyTreeViewModel.cs @@ -89,7 +89,7 @@ public partial class HierarchyTreeViewModel : ViewModelBase, IDisposable this.ChildNodeMap.Add(childNode.Tag.Id, []); this.ChildNodeMap[childNode.Tag.Id].Add(tag.Id); - this.AddTagNodeToViewModelMap(childNode, beingUpdated); + this.AddTagNodeToViewModelMap(childNode); tag.Children.Add(childNode); this.AddAllChildrenAsync(childNode); @@ -124,7 +124,7 @@ public partial class HierarchyTreeViewModel : ViewModelBase, IDisposable set.Add(parentId); } - private void AddTagNodeToViewModelMap(TagItemViewModel tagNode, bool beingUpdated = false) + private void AddTagNodeToViewModelMap(TagItemViewModel tagNode) { if (!this.ViewModelMap.TryGetValue(tagNode.Id, out var tagNodeSet)) { @@ -185,7 +185,8 @@ public partial class HierarchyTreeViewModel : ViewModelBase, IDisposable partial void OnSelectedTagChanged(TagItemViewModel? value) { - this._mainWindow.SelectedTag = value; + if (value is null || this._mainWindow.SelectedTagId == value.Id) return; + this._mainWindow.SelectedTagId = value.Id; } private async Task ProcessTagAdditions(IReadOnlyList newTags) diff --git a/UI/ViewModels/MainWindowViewModel.cs b/UI/ViewModels/MainWindowViewModel.cs index b4b518f..828b38a 100644 --- a/UI/ViewModels/MainWindowViewModel.cs +++ b/UI/ViewModels/MainWindowViewModel.cs @@ -29,6 +29,8 @@ public partial class MainWindowViewModel : ViewModelBase // Since multiple view models will be using this tag, best to store it here as the authoritative source. private TagItemViewModel? _selectedTag; + [ObservableProperty] private int? _selectedTagId; + [ObservableProperty] private string _statusBlockText = Resources.StatusBlockReady; [ObservableProperty] private TagDatabaseService _tagDatabaseService; @@ -250,6 +252,16 @@ public partial class MainWindowViewModel : ViewModelBase } } + partial void OnSelectedTagIdChanged(int? id) + { + if (id is null) return; + var tag = this.TagDatabaseService.GetTagById(id.Value); + this.SelectedTag = + tag is null + ? null + : new TagItemViewModel(tag, this.TagDatabaseService.GetParentNamesByIds); + } + [RelayCommand] private async Task SaveSelectedTagAsync() { diff --git a/UI/ViewModels/RecentsViewModel.cs b/UI/ViewModels/RecentsViewModel.cs index 5965b47..546aa54 100644 --- a/UI/ViewModels/RecentsViewModel.cs +++ b/UI/ViewModels/RecentsViewModel.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; using TagHierarchyManager.Models; @@ -8,7 +7,6 @@ namespace TagHierarchyManager.UI.ViewModels; public partial class RecentsViewModel : ViewModelBase { - private readonly Func, List> _getParentNamesById; private readonly MainWindowViewModel _mainWindow; [ObservableProperty] private ObservableCollection _recentAdded = []; [ObservableProperty] private ObservableCollection _recentEdited = []; @@ -16,7 +14,6 @@ public partial class RecentsViewModel : ViewModelBase public RecentsViewModel(MainWindowViewModel mainWindow) { this._mainWindow = mainWindow; - this._getParentNamesById = mainWindow.TagDatabaseService.GetParentNamesByIds; this.RecentAdded = new ObservableCollection(); foreach (var tag in this._mainWindow.TagDatabaseService.GetRecentTags(true)) if (tag.CreatedAt is not null) @@ -32,8 +29,7 @@ public partial class RecentsViewModel : ViewModelBase { var tag = this._mainWindow.TagDatabaseService.GetTagById(tagRow.Id); if (tag is null) return; - var vm = new TagItemViewModel(tag, this._getParentNamesById); - this._mainWindow.SelectedTag = vm; + this._mainWindow.SelectedTagId = tagRow.Id; this.RequestClose?.Invoke(); } diff --git a/UI/ViewModels/SearchViewModel.cs b/UI/ViewModels/SearchViewModel.cs index 46dc2e8..b516ffd 100644 --- a/UI/ViewModels/SearchViewModel.cs +++ b/UI/ViewModels/SearchViewModel.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using CommunityToolkit.Mvvm.ComponentModel; @@ -10,7 +9,6 @@ namespace TagHierarchyManager.UI.ViewModels; public partial class SearchViewModel : ViewModelBase, IDisposable { - private readonly Func, List> _getParentNamesById; private readonly MainWindowViewModel _mainWindow; [ObservableProperty] private ObservableCollection _searchResults = []; @@ -20,7 +18,6 @@ public partial class SearchViewModel : ViewModelBase, IDisposable public SearchViewModel(MainWindowViewModel mainWindow) { this._mainWindow = mainWindow; - this._getParentNamesById = mainWindow.TagDatabaseService.GetParentNamesByIds; mainWindow.TagDatabaseService.TagsWritten += this.TagDatabase_OnTagsWritten; } @@ -44,7 +41,7 @@ public partial class SearchViewModel : ViewModelBase, IDisposable } results.Select(tag => - new TagItemViewModel(tag, this._getParentNamesById)) + new TagItemViewModel(tag)) .OrderBy(tag => tag.Name) .ToList() .ForEach(this.SearchResults.Add); @@ -56,8 +53,8 @@ public partial class SearchViewModel : ViewModelBase, IDisposable partial void OnSelectedSearchResultChanged(TagItemViewModel? value) { - if (value is null) return; - this._mainWindow.SelectedTag = value; + if (value is null || this._mainWindow.SelectedTagId == value.Id) return; + this._mainWindow.SelectedTagId = value.Id; } private void TagDatabase_OnTagsWritten(object? sender, TagDatabaseService.TagWriteResult result) diff --git a/UI/ViewModels/TagItemViewModel.cs b/UI/ViewModels/TagItemViewModel.cs index 02493c0..4d0d461 100644 --- a/UI/ViewModels/TagItemViewModel.cs +++ b/UI/ViewModels/TagItemViewModel.cs @@ -50,7 +50,7 @@ public partial class TagItemViewModel(Tag tag, Func, List>? ge public ObservableCollection Children { get; set; } = []; - internal Tag Tag { get; set; } = tag; + internal Tag Tag { get; set; } = tag; private bool IsTopLevel => this.Tag.IsTopLevel;