修复历史功能相关问题 (#589)

This commit is contained in:
Poker 2025-01-01 03:24:28 +08:00 committed by GitHub
parent 355f955729
commit c79be233cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 46 additions and 37 deletions

View File

@ -24,7 +24,6 @@ using System.IO.Compression;
using System.IO;
using System.Threading.Tasks;
using Microsoft.IO;
using System.Linq;
namespace Pixeval.Utilities;

View File

@ -133,13 +133,9 @@ public static partial class AppInfo
public static void RestoreHistories()
{
var downloadHistoryPersistentManager = App.AppViewModel.AppServiceProvider.GetRequiredService<DownloadHistoryPersistentManager>();
var browseHistoryPersistentManager = App.AppViewModel.AppServiceProvider.GetRequiredService<BrowseHistoryPersistentManager>();
foreach (var downloadTaskGroup in downloadHistoryPersistentManager.Enumerate())
App.AppViewModel.DownloadManager.QueueTask(downloadTaskGroup);
foreach (var browseHistoryEntry in browseHistoryPersistentManager.Enumerate())
browseHistoryPersistentManager.ObservableEntries.Insert(0, browseHistoryEntry);
}
public static void ClearConfig()

View File

@ -18,7 +18,6 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System.Collections.ObjectModel;
using LiteDB;
using Microsoft.Extensions.DependencyInjection;
using Pixeval.CoreApi.Global.Enum;
@ -29,18 +28,14 @@ namespace Pixeval.Database.Managers;
public class BrowseHistoryPersistentManager(ILiteDatabase db, int maximumRecords)
: SimplePersistentManager<BrowseHistoryEntry>(db, maximumRecords)
{
public ObservableCollection<BrowseHistoryEntry> ObservableEntries { get; } = [];
public static void AddHistory(IWorkEntry entry)
{
if (entry.Id is 0)
return;
var type = entry is Illustration ? SimpleWorkType.IllustAndManga : SimpleWorkType.Novel;
var manager = App.AppViewModel.AppServiceProvider.GetRequiredService<BrowseHistoryPersistentManager>();
if (manager.TryDelete(x => x.Id == entry.Id && x.Type == type) is { } e)
manager.ObservableEntries.Remove(e);
_ = manager.TryDelete(x => x.Id == entry.Id && x.Type == type);
var browseHistoryEntry = new BrowseHistoryEntry(entry);
manager.Insert(browseHistoryEntry);
manager.ObservableEntries.Insert(0, browseHistoryEntry);
}
}

View File

@ -58,6 +58,11 @@ public class DownloadHistoryPersistentManager(ILiteDatabase collection, int maxi
return Collection.Find(_ => true, 0, count).Select(ToDownloadTaskGroup);
}
public IEnumerable<IDownloadTaskGroup> SelectLast(int count)
{
return Collection.Find(_ => true, Collection.Count() - count, count).Select(ToDownloadTaskGroup);
}
/// <summary>
/// 删除
/// </summary>

View File

@ -56,6 +56,8 @@ public interface IPersistentManager<TEntry, out TModel> where TEntry : IHistoryE
IEnumerable<TModel> Select(int count);
IEnumerable<TModel> SelectLast(int count);
TEntry? TryDelete(Expression<Func<TEntry, bool>> predicate);
int Delete(Expression<Func<TEntry, bool>> predicate);

View File

@ -1,4 +1,4 @@
#region Copyright (c) Pixeval/Pixeval
#region Copyright (c) Pixeval/Pixeval
// GPL v3 License
//
// Pixeval/Pixeval
@ -18,8 +18,29 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Linq;
using LiteDB;
using Microsoft.Extensions.DependencyInjection;
namespace Pixeval.Database.Managers;
public class SearchHistoryPersistentManager(ILiteDatabase db, int maximumRecords) : SimplePersistentManager<SearchHistoryEntry>(db, maximumRecords);
public class SearchHistoryPersistentManager(ILiteDatabase db, int maximumRecords)
: SimplePersistentManager<SearchHistoryEntry>(db, maximumRecords)
{
public static void AddHistory(string text, string? optTranslatedName = null)
{
if (string.IsNullOrWhiteSpace(text))
return;
var manager = App.AppViewModel.AppServiceProvider.GetRequiredService<SearchHistoryPersistentManager>();
if (manager.Count is 0 || manager.SelectLast(1).FirstOrDefault() is { Value: var last } && last != text)
{
manager.Insert(new SearchHistoryEntry
{
Value = text,
TranslatedName = optTranslatedName,
Time = DateTime.Now
});
}
}
}

View File

@ -64,6 +64,11 @@ public abstract class SimplePersistentManager<T>(ILiteDatabase db, int maximumRe
return Collection.Find(_ => true, 0, count);
}
public IEnumerable<T> SelectLast(int count)
{
return Collection.Find(_ => true, Collection.Count() - count, count);
}
public T? TryDelete(Expression<Func<T, bool>> predicate)
{
if (Collection.FindOne(predicate) is { } e)
@ -95,7 +100,7 @@ public abstract class SimplePersistentManager<T>(ILiteDatabase db, int maximumRe
if (Collection.Count() > limit)
{
var last = Collection.FindAll().Take(^limit..).ToHashSet();
_ = Delete(e => !last.Contains(e!));
_ = Delete(e => !last.Contains(e));
}
}

View File

@ -19,7 +19,6 @@
#endregion
using System;
using System.Diagnostics;
using System.Numerics;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;

View File

@ -84,6 +84,7 @@
Content="{fluent:SymbolIcon Symbol=Settings}" />
</CommandBar>
<AutoSuggestBox
x:Name="MainPageAutoSuggestionBox"
x:Uid="/MainPage/MainPageAutoSuggestionBox"
VerticalAlignment="Center"
AllowDrop="True"

View File

@ -41,7 +41,6 @@ using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.Windows.AppLifecycle;
using Pixeval.Activation;
using Pixeval.Database;
using Pixeval.Database.Managers;
using Pixeval.Messages;
using Pixeval.Pages.Capability;
@ -97,6 +96,7 @@ public sealed partial class MainPage
_ = WeakReferenceMessenger.Default.TryRegister<MainPage, WorkTagClickedMessage>(this, (_, message) =>
{
var window = WindowFactory.ForkedWindows[HWnd];
MainPageAutoSuggestionBox.Text = message.Tag;
window.AppWindow.MoveInZOrderAtTop();
PerformSearchWork(message.Type, message.Tag);
});
@ -242,30 +242,16 @@ public sealed partial class MainPage
private void PerformSearchWork(SimpleWorkType type, string text, string? optTranslatedName = null)
{
PerformSearch(text, optTranslatedName);
SearchHistoryPersistentManager.AddHistory(text, optTranslatedName);
NavigationView.SelectedItem = null;
_ = MainPageRootFrame.Navigate(typeof(SearchWorksPage), (type, text));
}
private void PerformSearchUser(string text)
{
PerformSearch(text);
_ = MainPageRootFrame.Navigate(typeof(SearchUsersPage), text);
}
private void PerformSearch(string text, string? optTranslatedName = null)
{
var manager = App.AppViewModel.AppServiceProvider.GetRequiredService<SearchHistoryPersistentManager>();
if (manager.Count is 0 || manager.Select(count: 1).FirstOrDefault() is { Value: var last } && last != text)
{
manager.Insert(new SearchHistoryEntry
{
Value = text,
TranslatedName = optTranslatedName,
Time = DateTime.Now
});
}
SearchHistoryPersistentManager.AddHistory(text);
NavigationView.SelectedItem = null;
_ = MainPageRootFrame.Navigate(typeof(SearchUsersPage), text);
}
private async void OpenSearchSettingButton_OnClicked(object sender, RoutedEventArgs e)

View File

@ -45,7 +45,7 @@ public sealed partial class BrowsingHistoryPage : IScrollViewHost
{
var manager = App.AppViewModel.AppServiceProvider.GetRequiredService<BrowseHistoryPersistentManager>();
var type = SimpleWorkTypeComboBox.GetSelectedItem<SimpleWorkType>();
var source = manager.ObservableEntries
var source = manager.Enumerate()
.SelectNotNull(t => t.TryGetEntry(type))
.ToAsyncEnumerable();

View File

@ -84,7 +84,7 @@ public record SuggestionModel(string? Name, string? TranslatedName, SuggestionTy
public static SuggestionModel FromHistory(SearchHistoryEntry history)
{
return new SuggestionModel(history.Value, null, SuggestionType.History);
return new SuggestionModel(history.Value, history.TranslatedName, SuggestionType.History);
}
/// <summary>

View File

@ -102,7 +102,7 @@ public class SuggestionStateMachine
{
var newItems = new List<SuggestionModel>();
var manager = App.AppViewModel.AppServiceProvider.GetRequiredService<SearchHistoryPersistentManager>();
var histories = manager.Select(count: App.AppViewModel.AppSettings.MaximumSuggestionBoxSearchHistory).OrderByDescending(e => e.Time).SelectNotNull(SuggestionModel.FromHistory);
var histories = manager.SelectLast(count: App.AppViewModel.AppSettings.MaximumSuggestionBoxSearchHistory).Reverse().SelectNotNull(SuggestionModel.FromHistory);
newItems.AddRange(histories);
var prior = App.AppViewModel.AppSettings.SimpleWorkType is SimpleWorkType.IllustAndManga;
if (prior)