From a2f729a67869a4443846af6ddc5cbb3c19f2ca37 Mon Sep 17 00:00:00 2001 From: ImoutoChan Date: Mon, 30 Oct 2023 00:24:55 +0400 Subject: [PATCH] Add counter tags initial support: you have to create tag with value "Counter:0", then you can increase it in left tags window --- CHANGELOG.md | 3 + .../Services/Tags/FileTagService.cs | 6 +- .../Services/Tags/IFileTagService.cs | 7 +- .../ViewModel/BindedTagVM.cs | 95 ++++++++++--------- .../ViewModel/MainWindowVM.cs | 2 +- .../ViewModel/TagSearchVM.cs | 8 +- .../ImoutoRebirth.Navigator/styles.xaml | 62 +++++++++--- 7 files changed, 116 insertions(+), 67 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ec45129..18da9c58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ ### Navigator * Add tag "my wallpapers" to the file when setting wallpaper from context menu +* Add counter tags initial support: you have to create tag with value "Counter:0", +then you can increase it in left tags window +* Fixed bug with deletion of user tags with custom value # 4.19.3 diff --git a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/FileTagService.cs b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/FileTagService.cs index b1013456..708d84ff 100644 --- a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/FileTagService.cs +++ b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/FileTagService.cs @@ -68,12 +68,14 @@ await _filesClient.BindTagsAsync( SameTagHandleStrategy.ReplaceExistingValue)); } - public async Task BindTags(IReadOnlyCollection fileTags) + public async Task BindTags( + IReadOnlyCollection fileTags, + SameTagHandleStrategy strategy = SameTagHandleStrategy.AddNewFileTag) { var requests = _mapper.Map>(fileTags); await _filesClient.BindTagsAsync( - new BindTagsCommand(requests, SameTagHandleStrategy.AddNewFileTag)); + new BindTagsCommand(requests, strategy)); } public async Task UnbindTags(params UnbindTagRequest[] tagsToUnbind) diff --git a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/IFileTagService.cs b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/IFileTagService.cs index 960ae359..1e1e1569 100644 --- a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/IFileTagService.cs +++ b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/IFileTagService.cs @@ -1,4 +1,5 @@ -using ImoutoRebirth.Navigator.Services.Tags.Model; +using ImoutoRebirth.LilinService.WebApi.Client; +using ImoutoRebirth.Navigator.Services.Tags.Model; namespace ImoutoRebirth.Navigator.Services.Tags; @@ -10,7 +11,9 @@ interface IFileTagService Task SetWasWallpaper(Guid selectedItemDbId); - Task BindTags(IReadOnlyCollection fileTags); + Task BindTags( + IReadOnlyCollection fileTags, + SameTagHandleStrategy strategy = SameTagHandleStrategy.AddNewFileTag); Task UnbindTags(params UnbindTagRequest[] tagsToUnbind); diff --git a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/BindedTagVM.cs b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/BindedTagVM.cs index 0e0f8102..40614466 100644 --- a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/BindedTagVM.cs +++ b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/BindedTagVM.cs @@ -1,7 +1,7 @@ using System.Diagnostics; using System.Windows.Input; using System.Windows.Media; -using ImoutoRebirth.Navigator.Commands; +using ImoutoRebirth.LilinService.WebApi.Client; using ImoutoRebirth.Navigator.Services; using ImoutoRebirth.Navigator.Services.Tags; using ImoutoRebirth.Navigator.Services.Tags.Model; @@ -12,9 +12,7 @@ namespace ImoutoRebirth.Navigator.ViewModel; class BindedTagVM : VMBase { - #region Static members - - private static readonly List TypePriorities = new List + private static readonly List TypePriorities = new() { "Artist", "Copyright", @@ -27,30 +25,21 @@ class BindedTagVM : VMBase "General" }; - #endregion Static members - - #region Fields - - private ICommand _unbindCommand; - private readonly Guid? _targetId; + private ICommand? _incrementCounterCommand; + private ICommand? _unbindCommand; + private readonly Guid? _fileId; + private readonly Action? _updateAction; private readonly IFileTagService _fileTagService; private SearchType _searchType; - #endregion Fields - - #region Constructors - - public BindedTagVM(FileTag bindedTagModel, Guid? targetId = null) + public BindedTagVM(FileTag model, Guid? fileId, Action? updateAction) { - Model = bindedTagModel; + Model = model; _fileTagService = ServiceLocator.GetService(); - _targetId = targetId; + _fileId = fileId; + _updateAction = updateAction; } - #endregion Constructors - - #region Properties - public SearchType SearchType { get => _searchType; @@ -67,7 +56,7 @@ public SearchType SearchType public string Synonyms => string.Join(", ", Tag.SynonymsCollection); - public string Value + public string? Value { get => Model.Value; set @@ -85,15 +74,20 @@ public string Title { var tag = Model.Tag.Title; - if (Model.Tag.HasValue) - { + if (Model.Tag.HasValue && !IsCounterTag) return tag + " : " + Model.Value; - } + return tag; } } + public string CounterCountTitle => CounterCount == null ? string.Empty : $"[{CounterCount}]"; + public bool IsEditable => Model.IsEditable; + + public bool IsCounterTag => Model.Value != null && Model.Value.StartsWith("Counter:"); + + public int? CounterCount => IsCounterTag ? int.Parse(Model.Value!.Split(':')[1]) : null; public int TypePriority { @@ -104,23 +98,21 @@ public int TypePriority } } - #endregion Properties - - #region Commands - - public ICommand UnbindCommand => _unbindCommand ??= new RelayCommand(UnbindAsync); + public ICommand UnbindCommand => _unbindCommand ??= new AsyncCommand(UnbindAsync); + + public ICommand IncrementCounterCommand => _incrementCounterCommand ??= new AsyncCommand(IncrementCounter); - private async void UnbindAsync(object obj) + private async Task UnbindAsync() { - if (_targetId == null - || Model?.Tag?.Id == null) - { + if (_fileId == null || Model?.Tag?.Id == null) return; - } try { - await UnbindTagTask(_targetId.Value, Model.Tag.Id, Model.Source); + await _fileTagService.UnbindTags(new UnbindTagRequest(_fileId.Value, Model.Tag.Id, Model.Value, + Model.Source)); + + OnReloadRequested(); } catch (Exception ex) { @@ -128,17 +120,28 @@ private async void UnbindAsync(object obj) } } - private async Task UnbindTagTask(Guid imageId, Guid tagId, FileTagSource source) - => await _fileTagService.UnbindTags(new UnbindTagRequest(imageId, tagId, default, source)); - - #endregion Commands - - #region Methods - - public override string ToString() + private async Task IncrementCounter() { - return $"{Tag.Id} - {Tag.Title} : {Value}"; + if (_fileId == null || Model?.Tag?.Id == null || !IsCounterTag) + return; + + var newCountValue = "Counter:" + (CounterCount + 1); + + try + { + await _fileTagService.BindTags(new[] + { new FileTag(_fileId.Value, Model.Tag, newCountValue, Model.Source) }, + SameTagHandleStrategy.ReplaceExistingValue); + + OnReloadRequested(); + } + catch (Exception ex) + { + Debug.WriteLine(ex.Message); + } } - #endregion Methods + public override string ToString() => $"{Tag.Id} - {Tag.Title} : {Value}"; + + protected virtual void OnReloadRequested() => _updateAction?.Invoke(); } diff --git a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/MainWindowVM.cs b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/MainWindowVM.cs index 8c8d591c..53c0c027 100644 --- a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/MainWindowVM.cs +++ b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/MainWindowVM.cs @@ -136,7 +136,7 @@ private void OnViewOnSelectedItemsChanged(object sender, EventArgs args) if (SelectedItem == null) return; - TagSearchVM.UpdateCurrentTags(SelectedItem); + TagSearchVM.UpdateCurrentTags(SelectedItem.DbId); FileInfoVM.UpdateCurrentInfo(SelectedItem, NavigatorList.IndexOf(SelectedItem)); } diff --git a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/TagSearchVM.cs b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/TagSearchVM.cs index 12fa4a88..0475498d 100644 --- a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/TagSearchVM.cs +++ b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/TagSearchVM.cs @@ -228,23 +228,23 @@ public bool IsRateSetted #region Public methods - public async void UpdateCurrentTags(INavigatorListEntry listEntry) + public async void UpdateCurrentTags(Guid? fileId) { - if (listEntry?.DbId == null) + if (fileId == null) { IsRateSetted = false; return; } - var id = listEntry.DbId.Value; + var id = fileId.Value; var tags = await _fileTagService.GetFileTags(id); _lastListEntryId = id; var tagVmsCollection = tags - .Select(x => new BindedTagVM(x, listEntry.DbId)) + .Select(x => new BindedTagVM(x, id, () => UpdateCurrentTags(_lastListEntryId))) .ToList(); CurrentTagsSources.Clear(); diff --git a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/styles.xaml b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/styles.xaml index 75dd0fd3..447b94d6 100644 --- a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/styles.xaml +++ b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/styles.xaml @@ -237,11 +237,32 @@ + - + + + + + - + + + + + - + -