Fix radio architecture
This commit is contained in:
parent
aeb1b1d573
commit
2f80af70ba
82
Handlers/RadioHandler.cs
Normal file
82
Handlers/RadioHandler.cs
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using RadioBrowser;
|
||||||
|
using RadioBrowser.Models;
|
||||||
|
|
||||||
|
namespace unison.Handlers
|
||||||
|
{
|
||||||
|
public class CountryListItem
|
||||||
|
{
|
||||||
|
public uint Count { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
if (Name == "")
|
||||||
|
return "None";
|
||||||
|
return $"{Name} ({Count})";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StationListItem
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Codec { get; set; }
|
||||||
|
public string Tags { get; set; }
|
||||||
|
public int Bitrate { get; set; }
|
||||||
|
public Uri Url { get; set; }
|
||||||
|
|
||||||
|
private string _country;
|
||||||
|
public string Country
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_country.Length == 0)
|
||||||
|
return "🏴☠️";
|
||||||
|
return string.Concat(_country.ToUpper().Select(x => char.ConvertFromUtf32(x + 0x1F1A5))); // return emoji
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_country = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class RadioHandler
|
||||||
|
{
|
||||||
|
private readonly RadioBrowserClient _radioBrowser;
|
||||||
|
private readonly bool _connected = true;
|
||||||
|
|
||||||
|
public bool IsConnected() => _connected;
|
||||||
|
|
||||||
|
|
||||||
|
public RadioHandler()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_radioBrowser = new RadioBrowserClient();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Trace.WriteLine("Exception while connecting to RadioBrowser: " + e.Message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_connected = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<NameAndCount>> GetCountries()
|
||||||
|
{
|
||||||
|
return await _radioBrowser.Lists.GetCountriesAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<StationInfo>> AdvancedSearch(AdvancedSearchOptions options)
|
||||||
|
{
|
||||||
|
|
||||||
|
return await _radioBrowser.Search.AdvancedAsync(options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,7 @@ using System.Windows.Threading;
|
|||||||
using System.Windows.Interop;
|
using System.Windows.Interop;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Windows.Controls.Primitives;
|
using System.Windows.Controls.Primitives;
|
||||||
|
using unison.Handlers;
|
||||||
|
|
||||||
namespace unison
|
namespace unison
|
||||||
{
|
{
|
||||||
|
@ -37,7 +37,7 @@
|
|||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
|
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
|
||||||
<Button Content="{x:Static properties:Resources.Radio_Search}" Click="Search_Clicked" Padding="5, 2"/>
|
<Button x:Name="SearchButton" Content="{x:Static properties:Resources.Radio_Search}" Click="Search_Clicked" Padding="5, 2"/>
|
||||||
<Button Content="{x:Static properties:Resources.Radio_Reset}" Click="Reset_Clicked" Margin="10,0,0,0" Padding="5, 2"/>
|
<Button Content="{x:Static properties:Resources.Radio_Reset}" Click="Reset_Clicked" Margin="10,0,0,0" Padding="5, 2"/>
|
||||||
<TextBlock x:Name="SearchStatus" Margin="15,1,0,0" FontStyle="Italic" />
|
<TextBlock x:Name="SearchStatus" Margin="15,1,0,0" FontStyle="Italic" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
using RadioBrowser;
|
using System.Diagnostics;
|
||||||
using RadioBrowser.Models;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System;
|
using System;
|
||||||
@ -10,79 +7,36 @@ using System.Windows.Interop;
|
|||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using unison.Handlers;
|
||||||
|
using RadioBrowser.Models;
|
||||||
|
|
||||||
namespace unison
|
namespace unison
|
||||||
{
|
{
|
||||||
public class CountryListItem
|
|
||||||
{
|
|
||||||
public uint Count { get; set; }
|
|
||||||
public string Name { get; set; }
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
if (Name == "")
|
|
||||||
return "None";
|
|
||||||
return $"{Name} ({Count})";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class StationListItem
|
|
||||||
{
|
|
||||||
public string Name { get; set; }
|
|
||||||
public string Codec { get; set; }
|
|
||||||
public string Tags { get; set; }
|
|
||||||
public int Bitrate { get; set; }
|
|
||||||
public Uri Url { get; set; }
|
|
||||||
|
|
||||||
private string _country;
|
|
||||||
public string Country
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
if (_country.Length == 0)
|
|
||||||
return "🏴☠️";
|
|
||||||
return string.Concat(_country.ToUpper().Select(x => char.ConvertFromUtf32(x + 0x1F1A5))); // return emoji
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_country = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class Radios : Window
|
public partial class Radios : Window
|
||||||
{
|
{
|
||||||
private RadioBrowserClient _radioBrowser;
|
|
||||||
private MPDHandler _mpd;
|
private MPDHandler _mpd;
|
||||||
private bool _connected = true;
|
RadioHandler _radio;
|
||||||
|
|
||||||
public bool IsConnected() => _connected;
|
public bool IsConnected() => _radio.IsConnected();
|
||||||
|
|
||||||
public Radios()
|
public Radios()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
Initialize();
|
||||||
try
|
|
||||||
{
|
|
||||||
_radioBrowser = new RadioBrowserClient();
|
|
||||||
Initialize();
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Debug.WriteLine("Exception while connecting to RadioBrowser: " + e.Message);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_connected = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async void Initialize()
|
public async void Initialize()
|
||||||
{
|
{
|
||||||
|
_radio = new RadioHandler();
|
||||||
|
|
||||||
|
if (!_radio.IsConnected())
|
||||||
|
SearchButton.IsEnabled = false;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
List<NameAndCount> Countries = await _radioBrowser.Lists.GetCountriesAsync();
|
|
||||||
CountryList.Items.Add(new CountryListItem { Name = "", Count = 0 });
|
CountryList.Items.Add(new CountryListItem { Name = "", Count = 0 });
|
||||||
|
|
||||||
|
List<NameAndCount> Countries = await _radio.GetCountries();
|
||||||
foreach (NameAndCount Country in Countries)
|
foreach (NameAndCount Country in Countries)
|
||||||
{
|
{
|
||||||
CountryList.Items.Add(new CountryListItem
|
CountryList.Items.Add(new CountryListItem
|
||||||
@ -94,27 +48,49 @@ namespace unison
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("Exception while getting countries in RadioBrowser: " + e.Message);
|
Trace.WriteLine("Exception while getting countries in RadioBrowser: " + e.Message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private string CleanString(string str)
|
private static string CleanString(string str)
|
||||||
{
|
{
|
||||||
return str.Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
|
return str.Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void SearchHandler(object sender, KeyEventArgs e)
|
||||||
|
{
|
||||||
|
if (e.Key == Key.Return)
|
||||||
|
Search_Clicked(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void Search_Clicked(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
CountryListItem a = (CountryListItem)CountryList.SelectedItem;
|
||||||
|
await SearchAdvanced(NameSearch.Text, a?.Name, TagSearch.Text);
|
||||||
|
}
|
||||||
|
catch (Exception except)
|
||||||
|
{
|
||||||
|
Trace.WriteLine("Error on RadioBrowser search: " + except.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async Task SearchAdvanced(string name, string country, string tags)
|
public async Task SearchAdvanced(string name, string country, string tags)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
SearchStatus.Text = unison.Resources.Resources.Radio_Loading;
|
SearchStatus.Text = unison.Resources.Resources.Radio_Loading;
|
||||||
|
|
||||||
List<StationInfo> advancedSearch = await _radioBrowser.Search.AdvancedAsync(new AdvancedSearchOptions
|
List<StationInfo> advancedSearch = await Task.Run(async () =>
|
||||||
{
|
{
|
||||||
Name = name,
|
return await _radio.AdvancedSearch(new AdvancedSearchOptions
|
||||||
Country = country,
|
{
|
||||||
TagList = tags
|
Name = name,
|
||||||
|
Country = country,
|
||||||
|
TagList = tags
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
RadioListGrid.Items.Clear();
|
RadioListGrid.Items.Clear();
|
||||||
@ -140,7 +116,7 @@ namespace unison
|
|||||||
}
|
}
|
||||||
catch (Exception except)
|
catch (Exception except)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("Error on RadioBrowser search advanced: " + except.Message);
|
Trace.WriteLine("Error on RadioBrowser search advanced: " + except.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,13 +136,13 @@ namespace unison
|
|||||||
}
|
}
|
||||||
catch (ArgumentOutOfRangeException)
|
catch (ArgumentOutOfRangeException)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("Error: Invalid index.");
|
Trace.WriteLine("Error: Invalid index.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (station.Url == null)
|
if (station.Url == null)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("Error: Invalid station.");
|
Trace.WriteLine("Error: Invalid station.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,19 +150,6 @@ namespace unison
|
|||||||
_mpd.ClearAddAndPlay(station.Url.AbsoluteUri);
|
_mpd.ClearAddAndPlay(station.Url.AbsoluteUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void Search_Clicked(object sender, RoutedEventArgs e)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
CountryListItem a = (CountryListItem)CountryList.SelectedItem;
|
|
||||||
await SearchAdvanced(NameSearch.Text, a?.Name, TagSearch.Text);
|
|
||||||
}
|
|
||||||
catch (Exception except)
|
|
||||||
{
|
|
||||||
Debug.WriteLine("Error on RadioBrowser search: " + except.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Reset_Clicked(object sender, RoutedEventArgs e)
|
private void Reset_Clicked(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
NameSearch.Text = "";
|
NameSearch.Text = "";
|
||||||
@ -194,12 +157,6 @@ namespace unison
|
|||||||
CountryList.SelectedIndex = 0;
|
CountryList.SelectedIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SearchHandler(object sender, KeyEventArgs e)
|
|
||||||
{
|
|
||||||
if (e.Key == Key.Return)
|
|
||||||
Search_Clicked(null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Window_Closing(object sender, CancelEventArgs e)
|
private void Window_Closing(object sender, CancelEventArgs e)
|
||||||
{
|
{
|
||||||
e.Cancel = true;
|
e.Cancel = true;
|
||||||
|
Loading…
Reference in New Issue
Block a user