-
-
Notifications
You must be signed in to change notification settings - Fork 574
pick random song from tracklist #339
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
Changes from 8 commits
b368296
122b554
392db76
9c0430c
a1d1844
bbff58d
402483d
e63b213
8b855d4
d4a639d
b93f5a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ use super::{ | |
| }; | ||
| use crate::event::Key; | ||
| use crate::network::IoEvent; | ||
| use rand::{thread_rng, Rng}; | ||
| use serde_json::from_value; | ||
|
|
||
| pub fn handler(key: Key, app: &mut App) { | ||
| match key { | ||
|
|
@@ -139,6 +141,7 @@ pub fn handler(key: Key, app: &mut App) { | |
| None => {} | ||
| }; | ||
| } | ||
| Key::Char('S') => play_random_song(app), | ||
| Key::Ctrl('e') => jump_to_end(app), | ||
| Key::Ctrl('a') => jump_to_start(app), | ||
| //recommended song radio | ||
|
|
@@ -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; | ||
| if let Some(context) = &app.track_table.context { | ||
| match context { | ||
| TrackTableContext::MyPlaylists => { | ||
| let (context_uri, track_json) = match (&app.selected_playlist_index, &app.playlists) { | ||
| (Some(selected_playlist_index), Some(playlists)) => { | ||
| if let Some(selected_playlist) = playlists.items.get(selected_playlist_index.to_owned()) | ||
| { | ||
| ( | ||
| Some(selected_playlist.uri.to_owned()), | ||
| selected_playlist.tracks.get("total"), | ||
| ) | ||
| } else { | ||
| (None, None) | ||
| } | ||
| } | ||
| _ => (None, None), | ||
| }; | ||
|
|
||
| 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)), | ||
| )); | ||
| } | ||
| } | ||
|
Rigellute marked this conversation as resolved.
|
||
| TrackTableContext::RecommendedTracks => {} | ||
| TrackTableContext::SavedTracks => { | ||
| if let Some(saved_tracks) = &app.library.saved_tracks.get_results(None) { | ||
| let track_uris: Vec<String> = saved_tracks | ||
| .items | ||
| .iter() | ||
| .map(|item| item.track.uri.to_owned()) | ||
| .collect(); | ||
| let rand_idx = thread_rng().gen_range(0, track_uris.len()); | ||
| app.dispatch(IoEvent::StartPlayback( | ||
| None, | ||
| Some(track_uris), | ||
| Some(rand_idx), | ||
| )) | ||
| } | ||
| } | ||
| TrackTableContext::AlbumSearch => {} | ||
| TrackTableContext::PlaylistSearch => { | ||
| let (context_uri, playlist_track_json) = match ( | ||
|
Rigellute marked this conversation as resolved.
|
||
| &app.search_results.selected_playlists_index, | ||
| &app.search_results.playlists, | ||
| ) { | ||
| (Some(selected_playlist_index), Some(playlist_result)) => { | ||
| if let Some(selected_playlist) = playlist_result | ||
| .playlists | ||
| .items | ||
| .get(selected_playlist_index.to_owned()) | ||
| { | ||
| ( | ||
| Some(selected_playlist.uri.to_owned()), | ||
| selected_playlist.tracks.get("total"), | ||
| ) | ||
| } else { | ||
| (None, None) | ||
| } | ||
| } | ||
| _ => (None, None), | ||
| }; | ||
| if let Some(val) = playlist_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)), | ||
| )) | ||
| } | ||
| } | ||
| TrackTableContext::MadeForYou => { | ||
| let (context_uri, track_json) = match app | ||
|
Rigellute marked this conversation as resolved.
Outdated
|
||
| .library | ||
| .made_for_you_playlists | ||
| .get_results(Some(0)) | ||
| .unwrap() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you able to remove these 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)),
));
}
};
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)), | ||
| )) | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
|
Rigellute marked this conversation as resolved.
|
||
| fn handle_recommended_tracks(app: &mut App) { | ||
| let (selected_index, tracks) = (&app.track_table.selected_index, &app.track_table.tracks); | ||
| if let Some(track) = tracks.get(*selected_index) { | ||
|
|
||
There was a problem hiding this comment.
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?