Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pick random song from tracklist #339

Merged
merged 11 commits into from
Mar 20, 2020

Conversation

BKitor
Copy link
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
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.

@@ -149,6 +152,108 @@ pub fn handler(key: Key, app: &mut App) {
}
}

fn play_random_song(app: &mut App) {
let TrackTable { context, .. } = &app.track_table;
Copy link
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 {

src/handlers/track_table.rs Show resolved Hide resolved
src/handlers/track_table.rs Show resolved Hide resolved
src/handlers/track_table.rs Outdated Show resolved Hide resolved
src/handlers/track_table.rs Show resolved Hide resolved
@BKitor
Copy link
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
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?

@@ -149,6 +152,107 @@ pub fn handler(key: Key, app: &mut App) {
}
}

fn play_random_song(app: &mut App) {
// let TrackTable { context, .. } = &app.track_table;
Copy link
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?

src/handlers/track_table.rs Show resolved Hide resolved
.library
.made_for_you_playlists
.get_results(Some(0))
.unwrap()
Copy link
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
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
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
Owner

@all-contributors please add @BKitor for code

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

@Rigellute

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

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