// Method to set the 'Love track' Ui state to a specific state internal void ResetLoveTrackState(LoveStatus newState) { this.BeginInvoke(new MethodInvoker(async() => { stripLoveTrack.Text = string.Empty; stripLoveTrack.Tag = newState; stripLoveTrack.Enabled = _currentUser != null && _currentMediaItem != null && _currentMediaItem.TrackLength > 0; switch (newState) { case LoveStatus.Love: { stripLoveTrack.Image = await ImageHelper.LoadImage("Resources\\love_off_64.png").ConfigureAwait(false); break; } case LoveStatus.Unlove: { stripLoveTrack.Image = await ImageHelper.LoadImage("Resources\\love_on_64.png").ConfigureAwait(false); break; } } })); RefreshMenuState(_currentMediaItem); }
// A common handler for various Ui elements when the mouse cursor leaves them private async void Common_MouseLeave(object sender, EventArgs e) { statusStrip1.Cursor = Cursors.Default; ToolStripLabel sendingLabel = sender as ToolStripStatusLabel; if (sendingLabel != null) { if (sendingLabel == stripLoveTrack) { LoveStatus currentStatus = (LoveStatus)sendingLabel.Tag; switch (currentStatus) { case LoveStatus.Love: { stripLoveTrack.Image = await ImageHelper.LoadImage("Resources\\love_off_64.png").ConfigureAwait(false); break; } case LoveStatus.Unlove: { stripLoveTrack.Image = await ImageHelper.LoadImage("Resources\\love_on_64.png").ConfigureAwait(false); break; } } } } }
// Method for sending the Love or Unlove status to the Last.fm API private async void LoveorUnloveCurrentMedia() { LoveStatus statusToSend = (LoveStatus)stripLoveTrack.Tag; try { await _apiClient.LoveTrack(statusToSend, _currentMediaItem).ConfigureAwait(false); ResetLoveTrackState((statusToSend == LoveStatus.Love) ? LoveStatus.Unlove : LoveStatus.Love); } catch (Exception) { // As current user is generally handled outside of this routine, we'll set it here _currentUser = null; // No connection to the API is available not a lot we can do about it... // (well not that is in scope for this phase of the project) ResetLoveTrackState(statusToSend); } }
// Public method for changing the 'Love' status for the specified media item public async Task LoveTrack(LoveStatus loveStatus, MediaItem mediaItem) { // Create an empty parameter base var baseParameters = new Dictionary <string, string>(); string updateMethod = string.Empty; // Add basic details of the track to the parameters baseParameters.Add("artist", mediaItem.ArtistName); baseParameters.Add("track", mediaItem.TrackName); // If the user just 'Loved' the track if (loveStatus == LoveStatus.Love) { // Use the API method 'love' updateMethod = "track.love"; } // Or if the user just 'unloved' the track else if (loveStatus == LoveStatus.Unlove) { // Use the API method 'unlove; updateMethod = "track.unlove"; } // Add the required authentication parameters AddRequiredRequestParams(baseParameters, updateMethod, _sessionToken.Key); // If a method has been specified (ie the caller hasn't tried to specify a value > 1 if (!string.IsNullOrEmpty(updateMethod)) { // Convert the parameters to body content FormUrlEncodedContent postContent = new FormUrlEncodedContent(baseParameters); // Post the request and get the response var rawResponse = await Post <JObject>(updateMethod, postContent, baseParameters.ToArray()).ConfigureAwait(false); #if DebugAPICalls Console.WriteLine($"Sent Love/Unlove track request ({updateMethod}), response:\r\n {rawResponse}"); #endif } }
// Method raised when the Ui is notified of 'Love Track' changes public void RefreshLoveTrackState() { LoveStatus currentState = (LoveStatus)stripLoveTrack.Tag; ResetLoveTrackState(currentState); }