From 52b0a6fc85c6af0059dcc5c8de344630de11b9a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Marchal?= Date: Sun, 13 Nov 2022 14:01:07 +0100 Subject: [PATCH] Bugfix --- Handlers/ShuffleHandler.cs | 102 ++++++++------------- Views/MainWindow.xaml.cs | 4 + Views/Settings.xaml | 14 +-- Views/Shuffle.xaml | 8 +- Views/Shuffle.xaml.cs | 180 +++++++++++++++++++++---------------- 5 files changed, 155 insertions(+), 153 deletions(-) diff --git a/Handlers/ShuffleHandler.cs b/Handlers/ShuffleHandler.cs index 8302979..0ca439d 100644 --- a/Handlers/ShuffleHandler.cs +++ b/Handlers/ShuffleHandler.cs @@ -5,7 +5,9 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; +using MpcNET; using MpcNET.Commands.Database; +using MpcNET.Commands.Playback; using MpcNET.Commands.Queue; using MpcNET.Commands.Reflection; using MpcNET.Tags; @@ -16,49 +18,35 @@ namespace unison { class ShuffleHandler { - private MPDHandler _mpd; - public List _songList { get; } + private readonly MPDHandler _mpd; public int AddedSongs = 0; + public List SongList { get; } + public ShuffleHandler() { - _songList = new(); + SongList = new(); _mpd = (MPDHandler)Application.Current.Properties["mpd"]; } - /*private bool IsOnMainThread() - { - return Application.Current.Dispatcher.Thread == System.Threading.Thread.CurrentThread; - }*/ - public async Task GetSongsFromFilter(List filter, CancellationToken token) { - //Debug.WriteLine("[GetSongsFromFilterBefore] is on main thread => " + IsOnMainThread()); - //await Task.Run(async() => - //{ - //Debug.WriteLine("[GetSongsFromFilterAfter] is on main thread => " + IsOnMainThread()); - if (token.IsCancellationRequested) return; - _songList.Clear(); + SongList.Clear(); int song = _mpd.GetStats().Songs; Debug.WriteLine("before search command / song == " + song); - //var response = await Task.Run(async () => - //{ - /*return*/ IEnumerable response = await _mpd.SafelySendCommandAsync(new SearchCommand(filter, 0, song + 1)); - //}); + IEnumerable response = await _mpd.SafelySendCommandAsync(new SearchCommand(filter, 0, song + 1)); Debug.WriteLine("got response => " + response.Count()); foreach (IMpdFile file in response) { - _songList.Add(file.Path); - //Debug.WriteLine(file.Path); + SongList.Add(file.Path); } - //}); } public async Task AddToQueueRandom(int SongNumber, CancellationToken token) @@ -68,87 +56,75 @@ namespace unison if (token.IsCancellationRequested) return; - //await Task.Run(async () => - //{ int AddedSongs = 0; Debug.WriteLine("song to add => " + SongNumber); + + var commandList = new CommandList(); + int songTotal = _mpd.GetStats().Songs; + for (int i = 0; i < SongNumber; i++) { - // generate random number - int song = new Random().Next(0, _mpd.GetStats().Songs - 1); - Debug.WriteLine("song " + song + " - song total " + _mpd.GetStats().Songs); - IEnumerable Response = await _mpd.SafelySendCommandAsync(new SearchCommand(new FilterTag(MpdTags.Title, "", FilterOperator.Contains), song, song + 1)); + int song = new Random().Next(0, songTotal - 1); + commandList.Add(new SearchAddCommand(new FilterTag(MpdTags.Title, "", FilterOperator.Contains), song, song + 1)); + AddedSongs++; - Debug.WriteLine("got response"); - - await Task.Delay(1); - if (Response.Count() > 0) - { - string filePath = Response.First().Path; - _mpd.AddSong(filePath); - Debug.WriteLine("song path => " + filePath); - - if (i == 0) - { - if (!_mpd.IsPlaying()) - _mpd.Play(0); - } - - AddedSongs++; - } + // play if stopped or unknown state (no queue managing at the moment, so mandatory) + if (i == 0 && (_mpd.GetStatus().State != MpdState.Play && _mpd.GetStatus().State != MpdState.Pause)) + commandList.Add(new PlayCommand(0)); } - //}); - Debug.WriteLine("Add To Queue Random - finished"); + await _mpd.SafelySendCommandAsync(commandList); + + Debug.WriteLine("Add To Queue Random - finished with " + AddedSongs + " songs"); } public async Task AddToQueueFilter(int SongNumber, CancellationToken token) { - Debug.WriteLine("Add To Queue Filter"); + Debug.WriteLine("Add To Queue Filter - start"); if (token.IsCancellationRequested) return; - //await Task.Run(async () => - //{ int AddedSongs = 0; Debug.WriteLine("song to add => " + SongNumber); - // more requested songs than available => add everything - if (SongNumber > _songList.Count) + // more (or equal) requested songs than available => add everything + if (SongNumber >= SongList.Count) { - var commandList = new CommandList(); + Trace.WriteLine("more requested songs than available => add everything"); - foreach (string path in _songList) + var commandList = new CommandList(); + foreach (string path in SongList) { commandList.Add(new AddCommand(path)); AddedSongs++; } - Trace.WriteLine("Added " + AddedSongs + " songs"); + await _mpd.SafelySendCommandAsync(commandList); } // more available songs than requested => // we add unique indexes until we reach the requested amount else { + Trace.WriteLine("more available songs than requested"); + HashSet SongIndex = new(); - Debug.WriteLine("while - before"); - while (SongIndex.Count < SongNumber)//_songList.Count - 1)//SongNumber) + while (SongIndex.Count < SongNumber) { - int MaxIndex = new Random().Next(0, _songList.Count - 1); + int MaxIndex = new Random().Next(0, SongList.Count - 1); SongIndex.Add(MaxIndex); } - Debug.WriteLine("while - middle"); - var commandList = new CommandList(); + var commandList = new CommandList(); foreach (int index in SongIndex) - commandList.Add(new AddCommand(_songList[index])); + { + commandList.Add(new AddCommand(SongList[index])); + AddedSongs++; + } + await _mpd.SafelySendCommandAsync(commandList); - //_mpd.AddSong(_songList[index]); - Debug.WriteLine("while - after"); } - //}); Debug.WriteLine("Add To Queue Filter - finished"); } diff --git a/Views/MainWindow.xaml.cs b/Views/MainWindow.xaml.cs index a8ebc37..fc7e8ba 100644 --- a/Views/MainWindow.xaml.cs +++ b/Views/MainWindow.xaml.cs @@ -10,6 +10,7 @@ using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using unison.Handlers; +using System.Windows.Navigation; namespace unison { @@ -133,7 +134,10 @@ namespace unison Debug.WriteLine("playlist length => " + _mpd.GetStatus().PlaylistLength); if (_mpd.GetStatus().PlaylistLength > 4) + { + Debug.WriteLine("return :)"); return; + } Debug.WriteLine("start continuous handling"); _mpd.CanPrevNext = false; diff --git a/Views/Settings.xaml b/Views/Settings.xaml index db73fbb..d54af55 100644 --- a/Views/Settings.xaml +++ b/Views/Settings.xaml @@ -102,7 +102,7 @@ @@ -110,7 +110,7 @@ @@ -118,7 +118,7 @@ @@ -126,7 +126,7 @@ @@ -134,7 +134,7 @@ @@ -142,7 +142,7 @@ @@ -150,7 +150,7 @@ diff --git a/Views/Shuffle.xaml b/Views/Shuffle.xaml index af3a9bc..967592c 100644 --- a/Views/Shuffle.xaml +++ b/Views/Shuffle.xaml @@ -35,7 +35,7 @@ - +