Progress on radio implementation

This commit is contained in:
2021-10-03 13:54:54 +02:00
parent fbb65a039a
commit 32d3610b07
4 changed files with 215 additions and 58 deletions

View File

@ -1,6 +1,5 @@
using RadioBrowser;
using RadioBrowser.Models;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
@ -9,86 +8,179 @@ using System;
using System.ComponentModel;
using System.Windows.Interop;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Collections.Generic;
namespace unison
{
public class StationView : StationInfo
public class CountryListItem
{
public StationView(string _name, Uri _url, int _bitrate, string _codec, string _country, Uri _favicon)
{
Name = _name;
Url = _url;
Bitrate = _bitrate;
Codec = _codec;
CountryCode = _country;
Favicon = _favicon;
}
public uint Count { get; set; }
public string Name { get; set; }
public override string ToString()
{
return $"{this.Name} - {this.Bitrate} - {this.Codec} - {this.CountryCode}";
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 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;
}
}
private string _bitrate;
public string Bitrate
{
get
{
if (_bitrate == "0")
return "—";
return _bitrate.ToString();
}
set
{
_bitrate = value;
}
}
}
public partial class Radios : Window
{
RadioBrowserClient _radioBrowser;
List<StationView> _stations = new List<StationView>();
private RadioBrowserClient _radioBrowser;
private MPDHandler _mpd;
public Radios()
{
InitializeComponent();
_radioBrowser = new RadioBrowserClient();
Initialize();
}
public async Task Search(string name)
public async void Initialize()
{
var searchByName = await _radioBrowser.Search.ByNameAsync(name);
Debug.WriteLine(searchByName.FirstOrDefault()?.Name);
Debug.WriteLine("");
}
public async Task SearchAdvanced(string name)
{
Debug.Write(name);
_stations.Clear();
var advancedSearch = await _radioBrowser.Search.AdvancedAsync(new AdvancedSearchOptions
List<NameAndCount> Countries = await _radioBrowser.Lists.GetCountriesAsync();
CountryList.Items.Add(new CountryListItem { Name = "", Count = 0 });
foreach (NameAndCount Country in Countries)
{
Name = name
});
foreach (var station in advancedSearch)
_stations.Add(new StationView(station.Name, station.Url, station.Bitrate, station.Codec, station.CountryCode, station.Favicon));
lvDataBinding.ItemsSource = _stations;
ICollectionView view = CollectionViewSource.GetDefaultView(_stations);
view.Refresh();
Debug.WriteLine(_stations[0].Url.AbsoluteUri);
CountryList.Items.Add(new CountryListItem
{
Name = Country.Name,
Count = Country.Stationcount
});
}
}
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
private string CleanString(string str)
{
Debug.WriteLine("Selected: {0}", e.AddedItems[0]);
return str.Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
}
public async Task SearchAdvanced(string name, string country, string tags)
{
SearchStatus.Text = "Loading stations...";
List<StationInfo> advancedSearch = await _radioBrowser.Search.AdvancedAsync(new AdvancedSearchOptions
{
Name = name,
Country = country,
TagList = tags
});
RadioListGrid.Items.Clear();
if (advancedSearch.Count > 0)
{
SearchStatus.Text = "";
foreach (StationInfo station in advancedSearch)
{
RadioListGrid.Items.Add(new StationListItem
{
Name = CleanString(station.Name),
Country = station.CountryCode,
Codec = station.Codec,
Bitrate = station.Bitrate.ToString(),
Url = station.Url,
Tags = string.Join(", ", station.Tags)
});
}
FitToContent();
}
else
SearchStatus.Text = "No stations found!";
}
private void FitToContent()
{
foreach (DataGridColumn column in RadioListGrid.Columns)
column.Width = new DataGridLength(1.0, DataGridLengthUnitType.SizeToCells);
}
private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
DataGrid grid = sender as DataGrid;
StationListItem station;
try
{
station = grid.Items[grid.SelectedIndex] as StationListItem;
}
catch (System.ArgumentOutOfRangeException)
{
Debug.WriteLine("Error: Invalid index.");
return;
}
if (station.Url == null)
{
Debug.WriteLine("Error: Invalid station.");
return;
}
_mpd = (MPDHandler)Application.Current.Properties["mpd"];
_mpd.ClearQueue();
StationView station = (StationView)e.AddedItems[0];
_mpd.AddSong(station.Url.AbsoluteUri);
_mpd.PlayCommand();
}
public List<StationView> GetStations()
{
return _stations;
}
private async void Search_Clicked(object sender, RoutedEventArgs e)
{
await SearchAdvanced(SearchBar.Text);
CountryListItem a = (CountryListItem)CountryList.SelectedItem;
await SearchAdvanced(NameSearch.Text, a?.Name, TagSearch.Text);
}
private void Reset_Clicked(object sender, RoutedEventArgs e)
{
NameSearch.Text = "";
TagSearch.Text = "";
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)
@ -104,4 +196,4 @@ namespace unison
helper.EnsureHandle();
}
}
}
}