refactor(import): put online sources behind IMusicProvider
A provider only has to search and return MusicTrack items. Downloading and storing them is shared by all providers in TrackDownloader and OnlineImportController. OpenverseProvider is the first implementation. Song.OpenverseId becomes ExternalId and SongSource.Openverse becomes Online; the database column and the stored values stay the same. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
c6e9bf4e82
commit
a8ccb9b73d
14 files changed
+371
-260
No files matched your search
@@ -60,8 +60,8 @@
|
||||
</Grid>
|
||||
</TabItem>
|
||||
|
||||
<!-- Openverse -->
|
||||
<TabItem Header="Openverse">
|
||||
<!-- Online providers; the header is the provider name when there is only one -->
|
||||
<TabItem x:Name="OnlineTab" Header="Online">
|
||||
<Grid Margin="0,12,0,0">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
@@ -72,15 +72,24 @@
|
||||
|
||||
<Grid Grid.Row="0">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<!-- Only shown when more than one provider is registered -->
|
||||
<ComboBox x:Name="ProviderBox"
|
||||
MinWidth="140"
|
||||
Margin="0,0,8,0"
|
||||
AutomationProperties.Name="Musikquelle"
|
||||
DisplayMemberPath="Name"
|
||||
Visibility="Collapsed" />
|
||||
<ui:TextBox x:Name="QueryBox"
|
||||
Grid.Column="1"
|
||||
PlaceholderText="Freie Musik suchen, zum Beispiel "jazz piano""
|
||||
AutomationProperties.Name="Suchbegriff"
|
||||
KeyDown="OnQueryKeyDown" />
|
||||
<ui:Button x:Name="SearchButton"
|
||||
Grid.Column="1"
|
||||
Grid.Column="2"
|
||||
Margin="8,0,0,0"
|
||||
Appearance="Primary"
|
||||
Content="Suchen"
|
||||
@@ -102,7 +111,7 @@
|
||||
<ui:GridViewColumn Width="300" Header="Titel" DisplayMemberBinding="{Binding Track.Title}" />
|
||||
<ui:GridViewColumn Width="180" Header="Urheber" DisplayMemberBinding="{Binding Track.Creator}" />
|
||||
<ui:GridViewColumn Width="80" Header="Dauer" DisplayMemberBinding="{Binding Track.DurationText}" />
|
||||
<ui:GridViewColumn Width="130" Header="Lizenz" DisplayMemberBinding="{Binding Track.LicenseName}" />
|
||||
<ui:GridViewColumn Width="130" Header="Lizenz" DisplayMemberBinding="{Binding Track.License}" />
|
||||
</ui:GridView>
|
||||
</ui:ListView.View>
|
||||
</ui:ListView>
|
||||
|
||||
@@ -12,15 +12,11 @@ using Wpf.Ui.Controls;
|
||||
|
||||
namespace Radiosender.Views.Pages;
|
||||
|
||||
/// <summary>Adds music to the library from local files or from Openverse; both list their results in one place.</summary>
|
||||
/// <summary>Adds music to the library from local files or from an online provider; both list their results in one place.</summary>
|
||||
public partial class ImportPage : Page
|
||||
{
|
||||
// One Openverse controller (and HttpClient) for the app lifetime; created on first use, after App.Songs exists.
|
||||
private static OpenverseController? _openverse;
|
||||
private static OpenverseController Openverse => _openverse ??= new(OpenverseClient.CreateDefault(), App.Songs);
|
||||
|
||||
private readonly UploadController _upload =
|
||||
new(new SongImportService(App.Songs, SongImportService.DefaultMusicDirectory));
|
||||
private readonly OnlineImportController _online;
|
||||
private readonly UploadController _upload;
|
||||
|
||||
private readonly ObservableCollection<SessionRow> _session = [];
|
||||
// Preview has its own output, so it never interrupts a running broadcast.
|
||||
@@ -31,9 +27,19 @@ public partial class ImportPage : Page
|
||||
private bool _busy;
|
||||
private bool _previewing;
|
||||
|
||||
public ImportPage()
|
||||
public ImportPage(OnlineImportController online, UploadController upload)
|
||||
{
|
||||
_online = online;
|
||||
_upload = upload;
|
||||
InitializeComponent();
|
||||
// One provider needs no choice: the tab carries its name. Several get a list next to the search box.
|
||||
var providers = _online.Providers;
|
||||
ProviderBox.ItemsSource = providers;
|
||||
ProviderBox.SelectedIndex = providers.Count > 0 ? 0 : -1;
|
||||
ProviderBox.Visibility = providers.Count > 1 ? Visibility.Visible : Visibility.Collapsed;
|
||||
OnlineTab.Visibility = providers.Count > 0 ? Visibility.Visible : Visibility.Collapsed;
|
||||
if (providers.Count == 1)
|
||||
OnlineTab.Header = providers[0].Name;
|
||||
Session.ItemsSource = _session;
|
||||
_session.CollectionChanged += (_, _) =>
|
||||
{
|
||||
@@ -98,7 +104,7 @@ public partial class ImportPage : Page
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Openverse ----
|
||||
// ---- Online providers ----
|
||||
|
||||
private ResultRow? Selected => Results.SelectedItem as ResultRow;
|
||||
|
||||
@@ -111,21 +117,21 @@ public partial class ImportPage : Page
|
||||
private async void OnSearch(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var query = QueryBox.Text.Trim();
|
||||
if (query.Length == 0 || _busy)
|
||||
if (query.Length == 0 || _busy || ProviderBox.SelectedItem is not IMusicProvider provider)
|
||||
return;
|
||||
|
||||
SetBusy($"Suche auf Openverse nach \"{query}\"...");
|
||||
SetBusy($"Suche auf {provider.Name} nach \"{query}\"...");
|
||||
Message.IsOpen = false;
|
||||
try
|
||||
{
|
||||
var tracks = await Openverse.SearchAsync(query);
|
||||
var imported = Openverse.ImportedIds();
|
||||
var tracks = await provider.SearchAsync(query);
|
||||
var imported = _online.ImportedIds();
|
||||
Results.ItemsSource = tracks.Select(t => ResultRow.For(t, imported)).ToList();
|
||||
GridColumns.Fit(Results);
|
||||
if (tracks.Count == 0)
|
||||
Show(InfoBarSeverity.Informational, "Keine Ergebnisse", $"Openverse hat nichts zu \"{query}\" gefunden.");
|
||||
Show(InfoBarSeverity.Informational, "Keine Ergebnisse", $"{provider.Name} hat nichts zu \"{query}\" gefunden.");
|
||||
}
|
||||
catch (OpenverseException ex)
|
||||
catch (MusicProviderException ex)
|
||||
{
|
||||
Show(InfoBarSeverity.Error, "Suche fehlgeschlagen", ex.Message);
|
||||
}
|
||||
@@ -208,7 +214,7 @@ public partial class ImportPage : Page
|
||||
// Everything selected that can still be imported.
|
||||
private List<ResultRow> Importable => Results.SelectedItems.OfType<ResultRow>().Where(r => r.CanImport).ToList();
|
||||
|
||||
// One after the other: Openverse limits anonymous requests, and each result shows up as soon as it is done.
|
||||
// One after the other: providers limit anonymous requests, and each result shows up as soon as it is done.
|
||||
private async void OnImport(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var rows = Importable;
|
||||
@@ -228,14 +234,14 @@ public partial class ImportPage : Page
|
||||
: $"Lade {i + 1} von {rows.Count} herunter: \"{row.Track.Title}\"...");
|
||||
try
|
||||
{
|
||||
var song = await Openverse.ImportAsync(row.Track);
|
||||
var song = await _online.ImportAsync(row.Track);
|
||||
_session.Insert(0, SessionRow.Added(song, song.License ?? ""));
|
||||
ReplaceRow(row, row with { Status = "In Bibliothek", CanImport = false });
|
||||
}
|
||||
catch (Exception ex) when (ex is OpenverseException or Microsoft.EntityFrameworkCore.DbUpdateException)
|
||||
catch (Exception ex) when (ex is MusicProviderException or Microsoft.EntityFrameworkCore.DbUpdateException)
|
||||
{
|
||||
// The database error text is technical and English; the usual cause is a song that is already stored.
|
||||
var reason = ex is OpenverseException ? ex.Message : "Der Song konnte nicht in der Bibliothek gespeichert werden.";
|
||||
var reason = ex is MusicProviderException ? ex.Message : "Der Song konnte nicht in der Bibliothek gespeichert werden.";
|
||||
_session.Insert(0, SessionRow.Failed(row.Track.Title, reason));
|
||||
failed++;
|
||||
lastError = reason;
|
||||
@@ -296,9 +302,9 @@ public partial class ImportPage : Page
|
||||
|
||||
private void OnUnloaded(object sender, RoutedEventArgs e) => OnStop(sender, e);
|
||||
|
||||
private sealed record ResultRow(OpenverseTrack Track, string Status, bool CanImport)
|
||||
private sealed record ResultRow(MusicTrack Track, string Status, bool CanImport)
|
||||
{
|
||||
public static ResultRow For(OpenverseTrack track, IReadOnlySet<string> imported) =>
|
||||
public static ResultRow For(MusicTrack track, IReadOnlySet<string> imported) =>
|
||||
imported.Contains(track.Id) ? new(track, "In Bibliothek", false)
|
||||
: !track.CanImport ? new(track, "Keine Dauer", false)
|
||||
: new(track, "", true);
|
||||
|
||||
Reference in new issue
Block a user