Raise OnTrackChange event for "other" tracks as well (#204)

* Raise OnTrackChange event for "other" tracks as well

* Add null checks in Example project
This commit is contained in:
Alessandro Attard Barbini 2018-01-10 14:56:58 +01:00 committed by Jonas Dellinger
parent aa8e940726
commit 5ccef6fd14
3 changed files with 21 additions and 11 deletions

View File

@ -84,21 +84,21 @@ namespace SpotifyAPI.Example
if (track.IsAd()) if (track.IsAd())
return; //Don't process further, maybe null values return; //Don't process further, maybe null values
titleLinkLabel.Text = track.TrackResource.Name; titleLinkLabel.Text = track.TrackResource?.Name;
titleLinkLabel.Tag = track.TrackResource.Uri; titleLinkLabel.Tag = track.TrackResource?.Uri;
artistLinkLabel.Text = track.ArtistResource.Name; artistLinkLabel.Text = track.ArtistResource?.Name;
artistLinkLabel.Tag = track.ArtistResource.Uri; artistLinkLabel.Tag = track.ArtistResource?.Uri;
albumLinkLabel.Text = track.AlbumResource.Name; albumLinkLabel.Text = track.AlbumResource?.Name;
albumLinkLabel.Tag = track.AlbumResource.Uri; 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); bigAlbumPicture.Image = track.AlbumResource != null ? await track.GetAlbumArtAsync(AlbumArtSize.Size640) : null;
smallAlbumPicture.Image = await track.GetAlbumArtAsync(AlbumArtSize.Size160); smallAlbumPicture.Image = track.AlbumResource != null ? await track.GetAlbumArtAsync(AlbumArtSize.Size160) : null;
} }
public void UpdatePlayingStatus(bool playing) public void UpdatePlayingStatus(bool playing)

View File

@ -39,6 +39,15 @@ namespace SpotifyAPI.Local.Models
return false; return false;
} }
/// <summary>
/// Checks if the track id of type "other"
/// </summary>
/// <returns>true if the track is neither an advert nor a normal track, for example a podcast</returns>
public bool IsOtherTrackType()
{
return TrackType == "other";
}
/// <summary> /// <summary>
/// Returns a URL to the album cover in the provided size /// Returns a URL to the album cover in the provided size
/// </summary> /// </summary>

View File

@ -107,7 +107,8 @@ namespace SpotifyAPI.Local
} }
if (newStatusResponse.Track != null && _eventStatusResponse.Track != null) 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() OnTrackChange?.Invoke(this, new TrackChangeEventArgs()
{ {