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()); }
/// <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); }