Skip to content

pick random song from tracklist#339

Merged
Rigellute merged 11 commits into
Rigellute:masterfrom
BKitor:pick-random-song-with-shift-s
Mar 20, 2020
Merged

pick random song from tracklist#339
Rigellute merged 11 commits into
Rigellute:masterfrom
BKitor:pick-random-song-with-shift-s

Conversation

@BKitor
Copy link
Copy Markdown
Contributor

@BKitor BKitor commented Mar 16, 2020

When using Spotify I often use their Shuffle Playlist button. Spotify-tui doesn't seem to have that kind of feature. What if Shift-s on Track Table chooses a random song from whatever context is active, and plays it.

@BKitor BKitor force-pushed the pick-random-song-with-shift-s branch from 780ec70 to 50400ab Compare March 16, 2020 01:49
@BKitor BKitor force-pushed the pick-random-song-with-shift-s branch from 50400ab to b368296 Compare March 16, 2020 01:52
@BKitor BKitor changed the title pick random song from playlist pick random song from tracklist Mar 16, 2020
Copy link
Copy Markdown
Owner

@Rigellute Rigellute left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a useful feature, thank you @BKitor!

I've left a few comments that should simplify the implementation a little. Take a look and let me know if I've preserved the functionality 👍

At the end of the review, I've rewritten the play_random_song function with all my requested changes.

Comment thread src/handlers/track_table.rs Outdated
}

fn play_random_song(app: &mut App) {
let TrackTable { context, .. } = &app.track_table;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can reduce the nesting here by avoiding destructuring e.g.

if let Some(context) = &app.track_table.context {
  match context {

Comment thread src/handlers/track_table.rs
Comment thread src/handlers/track_table.rs
Comment thread src/handlers/track_table.rs Outdated
Comment thread src/handlers/track_table.rs
@BKitor
Copy link
Copy Markdown
Contributor Author

BKitor commented Mar 17, 2020

@Rigellute Thanks for the advice! I appreciate it!
I don't think your recommendations preserve functionality. The goal isn't to pick a random playlist, but to pick a song at random from a playlist.

Copy link
Copy Markdown
Owner

@Rigellute Rigellute left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out that we do need to deserialise the total number of tracks in a playlist.

I've just left a few more comments - main one is about removing unwrap, what do you think?

Comment thread src/handlers/track_table.rs Outdated
}

fn play_random_song(app: &mut App) {
// let TrackTable { context, .. } = &app.track_table;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you able to remove this commented line?

Comment thread src/handlers/track_table.rs
Comment thread src/handlers/track_table.rs Outdated
.library
.made_for_you_playlists
.get_results(Some(0))
.unwrap()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you able to remove these unwraps? Something like this?

TrackTableContext::MadeForYou => {
  if let Some(playlist) = &app
    .library
    .made_for_you_playlists
    .get_results(Some(0))
    .and_then(|playlist| playlist.items.get(app.made_for_you_index))
  {
    if let Some(num_tracks) = &playlist
      .tracks
      .get("total")
      .and_then(|total| -> Option<usize> { from_value(total.clone()).ok() })
    {
      let uri = Some(playlist.uri.clone());
      app.dispatch(IoEvent::StartPlayback(
        uri,
        None,
        Some(thread_rng().gen_range(0, num_tracks)),
      ));
    }
  };
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works great! I didn't know and_then() existed. Thanks for the tip.

Copy link
Copy Markdown
Owner

@Rigellute Rigellute left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @BKitor, thank you

@Rigellute
Copy link
Copy Markdown
Owner

@all-contributors please add @BKitor for code

@Rigellute Rigellute merged commit dcf41f9 into Rigellute:master Mar 20, 2020
@allcontributors
Copy link
Copy Markdown
Contributor

@Rigellute

I've put up a pull request to add @BKitor! 🎉

nighi pushed a commit to nighi/spotify-tui that referenced this pull request Jul 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants