Skip to content
Prev Previous commit
Next Next commit
shuffle pick from MadeForYou context
  • Loading branch information
BKitor committed Mar 16, 2020
commit 9c0430ca15b5eff57888bff17971edadb26f1a36
23 changes: 21 additions & 2 deletions src/handlers/track_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,28 @@ fn play_random_song(app: &mut App) {
Some(thread_rng().gen_range(0, num_tracks)),
))
}
// let rand_playlist_idx = thread_rng().gen_range(0, )
}
TrackTableContext::MadeForYou => {}
TrackTableContext::MadeForYou => {
let (context_uri, track_json) = match app
Comment thread
Rigellute marked this conversation as resolved.
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.

.items
.get(app.made_for_you_index)
.unwrap()
{
playlist => (Some(playlist.uri.to_owned()), playlist.tracks.get("total")),
};
if let Some(val) = track_json{
let num_tracks:usize = from_value(val.clone()).unwrap();
app.dispatch(IoEvent::StartPlayback(
context_uri,
None,
Some(thread_rng().gen_range(0, num_tracks))
))
}
}
},
None => {}
};
Expand Down