From faa1dc19cc5bd2bbaa9dfd06e973570efc2868fa Mon Sep 17 00:00:00 2001 From: Julian Lechner Date: Mon, 23 Nov 2026 10:24:12 +0100 Subject: [PATCH] feat: add music library page with search, edit and delete Add a Library page reachable from the navigation. It lists all songs from the SQLite database, filters them by title or artist, edits title, artist, license and attribution in a dialog, and deletes a song after a confirmation prompt. File path, duration and source stay read-only. LibraryController wraps SongRepository so the view only forwards user actions. Changes are written to the database immediately, and the list reloads every time the page is shown. Refs #3 Co-Authored-By: Claude Opus 5.5 --- .../Controllers/LibraryController.cs | 42 +++++++ src/Radiosender/Views/MainWindow.xaml | 3 + src/Radiosender/Views/Pages/LibraryPage.xaml | 67 +++++++++++ .../Views/Pages/LibraryPage.xaml.cs | 63 +++++++++++ src/Radiosender/Views/SongEditWindow.xaml | 41 +++++++ src/Radiosender/Views/SongEditWindow.xaml.cs | 34 ++++++ .../LibraryControllerTests.cs | 107 ++++++++++++++++++ 7 files changed, 357 insertions(+) create mode 100644 src/Radiosender/Controllers/LibraryController.cs create mode 100644 src/Radiosender/Views/Pages/LibraryPage.xaml create mode 100644 src/Radiosender/Views/Pages/LibraryPage.xaml.cs create mode 100644 src/Radiosender/Views/SongEditWindow.xaml create mode 100644 src/Radiosender/Views/SongEditWindow.xaml.cs create mode 100644 tests/Radiosender.Tests/LibraryControllerTests.cs diff --git a/src/Radiosender/Controllers/LibraryController.cs b/src/Radiosender/Controllers/LibraryController.cs new file mode 100644 index 0000000..832c617 --- /dev/null +++ b/src/Radiosender/Controllers/LibraryController.cs @@ -0,0 +1,42 @@ +using Radiosender.Models; +using Radiosender.Services; + +namespace Radiosender.Controllers; + +public class LibraryController(SongRepository repository) +{ + /// Returns all songs whose title or artist contains (case-insensitive). + public IReadOnlyList Search(string? query) + { + var songs = repository.GetAll(); + if (string.IsNullOrWhiteSpace(query)) + return songs; + + query = query.Trim(); + return songs + .Where(s => s.Title.Contains(query, StringComparison.OrdinalIgnoreCase) + || s.Artist.Contains(query, StringComparison.OrdinalIgnoreCase)) + .ToList(); + } + + /// Writes the editable metadata; file path, duration and source keep their stored values. + /// false if the song no longer exists. + public bool Update(long id, string title, string artist, string? license, string? attribution) + { + ArgumentException.ThrowIfNullOrWhiteSpace(title); + ArgumentException.ThrowIfNullOrWhiteSpace(artist); + + var song = repository.GetById(id); + if (song is null) + return false; + + song.Title = title.Trim(); + song.Artist = artist.Trim(); + song.License = string.IsNullOrWhiteSpace(license) ? null : license.Trim(); + song.Attribution = string.IsNullOrWhiteSpace(attribution) ? null : attribution.Trim(); + return repository.Update(song); + } + + /// false if the song no longer exists. + public bool Delete(long id) => repository.Delete(id); +} diff --git a/src/Radiosender/Views/MainWindow.xaml b/src/Radiosender/Views/MainWindow.xaml index 01281d6..6c43b01 100644 --- a/src/Radiosender/Views/MainWindow.xaml +++ b/src/Radiosender/Views/MainWindow.xaml @@ -22,6 +22,9 @@ + diff --git a/src/Radiosender/Views/Pages/LibraryPage.xaml b/src/Radiosender/Views/Pages/LibraryPage.xaml new file mode 100644 index 0000000..dcf4e56 --- /dev/null +++ b/src/Radiosender/Views/Pages/LibraryPage.xaml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Radiosender/Views/Pages/LibraryPage.xaml.cs b/src/Radiosender/Views/Pages/LibraryPage.xaml.cs new file mode 100644 index 0000000..43a7857 --- /dev/null +++ b/src/Radiosender/Views/Pages/LibraryPage.xaml.cs @@ -0,0 +1,63 @@ +using System.Windows; +using System.Windows.Controls; +using Radiosender.Controllers; +using Radiosender.Models; +using Wpf.Ui.Controls; +using MessageBox = Wpf.Ui.Controls.MessageBox; + +namespace Radiosender.Views.Pages; + +public partial class LibraryPage : Page +{ + private readonly LibraryController _controller = new(App.Songs); + + public LibraryPage() => InitializeComponent(); + + private Song? Selected => SongList.SelectedItem as Song; + + // Loaded fires on every navigation to the page, so the list is reloaded from the database each time. + private void OnLoaded(object sender, RoutedEventArgs e) => Refresh(); + + private void OnSearchChanged(object sender, TextChangedEventArgs e) => Refresh(); + + private void OnSelectionChanged(object sender, SelectionChangedEventArgs e) => + EditButton.IsEnabled = DeleteButton.IsEnabled = Selected is not null; + + private void Refresh() + { + var songs = _controller.Search(SearchBox.Text); + SongList.ItemsSource = songs; + EmptyText.Visibility = songs.Count == 0 ? Visibility.Visible : Visibility.Collapsed; + } + + private void OnEdit(object sender, RoutedEventArgs e) + { + if (Selected is not { } song) + return; + + var dialog = new SongEditWindow(song) { Owner = Window.GetWindow(this) }; + if (dialog.ShowDialog() == true) + _controller.Update(song.Id, dialog.SongTitle, dialog.Artist, dialog.License, dialog.Attribution); + Refresh(); + } + + private async void OnDelete(object sender, RoutedEventArgs e) + { + if (Selected is not { } song) + return; + + var result = await new MessageBox + { + Title = "Delete song", + Content = $"Delete \"{song.Title}\" by {song.Artist} from the library?", + PrimaryButtonText = "Delete", + CloseButtonText = "Cancel", + }.ShowDialogAsync(); + + if (result != Wpf.Ui.Controls.MessageBoxResult.Primary) + return; + + _controller.Delete(song.Id); + Refresh(); + } +} diff --git a/src/Radiosender/Views/SongEditWindow.xaml b/src/Radiosender/Views/SongEditWindow.xaml new file mode 100644 index 0000000..f0537ea --- /dev/null +++ b/src/Radiosender/Views/SongEditWindow.xaml @@ -0,0 +1,41 @@ + + + + + + + + diff --git a/src/Radiosender/Views/SongEditWindow.xaml.cs b/src/Radiosender/Views/SongEditWindow.xaml.cs new file mode 100644 index 0000000..c2d9c52 --- /dev/null +++ b/src/Radiosender/Views/SongEditWindow.xaml.cs @@ -0,0 +1,34 @@ +using System.Windows; +using Radiosender.Models; +using Wpf.Ui.Controls; + +namespace Radiosender.Views; + +/// Dialog for the editable song metadata. Only collects input; saving is done by the caller. +public partial class SongEditWindow : FluentWindow +{ + public SongEditWindow(Song song) + { + InitializeComponent(); + TitleBox.Text = song.Title; + ArtistBox.Text = song.Artist; + LicenseBox.Text = song.License; + AttributionBox.Text = song.Attribution; + } + + public string SongTitle => TitleBox.Text; + public string Artist => ArtistBox.Text; + public string License => LicenseBox.Text; + public string Attribution => AttributionBox.Text; + + private void OnSave(object sender, RoutedEventArgs e) + { + if (string.IsNullOrWhiteSpace(SongTitle) || string.IsNullOrWhiteSpace(Artist)) + { + ErrorText.Visibility = Visibility.Visible; + return; + } + + DialogResult = true; + } +} diff --git a/tests/Radiosender.Tests/LibraryControllerTests.cs b/tests/Radiosender.Tests/LibraryControllerTests.cs new file mode 100644 index 0000000..9420585 --- /dev/null +++ b/tests/Radiosender.Tests/LibraryControllerTests.cs @@ -0,0 +1,107 @@ +using Microsoft.Data.Sqlite; +using Radiosender.Controllers; +using Radiosender.Models; +using Radiosender.Services; + +namespace Radiosender.Tests; + +public sealed class LibraryControllerTests : IDisposable +{ + private readonly string _directory = Path.Combine(Path.GetTempPath(), "radiosender-tests", Guid.NewGuid().ToString("N")); + private readonly SongRepository _repository; + private readonly LibraryController _controller; + + public LibraryControllerTests() + { + _repository = new SongRepository(Path.Combine(_directory, "test.db")); + _controller = new LibraryController(_repository); + } + + public void Dispose() + { + SqliteConnection.ClearAllPools(); // pooled connections keep the file locked + Directory.Delete(_directory, recursive: true); + } + + private Song AddSong(string title, string artist) + { + var song = new Song + { + Title = title, + Artist = artist, + Duration = TimeSpan.FromSeconds(200), + FilePath = @"C:\music\song.mp3", + Source = SongSource.Upload, + License = "CC BY 4.0", + }; + _repository.Add(song); + return song; + } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + public void Search_WithoutQuery_ReturnsAllSongs(string? query) + { + AddSong("Alpha", "Band"); + AddSong("Beta", "Other"); + + Assert.Equal(2, _controller.Search(query).Count); + } + + [Fact] + public void Search_MatchesTitleOrArtistCaseInsensitive() + { + AddSong("Morning Light", "Band"); + AddSong("Evening", "The Lights"); + AddSong("Unrelated", "Nobody"); + + var titles = _controller.Search(" LIGHT ").Select(s => s.Title).ToList(); + + Assert.Equal(["Evening", "Morning Light"], titles.Order()); + } + + [Fact] + public void Update_WritesEditableFieldsAndKeepsTheRest() + { + var song = AddSong("Old", "Old Artist"); + + Assert.True(_controller.Update(song.Id, " New ", "New Artist", " ", "by someone")); + + var loaded = _repository.GetById(song.Id)!; + Assert.Equal("New", loaded.Title); + Assert.Equal("New Artist", loaded.Artist); + Assert.Null(loaded.License); + Assert.Equal("by someone", loaded.Attribution); + Assert.Equal(song.FilePath, loaded.FilePath); + Assert.Equal(song.Duration, loaded.Duration); + Assert.Equal(song.Source, loaded.Source); + } + + [Fact] + public void Update_RejectsBlankTitleOrArtist() + { + var song = AddSong("Title", "Artist"); + + Assert.ThrowsAny(() => _controller.Update(song.Id, " ", "Artist", null, null)); + Assert.ThrowsAny(() => _controller.Update(song.Id, "Title", "", null, null)); + Assert.Equal("Title", _repository.GetById(song.Id)!.Title); + } + + [Fact] + public void Update_ReturnsFalseForUnknownSong() + { + Assert.False(_controller.Update(42, "Title", "Artist", null, null)); + } + + [Fact] + public void Delete_RemovesSong() + { + var song = AddSong("Title", "Artist"); + + Assert.True(_controller.Delete(song.Id)); + Assert.Empty(_controller.Search(null)); + Assert.False(_controller.Delete(song.Id)); + } +}