Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ clipboard = "0.5.0"
crossterm = "0.16"
tokio = { version = "0.2", features = ["full"] }
proc-macro2 = "1.0.9"
rand="0.7.3"

[[bin]]
bench = false
Expand Down
104 changes: 104 additions & 0 deletions src/handlers/track_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
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?

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)),
));
}
}
Comment thread
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 (
Comment thread
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
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)),
))
}
}
}
};
}

Comment thread
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) {
Expand Down
1 change: 1 addition & 0 deletions src/ui/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ pub fn get_help_docs() -> Vec<Vec<&'static str>> {
vec!["Delete saved album", "D", "Library -> Albums"],
vec!["Delete saved playist", "D", "Playlist"],
vec!["Follow an artists/playlist", "w", "Search result"],
vec!["Play random song in playlist", "S", "Selected Playlist"],
]
}