Skip to content

Commit

Permalink
Add counter tags initial support: you have to create tag with value "…
Browse files Browse the repository at this point in the history
…Counter:0", then you can increase it in left tags window
  • Loading branch information
ImoutoChan committed Oct 29, 2023
1 parent 2bdab78 commit a2f729a
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 67 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ await _filesClient.BindTagsAsync(
SameTagHandleStrategy.ReplaceExistingValue));
}

public async Task BindTags(IReadOnlyCollection<FileTag> fileTags)
public async Task BindTags(
IReadOnlyCollection<FileTag> fileTags,
SameTagHandleStrategy strategy = SameTagHandleStrategy.AddNewFileTag)
{
var requests = _mapper.Map<IReadOnlyCollection<BindTag>>(fileTags);

await _filesClient.BindTagsAsync(
new BindTagsCommand(requests, SameTagHandleStrategy.AddNewFileTag));
new BindTagsCommand(requests, strategy));
}

public async Task UnbindTags(params UnbindTagRequest[] tagsToUnbind)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -10,7 +11,9 @@ interface IFileTagService

Task SetWasWallpaper(Guid selectedItemDbId);

Task BindTags(IReadOnlyCollection<FileTag> fileTags);
Task BindTags(
IReadOnlyCollection<FileTag> fileTags,
SameTagHandleStrategy strategy = SameTagHandleStrategy.AddNewFileTag);

Task UnbindTags(params UnbindTagRequest[] tagsToUnbind);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,9 +12,7 @@ namespace ImoutoRebirth.Navigator.ViewModel;

class BindedTagVM : VMBase
{
#region Static members

private static readonly List<string> TypePriorities = new List<string>
private static readonly List<string> TypePriorities = new()
{
"Artist",
"Copyright",
Expand All @@ -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<IFileTagService>();
_targetId = targetId;
_fileId = fileId;
_updateAction = updateAction;
}

#endregion Constructors

#region Properties

public SearchType SearchType
{
get => _searchType;
Expand All @@ -67,7 +56,7 @@ public SearchType SearchType

public string Synonyms => string.Join(", ", Tag.SynonymsCollection);

public string Value
public string? Value
{
get => Model.Value;
set
Expand All @@ -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
{
Expand All @@ -104,41 +98,50 @@ 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)
{
Debug.WriteLine(ex.Message);
}
}

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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
62 changes: 50 additions & 12 deletions Source/ImoutoRebirth.Navigator/ImoutoRebirth.Navigator/styles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,32 @@
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<TextBlock Grid.Column="0"
<Button Grid.Column="0"
Width="15"
Height="15"
Margin="7, 0, 0, 0"
Style="{DynamicResource MahApps.Styles.Button.Circle}"
HorizontalAlignment="Left"
Command="{Binding IncrementCounterCommand}"
Visibility="{Binding
IsCounterTag,
Mode=OneWay,
Converter={StaticResource ConditionVisibilityConverter},
ConverterParameter=true,
FallbackValue=Collapsed}">
<iconPacks:PackIconModern
Width="6"
Height="6"
Kind="Add">
</iconPacks:PackIconModern>
</Button>

<TextBlock Grid.Column="1"
Margin="5,0,0,2">
<Hyperlink TextDecorations="None"
Command="{Binding RelativeSource={RelativeSource FindAncestor,
Expand All @@ -255,36 +276,53 @@
<MouseBinding Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ExploreTagCommand}"
CommandParameter="{Binding}"
MouseAction="MiddleClick" />
<MouseBinding Command="{Binding
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.DraftAddTagCommand}"
CommandParameter="{Binding}"
MouseAction="RightClick" />
<MouseBinding
Command="{Binding
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},
Path=DataContext.DraftAddTagCommand}"
CommandParameter="{Binding}"
MouseAction="RightClick" />
</TextBlock.InputBindings>

<TextBlock
Visibility="{Binding
IsCounterTag,
Mode=OneWay,
Converter={StaticResource ConditionVisibilityConverter},
ConverterParameter=true,
FallbackValue=Collapsed}">
<Run Text="{Binding CounterCountTitle, Mode=OneWay}"
Foreground="{DynamicResource MahApps.Brushes.Accent}"
FontFamily="Arial"
FontSize="14"
FontWeight="Bold"/>
</TextBlock>

<Run Text="{Binding Title, Mode=OneWay}"
Foreground="{Binding TypeBrush}"
FontFamily="Comic Sans MS"
FontSize="13"
FontWeight="Bold" />

<Run Text="{Binding Tag.Count, StringFormat=' ({0})', Mode=OneWay}"
Foreground="{StaticResource MahApps.Brushes.Gray3}" />
</TextBlock>
</Hyperlink>
</TextBlock>

<Button Grid.Column="1"
<Button Grid.Column="2"
Width="15"
Height="15"
Margin="0, 0, 0, 0"
Style="{DynamicResource MahApps.Styles.Button.Circle}"
HorizontalAlignment="Left"
Command="{Binding UnbindCommand}"
Visibility="{Binding IsEditable,
Mode=OneWay,
Converter={StaticResource ConditionVisibilityConverter},
ConverterParameter=true,
FallbackValue=Collapsed}">
Visibility="{Binding
IsEditable,
Mode=OneWay,
Converter={StaticResource ConditionVisibilityConverter},
ConverterParameter=true,
FallbackValue=Collapsed}">
<iconPacks:PackIconModern
Width="6"
Height="6"
Expand Down

0 comments on commit a2f729a

Please sign in to comment.