From 5ccef6fd1411fb51bf2a83008a95c2ea13d92ee8 Mon Sep 17 00:00:00 2001 From: Alessandro Attard Barbini <11708634+aleab@users.noreply.github.com> Date: Wed, 10 Jan 2018 14:56:58 +0100 Subject: [PATCH] Raise OnTrackChange event for "other" tracks as well (#204) * Raise OnTrackChange event for "other" tracks as well * Add null checks in Example project --- SpotifyAPI.Example/LocalControl.cs | 20 ++++++++++---------- SpotifyAPI/Local/Models/Track.cs | 9 +++++++++ SpotifyAPI/Local/SpotifyLocalAPI.cs | 3 ++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/SpotifyAPI.Example/LocalControl.cs b/SpotifyAPI.Example/LocalControl.cs index a76df043..83ac80c3 100644 --- a/SpotifyAPI.Example/LocalControl.cs +++ b/SpotifyAPI.Example/LocalControl.cs @@ -84,21 +84,21 @@ namespace SpotifyAPI.Example if (track.IsAd()) return; //Don't process further, maybe null values - titleLinkLabel.Text = track.TrackResource.Name; - titleLinkLabel.Tag = track.TrackResource.Uri; + titleLinkLabel.Text = track.TrackResource?.Name; + titleLinkLabel.Tag = track.TrackResource?.Uri; - artistLinkLabel.Text = track.ArtistResource.Name; - artistLinkLabel.Tag = track.ArtistResource.Uri; + artistLinkLabel.Text = track.ArtistResource?.Name; + artistLinkLabel.Tag = track.ArtistResource?.Uri; - albumLinkLabel.Text = track.AlbumResource.Name; - albumLinkLabel.Tag = track.AlbumResource.Uri; + albumLinkLabel.Text = track.AlbumResource?.Name; + albumLinkLabel.Tag = track.AlbumResource?.Uri; - SpotifyUri uri = track.TrackResource.ParseUri(); + SpotifyUri uri = track.TrackResource?.ParseUri(); - trackInfoBox.Text = $@"Track Info - {uri.Id}"; + trackInfoBox.Text = $@"Track Info - {uri?.Id}"; - bigAlbumPicture.Image = await track.GetAlbumArtAsync(AlbumArtSize.Size640); - smallAlbumPicture.Image = await track.GetAlbumArtAsync(AlbumArtSize.Size160); + bigAlbumPicture.Image = track.AlbumResource != null ? await track.GetAlbumArtAsync(AlbumArtSize.Size640) : null; + smallAlbumPicture.Image = track.AlbumResource != null ? await track.GetAlbumArtAsync(AlbumArtSize.Size160) : null; } public void UpdatePlayingStatus(bool playing) diff --git a/SpotifyAPI/Local/Models/Track.cs b/SpotifyAPI/Local/Models/Track.cs index 45242930..ea50741f 100755 --- a/SpotifyAPI/Local/Models/Track.cs +++ b/SpotifyAPI/Local/Models/Track.cs @@ -38,6 +38,15 @@ namespace SpotifyAPI.Local.Models return true; return false; } + + /// + /// Checks if the track id of type "other" + /// + /// true if the track is neither an advert nor a normal track, for example a podcast + public bool IsOtherTrackType() + { + return TrackType == "other"; + } /// /// Returns a URL to the album cover in the provided size diff --git a/SpotifyAPI/Local/SpotifyLocalAPI.cs b/SpotifyAPI/Local/SpotifyLocalAPI.cs index 245ca1c2..28b0777c 100644 --- a/SpotifyAPI/Local/SpotifyLocalAPI.cs +++ b/SpotifyAPI/Local/SpotifyLocalAPI.cs @@ -107,7 +107,8 @@ namespace SpotifyAPI.Local } if (newStatusResponse.Track != null && _eventStatusResponse.Track != null) { - if (newStatusResponse.Track.TrackResource?.Uri != _eventStatusResponse.Track.TrackResource?.Uri) + if (newStatusResponse.Track.TrackResource?.Uri != _eventStatusResponse.Track.TrackResource?.Uri || + newStatusResponse.Track.IsOtherTrackType() && newStatusResponse.Track.Length != this._eventStatusResponse.Track.Length) { OnTrackChange?.Invoke(this, new TrackChangeEventArgs() {