From 0dcf1fa2da9adf732f4cae77519d289705e13f43 Mon Sep 17 00:00:00 2001 From: Alex <15199219+muskit@users.noreply.github.com> Date: Mon, 1 Sep 2025 13:08:38 -0700 Subject: [PATCH] add search function --- src/Data/Database.cs | 26 +++++++++++++++++++---- src/Data/Song/Song.cs | 9 ++++++-- src/UI/Views/Selection/Selection.axaml | 2 +- src/UI/Views/Selection/Selection.axaml.cs | 17 +++++++++++++-- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/Data/Database.cs b/src/Data/Database.cs index 490cabd..6a69b90 100644 --- a/src/Data/Database.cs +++ b/src/Data/Database.cs @@ -2,11 +2,10 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; +using System.Linq; using Avalonia.Threading; -using FFMpegCore.Arguments; using MercuryConverter.Utility; using SaturnData.Notation.Core; -using Tmds.DBus.Protocol; using UAssetAPI; using UAssetAPI.ExportTypes; using UAssetAPI.PropertyTypes.Objects; @@ -118,13 +117,32 @@ public static class Database song.Levels[(int)diff] = (lvl, data[Consts.DIFF_DESIGNER_KEY[diff]].ToString()!); } - Dispatcher.UIThread.Post(() => Songs.Add(song)); + Dispatcher.UIThread.Invoke(() => Songs.Add(song)); } catch (Exception e) { Console.WriteLine($"Couldn't construct a song!\n{e}"); } } - Console.WriteLine("Data setup finished."); + Console.WriteLine($"Data setup finished with {Songs.Count} songs."); + + // SEARCH TEST + var camellia = Search("camellia").ToList(); + camellia.ForEach(Console.WriteLine); + } + + public static IEnumerable Search(string substr) + { + var sanitized = substr.ToLower().Trim(); + + if (sanitized == "") + return Songs; + + return Songs.Where(s => + { + return + s.Artist.ToLower().Contains(sanitized) || + s.Name.ToLower().Contains(sanitized); + }); } } \ No newline at end of file diff --git a/src/Data/Song/Song.cs b/src/Data/Song/Song.cs index 2e90c00..666d082 100644 --- a/src/Data/Song/Song.cs +++ b/src/Data/Song/Song.cs @@ -73,8 +73,8 @@ public class Song e.Reading = Rubi; e.Artist = Artist; e.BpmMessage = BpmMessage; - e.PreviewBegin = PreviewTime*1000f; - e.PreviewLength = PreviewLen*1000f; + e.PreviewBegin = PreviewTime * 1000f; + e.PreviewLength = PreviewLen * 1000f; e.ClearThreshold = clearThreshold; e.Difficulty = diff; e.Level = l.Value.Item1; @@ -104,4 +104,9 @@ public class Song return ret; } + + public override string ToString() + { + return $"[{Id}] {Artist} - {Name}"; + } } \ No newline at end of file diff --git a/src/UI/Views/Selection/Selection.axaml b/src/UI/Views/Selection/Selection.axaml index 1c70bf6..a56679d 100644 --- a/src/UI/Views/Selection/Selection.axaml +++ b/src/UI/Views/Selection/Selection.axaml @@ -42,7 +42,7 @@ - + 0) { Song song = e.AddedItems.Count > 0 ? - (Song) e.AddedItems[e.AddedItems.Count - 1]! : - (Song) ListingTable.SelectedItems[ListingTable.SelectedItems.Count - 1]!; + (Song)e.AddedItems[e.AddedItems.Count - 1]! : + (Song)ListingTable.SelectedItems[ListingTable.SelectedItems.Count - 1]!; Dispatcher.UIThread.Post(() => { @@ -104,4 +104,17 @@ public partial class Selection : Panel }); } } + + private void OnSearchTextChange(object? _, TextChangedEventArgs args) + { + var src = (TextBox)args.Source!; + if (string.IsNullOrEmpty(src.Text)) + { + Dispatcher.UIThread.Post(() => ListingTable.ItemsSource = Database.Songs); + return; + } + + var searchResults = Database.Search(src.Text.ToLower()); + Dispatcher.UIThread.Post(() => ListingTable.ItemsSource = searchResults); + } } \ No newline at end of file