implemented search artists in MusicBrainz
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
using HarmonyWinUI.Models;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Data;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
using Microsoft.UI.Xaml.Navigation;
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
// To learn more about WinUI, the WinUI project structure,
|
||||
// and more about our project templates, see: http://aka.ms/winui-project-info.
|
||||
|
||||
namespace HarmonyWinUI.Pages;
|
||||
|
||||
public sealed class ImageUrlToImageSourceConverter : IValueConverter
|
||||
{
|
||||
public object? Convert(object value, Type targetType, object parameter, string language)
|
||||
{
|
||||
if (value is string imageUrl
|
||||
&& Uri.TryCreate(imageUrl, UriKind.Absolute, out Uri? imageUri))
|
||||
{
|
||||
return new BitmapImage(imageUri);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, string language)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An empty page that can be used on its own or navigated to within a Frame.
|
||||
/// </summary>
|
||||
public sealed partial class Search : Page
|
||||
{
|
||||
public ObservableCollection<MusicBrainzArtist> Artists { get; } = new();
|
||||
private string? pendingSearchText;
|
||||
|
||||
public Search()
|
||||
{
|
||||
InitializeComponent();
|
||||
Loaded += Search_Loaded;
|
||||
}
|
||||
|
||||
protected override void OnNavigatedTo(NavigationEventArgs e)
|
||||
{
|
||||
base.OnNavigatedTo(e);
|
||||
pendingSearchText = e.Parameter as string;
|
||||
}
|
||||
|
||||
private async void Search_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (pendingSearchText is not string searchText)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
pendingSearchText = null;
|
||||
await SearchText(searchText);
|
||||
}
|
||||
|
||||
private async Task SearchText(string searchText)
|
||||
{
|
||||
TextBlockQuery.Text = $"Query: '{searchText}'";
|
||||
var mb = new MusicBrainz();
|
||||
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
var artists = await mb.getArtists(searchText);
|
||||
|
||||
this.Artists.Clear();
|
||||
foreach (var artist in artists)
|
||||
{
|
||||
this.Artists.Add(artist);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
catch (Exception exception) when (
|
||||
exception is HttpRequestException
|
||||
or IOException
|
||||
or JsonException
|
||||
or TaskCanceledException
|
||||
)
|
||||
{
|
||||
ContentDialog errorDialog = new ContentDialog
|
||||
{
|
||||
XamlRoot = XamlRoot,
|
||||
Title = "MusicBrainz connection failed",
|
||||
Content = $"{exception.GetType().Name}: {exception.Message}",
|
||||
PrimaryButtonText = "Retry",
|
||||
CloseButtonText = "Cancel",
|
||||
DefaultButton = ContentDialogButton.Primary
|
||||
};
|
||||
|
||||
ContentDialogResult result = await errorDialog.ShowAsync();
|
||||
if (result != ContentDialogResult.Primary)
|
||||
{
|
||||
this.Artists.Clear();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user