add search function

This commit is contained in:
Alex
2025-09-01 13:08:38 -07:00
parent e41a5eaea9
commit 0dcf1fa2da
4 changed files with 45 additions and 9 deletions
+22 -4
View File
@@ -2,11 +2,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.IO; using System.IO;
using System.Linq;
using Avalonia.Threading; using Avalonia.Threading;
using FFMpegCore.Arguments;
using MercuryConverter.Utility; using MercuryConverter.Utility;
using SaturnData.Notation.Core; using SaturnData.Notation.Core;
using Tmds.DBus.Protocol;
using UAssetAPI; using UAssetAPI;
using UAssetAPI.ExportTypes; using UAssetAPI.ExportTypes;
using UAssetAPI.PropertyTypes.Objects; using UAssetAPI.PropertyTypes.Objects;
@@ -118,13 +117,32 @@ public static class Database
song.Levels[(int)diff] = (lvl, data[Consts.DIFF_DESIGNER_KEY[diff]].ToString()!); 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) catch (Exception e)
{ {
Console.WriteLine($"Couldn't construct a song!\n{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<Song> 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);
});
} }
} }
+7 -2
View File
@@ -73,8 +73,8 @@ public class Song
e.Reading = Rubi; e.Reading = Rubi;
e.Artist = Artist; e.Artist = Artist;
e.BpmMessage = BpmMessage; e.BpmMessage = BpmMessage;
e.PreviewBegin = PreviewTime*1000f; e.PreviewBegin = PreviewTime * 1000f;
e.PreviewLength = PreviewLen*1000f; e.PreviewLength = PreviewLen * 1000f;
e.ClearThreshold = clearThreshold; e.ClearThreshold = clearThreshold;
e.Difficulty = diff; e.Difficulty = diff;
e.Level = l.Value.Item1; e.Level = l.Value.Item1;
@@ -104,4 +104,9 @@ public class Song
return ret; return ret;
} }
public override string ToString()
{
return $"[{Id}] {Artist} - {Name}";
}
} }
+1 -1
View File
@@ -42,7 +42,7 @@
<!-- Song Listing Table --> <!-- Song Listing Table -->
<DockPanel> <DockPanel>
<DockPanel Margin="0 4 0 8" DockPanel.Dock="Top"> <DockPanel Margin="0 4 0 8" DockPanel.Dock="Top">
<TextBox Watermark="Search for title, artist, designer..."/> <TextBox Watermark="Search for song title or artist..." TextChanged="OnSearchTextChange"/>
</DockPanel> </DockPanel>
<UserControl Background="{DynamicResource DataGridContentBackground}"> <UserControl Background="{DynamicResource DataGridContentBackground}">
<DataGrid Name="ListingTable" IsReadOnly="True" SelectionMode="Extended" ItemsSource="{x:Static data:Database.Songs}" <DataGrid Name="ListingTable" IsReadOnly="True" SelectionMode="Extended" ItemsSource="{x:Static data:Database.Songs}"
+15 -2
View File
@@ -73,8 +73,8 @@ public partial class Selection : Panel
if (ListingTable.SelectedItems.Count > 0) if (ListingTable.SelectedItems.Count > 0)
{ {
Song song = e.AddedItems.Count > 0 ? Song song = e.AddedItems.Count > 0 ?
(Song) e.AddedItems[e.AddedItems.Count - 1]! : (Song)e.AddedItems[e.AddedItems.Count - 1]! :
(Song) ListingTable.SelectedItems[ListingTable.SelectedItems.Count - 1]!; (Song)ListingTable.SelectedItems[ListingTable.SelectedItems.Count - 1]!;
Dispatcher.UIThread.Post(() => 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);
}
} }