Cleaned things for shuffle, but still has some deadlocks and crashes
This commit is contained in:
@ -6,8 +6,9 @@ using System.Windows.Threading;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using MpcNET.Commands.Queue;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace unison
|
||||
{
|
||||
@ -53,6 +54,8 @@ namespace unison
|
||||
_mpd.QueryStats();
|
||||
_settingsWin.UpdateStats();
|
||||
|
||||
_mpd.QueryPlaylist().ConfigureAwait(false);
|
||||
|
||||
Snapcast.IsEnabled = true;
|
||||
ConnectionOkIcon.Visibility = Visibility.Visible;
|
||||
ConnectionFailIcon.Visibility = Visibility.Collapsed;
|
||||
@ -121,11 +124,21 @@ namespace unison
|
||||
if (!_shuffleWin.GetContinuous())
|
||||
return;
|
||||
|
||||
NextTrack.IsEnabled = false;
|
||||
PreviousTrack.IsEnabled = false;
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
Debug.WriteLine("start continuous");
|
||||
|
||||
await _mpd.QueryPlaylist();
|
||||
|
||||
Debug.WriteLine("queried playlist");
|
||||
});
|
||||
|
||||
if (_mpd.GetPlaylistCount() > 5)
|
||||
return;
|
||||
|
||||
_mpd.CanPrevNext = false;
|
||||
await _shuffleWin.HandleContinuous();
|
||||
NextTrack.IsEnabled = true;
|
||||
PreviousTrack.IsEnabled = true;
|
||||
_mpd.CanPrevNext = true;
|
||||
Debug.WriteLine("finished continuous");
|
||||
}
|
||||
|
||||
@ -227,19 +240,8 @@ namespace unison
|
||||
}
|
||||
|
||||
public void Pause_Clicked(object sender, RoutedEventArgs e) => _mpd.PlayPause();
|
||||
|
||||
public void Previous_Clicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (PreviousTrack.IsEnabled)
|
||||
_mpd.Prev();
|
||||
}
|
||||
|
||||
public void Next_Clicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (NextTrack.IsEnabled)
|
||||
_mpd.Next();
|
||||
}
|
||||
|
||||
public void Previous_Clicked(object sender, RoutedEventArgs e) => _mpd.Prev();
|
||||
public void Next_Clicked(object sender, RoutedEventArgs e) => _mpd.Next();
|
||||
public void Random_Clicked(object sender, RoutedEventArgs e) => _mpd.Random();
|
||||
public void Repeat_Clicked(object sender, RoutedEventArgs e) => _mpd.Repeat();
|
||||
public void Single_Clicked(object sender, RoutedEventArgs e) => _mpd.Single();
|
||||
|
@ -68,6 +68,7 @@
|
||||
<StackPanel Orientation="Horizontal" Margin="0,5,0,5">
|
||||
<Button Content="Query filter" Click="UpdateFilter_Clicked" Padding="5, 2" VerticalAlignment="Bottom" HorizontalAlignment="Left" FocusVisualStyle="{x:Null}" Margin="0,0,10,0"/>
|
||||
<Button Content="Reset" Click="Reset_Clicked" Padding="5, 2" VerticalAlignment="Bottom" HorizontalAlignment="Left" FocusVisualStyle="{x:Null}"/>
|
||||
<TextBlock Text="Querying filter..." Margin="15,3,0,0" FontStyle="Italic" Visibility="Visible" />
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
@ -85,7 +86,7 @@
|
||||
<StackPanel Orientation="Vertical" Margin="5,5,5,0">
|
||||
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
|
||||
<TextBlock Text="Songs to add" Margin="0,0,5,5"/>
|
||||
<TextBox x:Name="SongNumber" PreviewTextInput="QueueValidationTextBox" MaxLength="4" Text="100" Width="35" HorizontalAlignment="Left" VerticalAlignment="Top"/>
|
||||
<TextBox x:Name="SongNumber" PreviewTextInput="QueueValidationTextBox" MaxLength="4" Text="15" Width="35" HorizontalAlignment="Left" VerticalAlignment="Top"/>
|
||||
</StackPanel>
|
||||
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
|
||||
<Button Content="Add to queue" Click="AddToQueue_Clicked" Padding="5, 2" HorizontalAlignment="Left" FocusVisualStyle="{x:Null}"/>
|
||||
|
@ -1,6 +1,4 @@
|
||||
using MpcNET;
|
||||
using MpcNET.Commands.Database;
|
||||
using MpcNET.Commands.Reflection;
|
||||
using MpcNET.Commands.Database;
|
||||
using MpcNET.Tags;
|
||||
using MpcNET.Types;
|
||||
using MpcNET.Types.Filters;
|
||||
@ -14,7 +12,6 @@ using System.Windows.Controls;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using System.Linq;
|
||||
using MpcNET.Commands.Queue;
|
||||
using System.Windows.Input;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
@ -23,8 +20,8 @@ namespace unison
|
||||
public partial class Shuffle : Window
|
||||
{
|
||||
private MPDHandler _mpd;
|
||||
private ShuffleHandler _shuffle;
|
||||
bool _continuous = false;
|
||||
List<string> _songList { get; }
|
||||
List<string> _genreList { get; }
|
||||
List<string> _folderList { get; }
|
||||
List<IFilter> _filters { get; }
|
||||
@ -32,13 +29,13 @@ namespace unison
|
||||
public Shuffle()
|
||||
{
|
||||
InitializeComponent();
|
||||
_songList = new();
|
||||
_genreList = new();
|
||||
_folderList = new();
|
||||
_filters = new();
|
||||
SongFilterNumber.Text = "0";
|
||||
|
||||
_mpd = (MPDHandler)Application.Current.Properties["mpd"];
|
||||
_shuffle = (ShuffleHandler)Application.Current.Properties["shuffle"];
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
@ -113,8 +110,6 @@ namespace unison
|
||||
return FindParent<T>(parent);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void AddFilter_Clicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
FilterPanel.Children.Add(new ContentPresenter { ContentTemplate = (DataTemplate)FindResource("FilterPanel") });
|
||||
@ -135,6 +130,7 @@ namespace unison
|
||||
FilterPanel.Children.RemoveRange(0, FilterPanel.Children.Count);
|
||||
FilterPanel.Children.Add(new ContentPresenter { ContentTemplate = (DataTemplate)FindResource("FilterPanel") });
|
||||
SongFilterNumber.Text = "0";
|
||||
_shuffle._songList.Clear();
|
||||
}
|
||||
|
||||
private ITag FilterEquivalence_Type(string value)
|
||||
@ -165,6 +161,13 @@ namespace unison
|
||||
|
||||
private async void UpdateFilter_Clicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
await UpdateFilter();
|
||||
}
|
||||
|
||||
private async Task UpdateFilter()
|
||||
{
|
||||
Debug.WriteLine("update filter => start");
|
||||
|
||||
_filters.Clear();
|
||||
|
||||
Debug.WriteLine("is on main thread => " + IsOnMainThread());
|
||||
@ -206,60 +209,13 @@ namespace unison
|
||||
else
|
||||
_filters.Add(new FilterBase(value, FilterOperator.None));
|
||||
|
||||
await GetSongsFromFilter();
|
||||
await _shuffle.GetSongsFromFilter(_filters);
|
||||
SongFilterPanel.Visibility = Visibility.Visible;
|
||||
SongFilterNumber.Text = _shuffle._songList.Count.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task GetSongsFromFilter()
|
||||
{
|
||||
Debug.WriteLine("get songs from filter => start");
|
||||
|
||||
Debug.WriteLine("is on main thread => " + IsOnMainThread());
|
||||
|
||||
_songList.Clear();
|
||||
SongFilterPanel.Visibility = Visibility.Visible;
|
||||
|
||||
int song = _mpd.GetStats().Songs;
|
||||
|
||||
Debug.WriteLine("get songs from filter => before command");
|
||||
|
||||
CommandList commandList = new CommandList(new IMpcCommand<object>[] { new SearchCommand(_filters, 0, song + 1) });
|
||||
string Response = await _mpd.SafelySendCommandAsync(commandList);
|
||||
|
||||
Debug.WriteLine("get songs from filter => after command");
|
||||
|
||||
// create a list of the file url
|
||||
string[] value = Response.Split(", [file, ");
|
||||
|
||||
// there are no song in this filter
|
||||
if (value[0] == "")
|
||||
{
|
||||
SongFilterNumber.Text = _songList.Count.ToString();
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.WriteLine("get songs from filter => before adding to list");
|
||||
|
||||
foreach (string file in value)
|
||||
{
|
||||
int start = 0;
|
||||
int end = file.IndexOf("],");
|
||||
string filePath = file.Substring(start, end - start);
|
||||
|
||||
_songList.Add(filePath);
|
||||
SongFilterNumber.Text = _songList.Count.ToString();
|
||||
}
|
||||
|
||||
Debug.WriteLine("get songs from filter => after adding to list");
|
||||
|
||||
// remove characters from first file
|
||||
_songList[0] = _songList[0].Substring(7, _songList[0].Length - 7);
|
||||
|
||||
SongFilterPanel.Visibility = Visibility.Visible;
|
||||
SongFilterNumber.Text = _songList.Count.ToString();
|
||||
|
||||
Debug.WriteLine("get songs from filter => finish");
|
||||
Debug.WriteLine("update filter => stop");
|
||||
}
|
||||
|
||||
private void FilterType_Change(object sender, string Operator, List<string> Listing)
|
||||
@ -340,14 +296,6 @@ namespace unison
|
||||
SongNumber.Text = "1000";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private async void AddToQueue_Clicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
QueueValidationNumber();
|
||||
@ -355,93 +303,37 @@ namespace unison
|
||||
if (_mpd.GetStats() == null)
|
||||
return;
|
||||
|
||||
NumberAddedSongs.Text = "0";
|
||||
SearchStatus.Visibility = Visibility.Visible;
|
||||
|
||||
// start dispatcher
|
||||
// write _shuffle.AddedSongs in dispatcher
|
||||
|
||||
await AddToQueue(int.Parse(SongNumber.Text));
|
||||
|
||||
Debug.WriteLine("add to queue finished");
|
||||
|
||||
SearchStatus.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
private async Task AddToQueue(int NumberToAdd)
|
||||
{
|
||||
await UpdateFilter();
|
||||
|
||||
Debug.WriteLine("check filters");
|
||||
|
||||
if (IsFilterEmpty())
|
||||
await AddToQueueRandom(int.Parse(SongNumber.Text)); // @TODO await or not???
|
||||
await _shuffle.AddToQueueRandom(NumberToAdd);
|
||||
else
|
||||
{
|
||||
UpdateFilter_Clicked(null, null);
|
||||
await AddToQueueFilter(int.Parse(SongNumber.Text)); // @TODO await or not???
|
||||
Debug.WriteLine("add to queue filter - before");
|
||||
await _shuffle.AddToQueueFilter(NumberToAdd);
|
||||
Debug.WriteLine("add to queue filter - after");
|
||||
}
|
||||
|
||||
Debug.WriteLine("add to queue finished");
|
||||
}
|
||||
|
||||
private async /*void*/ Task AddToQueueRandom(int SongNumber)
|
||||
{
|
||||
int AddedSongs = 0;
|
||||
NumberAddedSongs.Text = AddedSongs.ToString();
|
||||
SearchStatus.Visibility = Visibility.Visible;
|
||||
|
||||
Debug.WriteLine("song to add => " + SongNumber);
|
||||
for (int i = 0; i < SongNumber; i++)
|
||||
{
|
||||
// generate random number
|
||||
int song = new Random().Next(0, _mpd.GetStats().Songs - 1);
|
||||
|
||||
// query random song
|
||||
CommandList commandList = new CommandList(new IMpcCommand<object>[] { new SearchCommand(new FilterTag(MpdTags.Title, "", FilterOperator.Contains), song, song + 1) });
|
||||
string Response = await _mpd.SafelySendCommandAsync(commandList);
|
||||
|
||||
await Task.Delay(1);
|
||||
if (Response.Length > 0)
|
||||
{
|
||||
// parse song and add it to queue
|
||||
int start = Response.IndexOf("[file, ");
|
||||
int end = Response.IndexOf("],");
|
||||
string filePath = Response.Substring(start + 7, end - (start + 7));
|
||||
_mpd.AddSong(filePath);
|
||||
Debug.WriteLine("song path => " + filePath);
|
||||
|
||||
AddedSongs++;
|
||||
NumberAddedSongs.Text = AddedSongs.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO make play at first position added, as soon as possible
|
||||
if (!_mpd.IsPlaying())
|
||||
_mpd.Play(0);
|
||||
|
||||
SearchStatus.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
private async /*void*/ Task AddToQueueFilter(int SongNumber)
|
||||
{
|
||||
int AddedSongs = 0;
|
||||
NumberAddedSongs.Text = AddedSongs.ToString();
|
||||
SearchStatus.Visibility = Visibility.Visible;
|
||||
|
||||
Debug.WriteLine("song to add => " + SongNumber);
|
||||
|
||||
// more requested songs than available => add everything
|
||||
if (SongNumber > _songList.Count)
|
||||
{
|
||||
foreach (string path in _songList)
|
||||
{
|
||||
await Task.Delay(1);
|
||||
_mpd.AddSong(path);
|
||||
Debug.WriteLine("song path => " + path);
|
||||
|
||||
AddedSongs++;
|
||||
NumberAddedSongs.Text = AddedSongs.ToString();
|
||||
}
|
||||
}
|
||||
// more available songs than requested =>
|
||||
// we add unique indexes until we reach the requested amount
|
||||
else
|
||||
{
|
||||
HashSet<int> SongIndex = new();
|
||||
while (SongIndex.Count < SongNumber)
|
||||
{
|
||||
int MaxIndex = new Random().Next(0, _songList.Count - 1);
|
||||
SongIndex.Add(MaxIndex);
|
||||
}
|
||||
|
||||
foreach (int index in SongIndex)
|
||||
_mpd.AddSong(_songList[index]);
|
||||
}
|
||||
|
||||
SearchStatus.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
|
||||
public bool GetContinuous()
|
||||
{
|
||||
return _continuous;
|
||||
@ -449,45 +341,21 @@ namespace unison
|
||||
|
||||
public async Task HandleContinuous()
|
||||
{
|
||||
if (!GetContinuous())
|
||||
if (!_continuous)
|
||||
return;
|
||||
|
||||
Debug.WriteLine("is on main thread => " + IsOnMainThread());
|
||||
|
||||
IEnumerable<IMpdFile> Playlist = await _mpd.SafelySendCommandAsync(new PlaylistCommand());
|
||||
int queueSize = 0;
|
||||
foreach (IMpdFile file in Playlist)
|
||||
{
|
||||
Debug.WriteLine(file.Path);
|
||||
queueSize++;
|
||||
}
|
||||
Debug.WriteLine("queue size is: " + queueSize);
|
||||
|
||||
if (queueSize < 5)
|
||||
{
|
||||
if (_mpd.GetStats() == null)
|
||||
return;
|
||||
|
||||
if (IsFilterEmpty())
|
||||
await AddToQueueRandom(5); // @TODO await or not?
|
||||
else
|
||||
await AddToQueueFilter(5); // @TODO await or not?
|
||||
}
|
||||
await AddToQueue(5);
|
||||
}
|
||||
|
||||
private async void ContinuousShuffle_Checked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (ContinuousShuffle.IsChecked == true)
|
||||
{
|
||||
_continuous = true;
|
||||
_songList.Clear();
|
||||
if (!IsFilterEmpty())
|
||||
UpdateFilter_Clicked(null, null);
|
||||
}
|
||||
else
|
||||
_continuous = false;
|
||||
|
||||
await HandleContinuous();
|
||||
if (_mpd.GetPlaylistCount() < 5)
|
||||
await HandleContinuous();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user