public async Task AddRandomSongAsync() { var song = await SelectRandomSongAsync().ConfigureAwait(false); // if for some reason could not find new song to play // then just add currently playing track to upcoming songs // (the same logic is implemented in synchronous version as well) UpcomingSongs.Add(song ?? CurrentSong); }
public async Task MoveToNextSongAsync() { if (SongArchive.Count >= MaxSongs) { SongArchive.RemoveAt(0); } SongArchive.Add(CurrentSong); CurrentSong = UpcomingSongs.First(); UpcomingSongs.RemoveAt(0); await AddRandomSongAsync().ConfigureAwait(false); }
public async Task RemoveSongAsync(int songId) { for (var i = 0; i < MaxSongs; i++) { if (UpcomingSongs[i].Id != songId) { continue; } await AddRandomSongAsync().ConfigureAwait(false); UpcomingSongs.RemoveAt(i); return; } }
/// <summary> /// Method removes song by its ID from the list of upcoming songs /// and adds new randonly selected song to the list (to keep its size constant) /// </summary> public void RemoveSong(int songId) { for (var i = 0; i < MaxSongs; i++) { if (UpcomingSongs[i].Id != songId) { continue; } UpcomingSongs.RemoveAt(i); UpcomingSongs.Add(SelectRandomSong()); return; } }
public void MoveToPrevSong() { if (!SongArchive.Any()) { return; } // shrink list of upcoming songs UpcomingSongs.Insert(0, CurrentSong); UpcomingSongs.Remove(UpcomingSongs.Last()); // reassign current song (take last song from archive) CurrentSong = SongArchive.Last(); // update archive SongArchive.Remove(SongArchive.Last()); }
public void MoveToNextSong() { // update archive if (SongArchive.Count >= MaxSongs) { SongArchive.RemoveAt(0); } SongArchive.Add(CurrentSong); // reassign current song (take first item from list of upcoming songs) CurrentSong = UpcomingSongs.First(); // update the list of upcoming songs UpcomingSongs.RemoveAt(0); AddRandomSong(); }
public void MoveUpcomingSong(int from, int to) { if (from == to) { return; } if (from < 0 || to < 0 || from >= UpcomingSongs.Count || to >= UpcomingSongs.Count) { return; } var song = UpcomingSongs[from]; UpcomingSongs.Remove(song); UpcomingSongs.Insert(to, song); }
/// <summary> /// Switching to previous song is done synchronously /// since his operation is very cheap (just recombinate songs in collections) /// </summary> public Task MoveToPrevSongAsync() { if (SongArchive.Any()) { // shrink list of upcoming songs UpcomingSongs.Insert(0, CurrentSong); UpcomingSongs.Remove(UpcomingSongs.Last()); // reassign current song (take last song from archive) CurrentSong = SongArchive.Last(); // update archive SongArchive.Remove(SongArchive.Last()); } return(Task.CompletedTask); }
/// <summary> /// Same as synchronous version but with async calls to EF /// </summary> private async Task <Song> SelectRandomSongAsync(int maxAttempts = 15) { var attempts = 0; Song song; // keep selecting song randomly until the song file is actually present in the file system... // ...and while it isn't present in archive of recently played songs and upcoming songs do { if (attempts++ == maxAttempts) { return(_songSelector.DefaultSong()); } song = await _songSelector.SelectSongAsync().ConfigureAwait(false); }while (SongArchive.Any(s => s.Id == song.Id) || // true, if the archive already contains this song UpcomingSongs.Any(s => s.Id == song.Id) || // true, if it is already in songlist song.Id == CurrentSong.Id || // true, if it's currently playing FileLocator.FindSongPath(song) == ""); // true, if the file with this song doesn't exist return(song); }
public void AddRandomSong() => UpcomingSongs.Add(SelectRandomSong() ?? CurrentSong);