Skip to content

Commit

Permalink
Add tag "my wallpapers" to the file when setting wallpaper from conte…
Browse files Browse the repository at this point in the history
…xt menu
  • Loading branch information
ImoutoChan committed Oct 29, 2023
1 parent ffdaf5d commit 2bdab78
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 18 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BindTag> { new(fileId, MetadataSource.Manual, tag.Id, null) },
SameTagHandleStrategy.ReplaceExistingValue));
}

public async Task BindTags(IReadOnlyCollection<FileTag> fileTags)
{
var requests = _mapper.Map<IReadOnlyCollection<BindTag>>(fileTags);
Expand All @@ -84,24 +94,24 @@ public async Task<IReadOnlyCollection<FileTag>> GetFileTags(Guid fileId)

private async Task<Tag> 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<string>());

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface IFileTagService

Task SetFavorite(Guid fileId, bool value);

Task SetWasWallpaper(Guid selectedItemDbId);

Task BindTags(IReadOnlyCollection<FileTag> fileTags);

Task UnbindTags(params UnbindTagRequest[] tagsToUnbind);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ public interface IAsyncCommand : ICommand
bool CanExecute();
}

public interface IAsyncCommand<in TParamType> : ICommand
{
Task ExecuteAsync(TParamType? parameter);

bool CanExecute(TParamType? parameter);
}

public class AsyncCommand : IAsyncCommand
{
public event EventHandler? CanExecuteChanged;
Expand Down Expand Up @@ -65,3 +72,46 @@ async void ICommand.Execute(object? parameter)
}
#endregion
}

public class AsyncCommand<TParamType> : IAsyncCommand<TParamType> where TParamType : class
{
public event EventHandler? CanExecuteChanged;

private bool _isExecuting;
private readonly Func<TParamType?, Task> _execute;
private readonly Func<TParamType?, bool>? _canExecute;

public AsyncCommand(
Func<TParamType?, Task> execute,
Func<TParamType?, bool>? 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private void InitializeCommands()

LoadPreviewsCommand = new RelayCommand(_ => LoadPreviews());
RemoveImageCommand = new RelayCommand(RemoveImage);
SetAsWallpaperCommand = new RelayCommand(SetAsWallpaper);
SetAsWallpaperCommand = new AsyncCommand<INavigatorListEntry>(SetAsWallpaper);
ShowInExplorerCommand = new RelayCommand(ShowInExplorer);

CopyCommand = new RelayCommand(CopySelected);
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2bdab78

Please sign in to comment.