Skip to content

Commit

Permalink
Add refresh button to the top bar
Browse files Browse the repository at this point in the history
Reset local room cache on file deletion
  • Loading branch information
ImoutoChan committed Oct 18, 2023
1 parent 7f3673e commit 5962c9c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@

<metroControl:MetroWindow.RightWindowCommands>
<metroControl:WindowCommands>

<Button Content="Refresh"
Command="{Binding RefreshCommand}"/>
<Button Content="Tags"
Command="{Binding Path=ToggleShowTagsCommand}" />
<Button Content="Images on row 2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
Imouto Viewer
# Imouto Navigator

Collection organizer for your images and videos.

## Features

* TODO will describe it later

* Top bar
* REFRESH button to reload

* File context menu
* Delete
* Delete file from collection and move it to !Deleted folder
* Also delete this file from local cache and current view~~~~

# Imouto Viewer
=============
The simple minimalistic image viewer with metro style window.

Expand All @@ -12,7 +28,7 @@ Imouto Viewer
Drag & drop available. Current image is draggable as file. Files with supported formats is droppable.


Hot keys
# Hot keys
=============
View:
F11 | ALT+ENTER — full-screen/default mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,9 @@ public async Task<int> CountFiles1(
.CountSearchFilesAsync(new SearchFilesCountQuery(_mapper.Map<List<TagSearchEntry>>(tags)), ct);
}

public Task RemoveFile(Guid fileId) => _collectionFilesClient.RemoveAsync(fileId);
public async Task RemoveFile(Guid fileId)
{
await _collectionFilesClient.RemoveAsync(fileId);
_roomCache.Clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ImoutoRebirth.Navigator.Services.Tags.Model;
using ImoutoRebirth.RoomService.WebApi.Client;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Primitives;

namespace ImoutoRebirth.Navigator.Services.Tags;

Expand All @@ -13,6 +14,8 @@ internal interface IRoomCache
Task<IReadOnlyCollection<File>> GetFilesByIds(IReadOnlyCollection<Guid> ids);

Task<IReadOnlyCollection<Guid>> GetIds(Guid? collectionId, int? skip, int? take);

void Clear();
}

internal class RoomCache : IRoomCache
Expand All @@ -22,6 +25,7 @@ internal class RoomCache : IRoomCache
private readonly CollectionFilesClient _collectionFilesClient;
private readonly IMemoryCache _memoryCache;
private readonly IMapper _mapper;
private static CancellationTokenSource _resetCacheToken = new();

public RoomCache(CollectionFilesClient collectionFilesClient, IMemoryCache memoryCache, IMapper mapper)
{
Expand All @@ -35,6 +39,7 @@ public async Task<IReadOnlyCollection<Guid>> GetIds(Guid? collectionId, int? ski
var key = "collection_file_ids_" + collectionId + "_" + skip + "_" + take;

var requestUsed = false;

var ids = await _memoryCache.GetOrCreateAsync(key, async entry =>
{
var ids = await _collectionFilesClient.SearchIdsAsync(new CollectionFilesRequest(
Expand All @@ -47,6 +52,8 @@ public async Task<IReadOnlyCollection<Guid>> GetIds(Guid? collectionId, int? ski

requestUsed = true;

entry.AddExpirationToken(new CancellationChangeToken(_resetCacheToken.Token));

return ids;
}) ?? Array.Empty<Guid>();

Expand All @@ -66,6 +73,17 @@ public async Task<IReadOnlyCollection<Guid>> GetIds(Guid? collectionId, int? ski
return ids;
}

public void Clear()
{
if (_resetCacheToken is { IsCancellationRequested: false, Token.CanBeCanceled: true })
{
_resetCacheToken.Cancel();
_resetCacheToken.Dispose();
}

_resetCacheToken = new CancellationTokenSource();
}

public async Task<IReadOnlyCollection<File>> GetFilesFromCollection(Guid? collectionId, int? skip, int? take)
{
var ids = await GetIds(collectionId, skip, take);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ namespace ImoutoRebirth.Navigator.Utils;
public interface IAsyncCommand : ICommand
{
Task ExecuteAsync();

bool CanExecute();
}

public class AsyncCommand : IAsyncCommand
{
public event EventHandler CanExecuteChanged;
public event EventHandler? CanExecuteChanged;

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

public AsyncCommand(
Func<Task> execute,
Func<bool> canExecute = null)
Func<bool>? canExecute = null)
{
_execute = execute;
_canExecute = canExecute;
Expand Down Expand Up @@ -47,20 +48,20 @@ public async Task ExecuteAsync()
RaiseCanExecuteChanged();
}

public void RaiseCanExecuteChanged()
private void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}

#region Explicit implementations
bool ICommand.CanExecute(object parameter)
bool ICommand.CanExecute(object? parameter)
{
return CanExecute();
}

async void ICommand.Execute(object parameter)
async void ICommand.Execute(object? parameter)
{
await ExecuteAsync();
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using ImoutoRebirth.Navigator.Services.Tags;
using ImoutoRebirth.Navigator.Services.Tags.Model;
using ImoutoRebirth.Navigator.UserControls;
using ImoutoRebirth.Navigator.Utils;
using ImoutoRebirth.Navigator.ViewModel.ListEntries;
using MahApps.Metro.Controls.Dialogs;
using Newtonsoft.Json;
Expand Down Expand Up @@ -221,6 +222,8 @@ public string StatusToolTip

public ICommand ReverseCommand { get; set; }

public ICommand RefreshCommand { get; set; }

public ICommand ZoomInCommand { get; set; }

public ICommand ZoomOutCommand { get; set; }
Expand Down Expand Up @@ -252,6 +255,8 @@ private void InitializeCommands()
ShuffleCommand = new RelayCommand(_ => ShuffleNavigatorList());

ReverseCommand = new RelayCommand(_ => ReverseNavigatorList());

RefreshCommand = new AsyncCommand(Reload);

ZoomInCommand = new RelayCommand(_ =>
{
Expand Down

0 comments on commit 5962c9c

Please sign in to comment.