From 2bdab78d0c9e6dc6a35c51673d5fe7180b6abb55 Mon Sep 17 00:00:00 2001 From: ImoutoChan Date: Sun, 29 Oct 2023 22:52:54 +0400 Subject: [PATCH] Add tag "my wallpapers" to the file when setting wallpaper from context menu --- CHANGELOG.md | 5 +- .../Services/Tags/FileTagService.cs | 32 ++++++++---- .../Services/Tags/IFileTagService.cs | 2 + .../Utils/AsyncCommand.cs | 50 +++++++++++++++++++ .../ViewModel/MainWindowVM.cs | 14 +++--- 5 files changed, 85 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a995df6..0ec45129 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ # Unreleased ### Infrastructure -* Optimize release notes builder +* Optimize release notes + +### Navigator +* Add tag "my wallpapers" to the file when setting wallpaper from context menu # 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 2c22a3a1..b1013456 100644 --- a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/FileTagService.cs +++ b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/FileTagService.cs @@ -58,6 +58,16 @@ await _filesClient.UnbindTagsAsync( } } + public async Task SetWasWallpaper(Guid fileId) + { + var tag = await GetOrCreateTag("my wallpapers", "LocalMeta", false); + + await _filesClient.BindTagsAsync( + new BindTagsCommand( + new List { new(fileId, MetadataSource.Manual, tag.Id, null) }, + SameTagHandleStrategy.ReplaceExistingValue)); + } + public async Task BindTags(IReadOnlyCollection fileTags) { var requests = _mapper.Map>(fileTags); @@ -84,24 +94,24 @@ public async Task> GetFileTags(Guid fileId) private async Task GetOrCreateTag(string name, string typeName, bool hasValue) { - var rateTags = await _tagService.SearchTags(name, 1); - var rateTag = rateTags.FirstOrDefault(); + var tags = await _tagService.SearchTags(name, 1); + var tag = tags.FirstOrDefault(); - var rateTagFound = rateTag != null - && rateTag.Title == name - && rateTag.HasValue == hasValue - && rateTag.Type.Title == typeName; + var tagFound = tag != null + && tag.Title == name + && tag.HasValue == hasValue + && tag.Type.Title == typeName; - if (rateTagFound) - return rateTag!; + if (tagFound) + return tag!; var types = await _tagService.GеtTypes(); var localType = types.First(x => x.Title == typeName); await _tagService.CreateTag(localType.Id, name, hasValue, Array.Empty()); - rateTags = await _tagService.SearchTags(name, 1); - rateTag = rateTags.First(x => x.Title == name && x.HasValue == hasValue && x.Type.Title == typeName); + tags = await _tagService.SearchTags(name, 1); + tag = tags.First(x => x.Title == name && x.HasValue == hasValue && x.Type.Title == typeName); - return rateTag; + return tag; } } diff --git a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/IFileTagService.cs b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/IFileTagService.cs index 8186981a..960ae359 100644 --- a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/IFileTagService.cs +++ b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Services/Tags/IFileTagService.cs @@ -8,6 +8,8 @@ interface IFileTagService Task SetFavorite(Guid fileId, bool value); + Task SetWasWallpaper(Guid selectedItemDbId); + Task BindTags(IReadOnlyCollection fileTags); Task UnbindTags(params UnbindTagRequest[] tagsToUnbind); diff --git a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Utils/AsyncCommand.cs b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Utils/AsyncCommand.cs index 15a6e891..b3c20d60 100644 --- a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Utils/AsyncCommand.cs +++ b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/Utils/AsyncCommand.cs @@ -9,6 +9,13 @@ public interface IAsyncCommand : ICommand bool CanExecute(); } +public interface IAsyncCommand : ICommand +{ + Task ExecuteAsync(TParamType? parameter); + + bool CanExecute(TParamType? parameter); +} + public class AsyncCommand : IAsyncCommand { public event EventHandler? CanExecuteChanged; @@ -65,3 +72,46 @@ async void ICommand.Execute(object? parameter) } #endregion } + +public class AsyncCommand : IAsyncCommand where TParamType : class +{ + public event EventHandler? CanExecuteChanged; + + private bool _isExecuting; + private readonly Func _execute; + private readonly Func? _canExecute; + + public AsyncCommand( + Func execute, + Func? canExecute = null) + { + _execute = execute; + _canExecute = canExecute; + } + + public bool CanExecute(TParamType? parameter) => !_isExecuting && (_canExecute?.Invoke(parameter) ?? true); + + public async Task ExecuteAsync(TParamType? parameter) + { + if (CanExecute(parameter)) + { + try + { + _isExecuting = true; + await _execute(parameter); + } + finally + { + _isExecuting = false; + } + } + + RaiseCanExecuteChanged(); + } + + private void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty); + + bool ICommand.CanExecute(object? parameter) => CanExecute(parameter as TParamType); + + async void ICommand.Execute(object? parameter) => await ExecuteAsync(parameter as TParamType); +} diff --git a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/MainWindowVM.cs b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/MainWindowVM.cs index d50cd96c..8c8d591c 100644 --- a/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/MainWindowVM.cs +++ b/Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/ViewModel/MainWindowVM.cs @@ -287,7 +287,7 @@ private void InitializeCommands() LoadPreviewsCommand = new RelayCommand(_ => LoadPreviews()); RemoveImageCommand = new RelayCommand(RemoveImage); - SetAsWallpaperCommand = new RelayCommand(SetAsWallpaper); + SetAsWallpaperCommand = new AsyncCommand(SetAsWallpaper); ShowInExplorerCommand = new RelayCommand(ShowInExplorer); CopyCommand = new RelayCommand(CopySelected); @@ -600,19 +600,21 @@ await _view.ShowMessageDialog( Status = "File successfully removed"; } - private void SetAsWallpaper(object o) + private async Task SetAsWallpaper(INavigatorListEntry? entry) { - var selectedItem = o as INavigatorListEntry; - - var path = selectedItem?.Path; + var path = entry?.Path; if (path == null) return; - if (selectedItem is not { Type: ListEntryType.Image or ListEntryType.Png }) + if (entry is not { Type: ListEntryType.Image or ListEntryType.Png }) return; WindowsDesktopService.SetWallpaper(path); + Status = "Wallpaper set"; + + if (entry.DbId.HasValue) + await _fileTagService.SetWasWallpaper(entry.DbId.Value); } private void ShowInExplorer(object o)