Frequency navigation
This commit is contained in:
@@ -89,7 +89,7 @@ public partial class FrequencyNavigationService : IFrequencyNavigationService, I
|
||||
if (TryAgeContainerScores() || DateTime.Now - _lastSave > TimeSpan.FromMinutes(5))
|
||||
{
|
||||
}
|
||||
|
||||
//TODO: move to if above
|
||||
await SaveStateAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -113,7 +113,7 @@ public partial class FrequencyNavigationService : IFrequencyNavigationService, I
|
||||
var itemsToRemove = new List<string>();
|
||||
foreach (var container in _containerScores)
|
||||
{
|
||||
var newScore = (int) Math.Floor(container.Value.Score * 0.9);
|
||||
var newScore = (int)Math.Floor(container.Value.Score * 0.9);
|
||||
if (newScore > 0)
|
||||
{
|
||||
container.Value.Score = newScore;
|
||||
@@ -136,14 +136,18 @@ public partial class FrequencyNavigationService : IFrequencyNavigationService, I
|
||||
return new List<string>();
|
||||
|
||||
_saveLock.Wait();
|
||||
var matchingContainers = _containerScores
|
||||
.Where(c => c.Key.Contains(searchText, StringComparison.OrdinalIgnoreCase))
|
||||
.OrderBy(c => GetWeightedScore(c.Value.Score, c.Value.LastAccessed))
|
||||
.Select(c => c.Key)
|
||||
.ToList();
|
||||
|
||||
_saveLock.Release();
|
||||
return matchingContainers;
|
||||
try
|
||||
{
|
||||
return _containerScores
|
||||
.Where(c => c.Key.Contains(searchText, StringComparison.OrdinalIgnoreCase))
|
||||
.OrderBy(c => GetWeightedScore(c.Value.Score, c.Value.LastAccessed))
|
||||
.Select(c => c.Key)
|
||||
.ToList();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_saveLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private int GetWeightedScore(int score, DateTime lastAccess)
|
||||
@@ -159,36 +163,48 @@ public partial class FrequencyNavigationService : IFrequencyNavigationService, I
|
||||
};
|
||||
}
|
||||
|
||||
public async Task InitAsync()
|
||||
{
|
||||
await LoadStateAsync();
|
||||
}
|
||||
public async Task InitAsync() => await LoadStateAsync();
|
||||
|
||||
private async Task LoadStateAsync()
|
||||
{
|
||||
if (!File.Exists(_dbPath))
|
||||
return;
|
||||
|
||||
await _saveLock.WaitAsync();
|
||||
await using var dbStream = File.OpenRead(_dbPath);
|
||||
var containerScores = await JsonSerializer.DeserializeAsync<Dictionary<string, ContainerFrequencyData>>(dbStream);
|
||||
if (containerScores is null) return;
|
||||
try
|
||||
{
|
||||
await _saveLock.WaitAsync();
|
||||
_logger.LogTrace("Loading frequency navigation state from file '{DbPath}'", _dbPath);
|
||||
await using var dbStream = File.OpenRead(_dbPath);
|
||||
var containerScores = await JsonSerializer.DeserializeAsync<Dictionary<string, ContainerFrequencyData>>(dbStream);
|
||||
if (containerScores is null) return;
|
||||
|
||||
_containerScores = containerScores;
|
||||
_saveLock.Release();
|
||||
_containerScores = containerScores;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error loading frequency navigation state");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_saveLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ExitAsync()
|
||||
{
|
||||
await SaveStateAsync();
|
||||
}
|
||||
public async Task ExitAsync() => await SaveStateAsync();
|
||||
|
||||
private async Task SaveStateAsync()
|
||||
{
|
||||
await _saveLock.WaitAsync();
|
||||
_lastSave = DateTime.Now;
|
||||
await using var dbStream = File.OpenWrite(_dbPath);
|
||||
await JsonSerializer.SerializeAsync(dbStream, _containerScores);
|
||||
_saveLock.Release();
|
||||
try
|
||||
{
|
||||
_lastSave = DateTime.Now;
|
||||
await using var dbStream = File.Create(_dbPath);
|
||||
await JsonSerializer.SerializeAsync(dbStream, _containerScores);
|
||||
dbStream.Flush();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_saveLock.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,22 @@
|
||||
using Avalonia.Input;
|
||||
using FileTime.App.Core.Services;
|
||||
using FileTime.App.Core.UserCommand;
|
||||
using FileTime.App.Core.ViewModels;
|
||||
using FileTime.App.FrequencyNavigation.Services;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Timeline;
|
||||
using MvvmGen;
|
||||
|
||||
namespace FileTime.App.FrequencyNavigation.ViewModels;
|
||||
|
||||
[ViewModel]
|
||||
[Inject(typeof(IFrequencyNavigationService), "_frequencyNavigationService")]
|
||||
[Inject(typeof(IUserCommandHandlerService), "_userCommandHandlerService")]
|
||||
[Inject(typeof(ITimelessContentProvider), "_timelessContentProvider")]
|
||||
public partial class FrequencyNavigationViewModel : IFrequencyNavigationViewModel
|
||||
{
|
||||
private string _searchText;
|
||||
|
||||
|
||||
[Property] private IObservable<bool> _showWindow;
|
||||
[Property] private List<string> _filteredMatches;
|
||||
[Property] private string _selectedItem;
|
||||
@@ -20,7 +27,7 @@ public partial class FrequencyNavigationViewModel : IFrequencyNavigationViewMode
|
||||
set
|
||||
{
|
||||
if (_searchText == value) return;
|
||||
|
||||
|
||||
_searchText = value;
|
||||
OnPropertyChanged();
|
||||
|
||||
@@ -29,18 +36,48 @@ public partial class FrequencyNavigationViewModel : IFrequencyNavigationViewMode
|
||||
}
|
||||
|
||||
public void Close()
|
||||
=> _frequencyNavigationService.CloseNavigationWindow();
|
||||
|
||||
public async void HandleKeyDown(KeyEventArgs keyEventArgs)
|
||||
{
|
||||
_frequencyNavigationService.CloseNavigationWindow();
|
||||
if (keyEventArgs.Key == Key.Down)
|
||||
{
|
||||
var nextItem = FilteredMatches.SkipWhile(i => i != SelectedItem).Skip(1).FirstOrDefault();
|
||||
|
||||
if (nextItem is not null)
|
||||
{
|
||||
SelectedItem = nextItem;
|
||||
}
|
||||
}
|
||||
else if (keyEventArgs.Key == Key.Up)
|
||||
{
|
||||
var previousItem = FilteredMatches.TakeWhile(i => i != SelectedItem).LastOrDefault();
|
||||
|
||||
if (previousItem is not null)
|
||||
{
|
||||
SelectedItem = previousItem;
|
||||
}
|
||||
}
|
||||
else if (keyEventArgs.Key == Key.Enter)
|
||||
{
|
||||
var targetContainer = await _timelessContentProvider.GetItemByFullNameAsync(new FullName(SelectedItem), PointInTime.Present);
|
||||
var openContainerCommand = new OpenContainerCommand(new AbsolutePath(_timelessContentProvider, targetContainer));
|
||||
await _userCommandHandlerService.HandleCommandAsync(openContainerCommand);
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
partial void OnInitialize()
|
||||
{
|
||||
_showWindow = _frequencyNavigationService.ShowWindow;
|
||||
}
|
||||
=> _showWindow = _frequencyNavigationService.ShowWindow;
|
||||
|
||||
private void UpdateFilteredMatches()
|
||||
{
|
||||
FilteredMatches = new List<string>(_frequencyNavigationService.GetMatchingContainers(_searchText));
|
||||
if (FilteredMatches.Contains(SelectedItem)) return;
|
||||
|
||||
SelectedItem = FilteredMatches.Count > 0
|
||||
? FilteredMatches[0]
|
||||
: null;
|
||||
}
|
||||
|
||||
string IModalViewModel.Name => "FrequencyNavigation";
|
||||
|
||||
Reference in New Issue
Block a user