Example #1
0
 protected void PreviousTrackClick()
 {
     if (PlaylistTracks.Count <= 1)
     {
         return;
     }
     if (PlaylistTracks.IndexOf(Data.CurrentTrack) == 0)
     {
         Data.CurrentTrack = PlaylistTracks[PlaylistTracks.Count - 1];
     }
     else
     {
         Data.CurrentTrack = PlaylistTracks[(PlaylistTracks.ToList().IndexOf(Data.CurrentTrack) - 1) % PlaylistTracks.Count];
     }
 }
Example #2
0
        /// <summary>
        /// Randomize the currently playlist, remove all of the tracks from the playlist, add all of the tracks back in a random order
        /// </summary>
        /// <returns></returns>
        public async Task Randify()
        {
            if (IsRandifying || IsPlaylistLoading)
            {
                return;
            }

            IsRandifying = true;

            var currentPlaylist     = CurrentPlaylist;
            var randomTracks        = PlaylistTracks.ToList();
            var listOfSkippedTracks = new List <PlaylistTrack>();

            var tracks = new List <Track>();

            // it looks overcomplicated and you're right, but the spotify endpoint has a limit of 100 songs
            try
            {
                for (int i = 0; i < randomTracks.Count; i++)
                {
                    if (!string.IsNullOrEmpty(randomTracks[i].Track.Id) && !string.IsNullOrWhiteSpace(randomTracks[i].Track.Id))
                    {
                        tracks.Add(randomTracks[i].Track);
                    }
                    else
                    {
                        listOfSkippedTracks.Add(randomTracks[i]);
                    }

                    if (i % 100 == 0)
                    {
                        await SpotifyService.RemoveTracksFromPlaylist(AuthenticationService.User, AuthenticationService.AuthenticationToken, currentPlaylist, tracks);

                        tracks.Clear();
                    }
                }

                await SpotifyService.RemoveTracksFromPlaylist(AuthenticationService.User, AuthenticationService.AuthenticationToken, currentPlaylist, tracks);
            }
            catch (Exception ex)
            {
                PageException = ex;
            }

            tracks.Clear();

            randomTracks = randomTracks
                           .OrderBy(o => Guid.NewGuid())
                           .ToList();

            // it looks overcomplicated and you're right, but the spotify endpoint has a limit of 100 songs
            try
            {
                for (int i = 0; i < randomTracks.Count; i++)
                {
                    if (!string.IsNullOrEmpty(randomTracks[i].Track.Id) && !string.IsNullOrWhiteSpace(randomTracks[i].Track.Id))
                    {
                        tracks.Add(randomTracks[i].Track);
                    }

                    if (i % 100 == 0)
                    {
                        await SpotifyService.AddTracksToPlaylist(AuthenticationService.User, AuthenticationService.AuthenticationToken, currentPlaylist, tracks);

                        tracks.Clear();
                    }
                }

                await SpotifyService.AddTracksToPlaylist(AuthenticationService.User, AuthenticationService.AuthenticationToken, currentPlaylist, tracks);
            }
            catch (Exception ex)
            {
                PageException = ex;
            }

            foreach (var skippedTrack in listOfSkippedTracks)
            {
                randomTracks.Remove(skippedTrack);
            }

            listOfSkippedTracks.Reverse();

            foreach (var skippedTrack in listOfSkippedTracks)
            {
                randomTracks.Insert(0, skippedTrack);
            }

            IsRandifying = false;

            await BindPlaylistAfterSort(randomTracks);
        }