Exemplo n.º 1
0
 public Episode(FullEpisode episode)
 {
     Artist  = episode.Show.Publisher;
     Title   = episode.Name;
     URI     = episode.Uri;
     Message = $"{Title} by {Artist} || {URI}";
 }
Exemplo n.º 2
0
 public async Task <bool> ToggleFavorite(IPlayableItem item)
 {
     return(item switch
     {
         FullTrack track => await ToggleFavorite(track),
         FullEpisode episode => await ToggleFavorite(episode),
         _ => false
     });
Exemplo n.º 3
0
 private static string GetUri(IPlayableItem item)
 {
     return(item switch
     {
         FullTrack t => t.Uri,
         FullEpisode e => e.Uri,
         _ => throw new ArgumentException("item has no valid Uri")
     });
Exemplo n.º 4
0
 public static string GetId(this IPlayableItem item)
 {
     return(item switch
     {
         FullEpisode e => e.Id,
         FullTrack t => t.Id,
         _ => throw new ArgumentException()
     });
Exemplo n.º 5
0
 public Episode(FullEpisode episode)
 {
     Artists = new[] { episode.Show.Publisher };
     Title   = episode.Name;
     Uri     = episode.Uri;
     Message = $"{Title} by {string.Join(", ", Artists)} || {Uri}";
     IsLocal = false;
 }
Exemplo n.º 6
0
    private async void OnToggleAddToLibrary()
    {
        SpotifyClient client = SpotifyService.Instance.GetSpotifyClient();

        // Get current context and check any are null
        CurrentlyPlayingContext context = this.GetCurrentContext();

        if (client != null && context != null)
        {
            List <string> ids = new List <string>();
            // Cast Item to correct type, add it's URI add make request
            if (context.Item.Type == ItemType.Track)
            {
                FullTrack track = context.Item as FullTrack;
                ids.Add(track.Id);

                if (_currentItemIsInLibrary)
                {
                    // Is in library, remove
                    LibraryRemoveTracksRequest removeRequest = new LibraryRemoveTracksRequest(ids);
                    await client.Library.RemoveTracks(removeRequest);

                    SetLibraryBtnIsLiked(false);
                }
                else
                {
                    // Not in library, add to user's library
                    LibrarySaveTracksRequest removeRequest = new LibrarySaveTracksRequest(ids);
                    await client.Library.SaveTracks(removeRequest);

                    SetLibraryBtnIsLiked(true);
                }
            }
            else if (context.Item.Type == ItemType.Episode)
            {
                FullEpisode episode = context.Item as FullEpisode;
                ids.Add(episode.Id);

                if (_currentItemIsInLibrary)
                {
                    LibraryRemoveShowsRequest request = new LibraryRemoveShowsRequest(ids);
                    await client.Library.RemoveShows(request);

                    SetLibraryBtnIsLiked(false);
                }
                else
                {
                    LibrarySaveShowsRequest request = new LibrarySaveShowsRequest(ids);
                    await client.Library.SaveShows(request);

                    SetLibraryBtnIsLiked(true);
                }
            }
        }
    }
Exemplo n.º 7
0
        /// <summary>
        /// Populate the songs and songsToRemove lists using allTracks
        /// </summary>
        /// <param name="allTracks">The full list of tracks to add from</param>
        /// <param name="songs">The playlists uri list</param>
        /// <param name="songsToRemove">The list of songs to remove</param>
        /// <returns></returns>
        private void PopulateSongLists(List <PlaylistTrack <IPlayableItem> > allTracks, List <Item> songs, List <Item> songsToRemove)
        {
            Log(LogType.Info, "Shuffle", "Populating lists...");
            int tracks = 0;
            int locals = 0;

            for (int i = allTracks.Count - 1; i >= 0; i--)
            {
                PlaylistTrack <IPlayableItem> track = allTracks[i];
                if (track.Track != null)
                {
                    bool   local = false;
                    string uri   = String.Empty;

                    switch (track.Track.Type)
                    {
                    case ItemType.Track:
                        FullTrack t = (track.Track as FullTrack);
                        if (t.Uri.ToLower().Contains("local"))
                        {
                            local = true;
                        }
                        else
                        {
                            uri = t.Uri;
                        }
                        break;

                    case ItemType.Episode:
                        FullEpisode e = (track.Track as FullEpisode);
                        if (e.Uri.ToLower().Contains("local"))
                        {
                            local = true;
                        }
                        else
                        {
                            uri = e.Uri;
                        }
                        break;
                    }
                    if (!local)
                    {
                        songs.Add(new PlaylistRemoveItemsRequest.Item()
                        {
                            Uri = uri
                        });
                        songsToRemove.Add(new PlaylistRemoveItemsRequest.Item()
                        {
                            Uri = uri
                        });
                        tracks++;
                    }
                    else
                    {
                        locals++;
                        Log(LogType.Warning, "Shuffle", "Found a local song. Skipping...");
                    }
                }
                else
                {
                    Log(LogType.Warning, "Shuffle", "Found an unavailable song. Skipping...");
                }
            }

            Tracks = tracks;
            Locals = locals;
        }
Exemplo n.º 8
0
    private async void FetchLatestPlayer()
    {
        if (_client != null)
        {
            // get the current context on this run
            CurrentlyPlayingContext newContext = await _client.Player.GetCurrentPlayback();

            // Check if not null
            if (newContext != null && newContext.Item != null)
            {
                // Check and cast the item to the correct type
                if (newContext.Item.Type == ItemType.Track)
                {
                    FullTrack currentTrack = newContext.Item as FullTrack;

                    // No previous track or previous item was different type
                    if (_currentItem == null || (_currentItem != null && _currentItem is FullEpisode episode))
                    {
                        Debug.Log($"No prev track or new type | -> '{S4UUtility.GetTrackString(currentTrack)}'");
                        _currentItem = currentTrack;
                        OnPlayingItemChanged?.Invoke(_currentItem);
                    }
                    else if (_currentItem != null && _currentItem is FullTrack lastTrack)
                    {
                        // Check if track name & artists aren't the same
                        if (lastTrack.Name != currentTrack.Name || S4UUtility.HasArtistsChanged(lastTrack.Artists, currentTrack.Artists))
                        {
                            Debug.Log($"Track to new Track | '{S4UUtility.GetTrackString(lastTrack)}' -> '{S4UUtility.GetTrackString(currentTrack)}'");
                            _currentItem = currentTrack;
                            OnPlayingItemChanged?.Invoke(_currentItem);
                        }
                    }
                }
                else if (newContext.Item.Type == ItemType.Episode)
                {
                    FullEpisode currentEpisode = newContext.Item as FullEpisode;

                    // If no previous item or current item is different type
                    if (_currentItem == null || (_currentItem != null && _currentItem is FullTrack track))
                    {
                        Debug.Log($"No prev episode or new type | -> '{currentEpisode.Show.Publisher} {currentEpisode.Name}'");
                        _currentItem = currentEpisode;
                        OnPlayingItemChanged?.Invoke(_currentItem);
                    }
                    else if (_currentItem != null && _currentItem is FullEpisode lastEpisode)
                    {
                        if (lastEpisode.Name != currentEpisode.Name || lastEpisode.Show?.Publisher != currentEpisode.Show?.Publisher)
                        {
                            Debug.Log($"Episode to new Episode | '{lastEpisode.Show.Publisher} {lastEpisode.Name}' -> '{currentEpisode.Show.Publisher} {currentEpisode.Name}'");
                            _currentItem = currentEpisode;
                            OnPlayingItemChanged?.Invoke(_currentItem);
                        }
                    }
                }
            }
            else
            {
                // No context or null current playing item

                // If previous item has been set
                if (_currentItem != null)
                {
                    Debug.Log($"Context null | '{(_currentItem.Type == ItemType.Track ? (_currentItem as FullTrack).Name : (_currentItem as FullEpisode).Name)}' -> ?");
                    _currentItem = null;
                    OnPlayingItemChanged?.Invoke(null);
                }
            }

            _currentContext = newContext;
        }
        else
        {
            // If no client but has a previous item, invoke event
            if (_currentItem != null)
            {
                _currentItem = null;
                OnPlayingItemChanged?.Invoke(null);
            }
        }
    }
 public Task <string> GetArtworkAsync(FullEpisode episode) => GetArtworkAsync(episode.Images, episode.Uri);
        private List <Result> GetPlaying()
        {
            var d = _client.ActiveDeviceName;

            if (d == null)
            {
                //Must have an active device to control Spotify
                return(SingleResult("No active device", "Select device with `sp device`", () => {}));
            }

            var item = _client.PlaybackContext.Item;

            if (item == null)
            {
                return(SingleResult("Nothing playing", $"Active Device: {d}", () => {}));
            }

            FullTrack   t = (item is FullTrack track ? track : null);
            FullEpisode e = (item is FullEpisode episode ? episode : null);

            var status       = _client.PlaybackContext.IsPlaying ? "Now Playing" : "Paused";
            var toggleAction = _client.PlaybackContext.IsPlaying ? "Pause" : "Resume";

            // Check if item is a track, episode, or default icon if neither work
            var icon = (t != null ? _client.GetArtworkAsync(t) :
                        e != null ? _client.GetArtworkAsync(e) :
                        null);

            return(new List <Result>()
            {
                new Result()
                {
                    Title = t.Name,
                    SubTitle = $"{status} | by {String.Join(", ",t.Artists.Select(a => String.Join("",a.Name)))}",
                    IcoPath = (icon != null ? icon.Result : SpotifyIcon)
                },
                new Result()
                {
                    IcoPath = SpotifyIcon,
                    Title = "Pause / Resume",
                    SubTitle = $"{toggleAction}: {t.Name}",
                    Action = _ =>
                    {
                        if (_client.PlaybackContext.IsPlaying)
                        {
                            _client.Pause();
                        }
                        else
                        {
                            _client.Play();
                        }
                        return true;
                    }
                },
                new Result()
                {
                    IcoPath = SpotifyIcon,
                    Title = "Next",
                    SubTitle = $"Skip: {t.Name}",
                    Action = context =>
                    {
                        _client.Skip();
                        return true;
                    }
                },
                new Result()
                {
                    IcoPath = SpotifyIcon,
                    Title = "Last",
                    SubTitle = "Skip backwards",
                    Action = context =>
                    {
                        _client.SkipBack();
                        return true;
                    }
                },
                ToggleMute().First(),
                ToggleShuffle().First(),
                SetVolume().First()
            });
        }