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

Change --like that toggles the state to explicit --like and --dislike flags #717

Merged
merged 2 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 12 additions & 2 deletions src/cli/clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ can be used together
.arg(
Arg::with_name("like")
.long("like")
.help("Likes the current song"),
.help("Likes the current song if possible"),
)
.arg(
Arg::with_name("dislike")
.long("dislike")
.help("Dislikes the current song if possible"),
)
.arg(
Arg::with_name("shuffle")
Expand Down Expand Up @@ -146,9 +151,14 @@ seconds backwards and `spt pb --seek 10` to the tenth second of the track.",
.multiple(false)
.conflicts_with_all(&["single", "flags", "actions"]),
)
.group(
ArgGroup::with_name("likes")
.args(&["like", "dislike"])
.multiple(false),
)
.group(
ArgGroup::with_name("flags")
.args(&["like", "shuffle", "repeat"])
.args(&["like", "dislike", "shuffle", "repeat"])
.multiple(true)
.conflicts_with_all(&["single", "jumps"]),
)
Expand Down
36 changes: 25 additions & 11 deletions src/cli/cli_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ impl<'a> CliApp<'a> {
Self { net, config }
}

async fn is_a_saved_track(&mut self, id: String) -> bool {
async fn is_a_saved_track(&mut self, id: &str) -> bool {
// Update the liked_song_ids_set
self
.net
.handle_network_event(IoEvent::CurrentUserSavedTracksContains(vec![id.clone()]))
.handle_network_event(IoEvent::CurrentUserSavedTracksContains(
vec![id.to_string()],
))
.await;
self.net.app.lock().await.liked_song_ids_set.contains(&id)
self.net.app.lock().await.liked_song_ids_set.contains(id)
}

pub fn format_output(&self, mut format: String, values: Vec<Format>) -> String {
Expand Down Expand Up @@ -333,7 +335,7 @@ impl<'a> CliApp<'a> {
Ok(())
}

// spt playback --like / --shuffle / --repeat
// spt playback --like / --dislike / --shuffle / --repeat
pub async fn mark(&mut self, flag: Flag) -> Result<()> {
let c = {
let app = self.net.app.lock().await;
Expand All @@ -344,19 +346,31 @@ impl<'a> CliApp<'a> {
};

match flag {
Flag::Like => {
Flag::Like(s) => {
// Get the id of the current song
let id = match c.item {
Some(i) => match i {
PlayingItem::Track(t) => t.id.ok_or_else(|| anyhow!("item has no id")),
PlayingItem::Episode(_) => Err(anyhow!("saving episodes not yet implemented")),
},
None => Err(anyhow!("no item playing")),
};
self
.net
.handle_network_event(IoEvent::ToggleSaveTrack(id?))
.await;
}?;

// Want to like but is already liked -> do nothing
// Want to like and is not liked yet -> like
if s && !self.is_a_saved_track(&id).await {
self
.net
.handle_network_event(IoEvent::ToggleSaveTrack(id))
.await;
// Want to dislike but is already disliked -> do nothing
// Want to dislike and is liked currently -> remove like
} else if !s && self.is_a_saved_track(&id).await {
self
.net
.handle_network_event(IoEvent::ToggleSaveTrack(id))
.await;
}
}
Flag::Shuffle => {
self
Expand Down Expand Up @@ -408,7 +422,7 @@ impl<'a> CliApp<'a> {
hs.push(Format::Flags((
context.repeat_state,
context.shuffle_state,
self.is_a_saved_track(id).await,
self.is_a_saved_track(&id).await,
)));
hs
}
Expand Down
12 changes: 10 additions & 2 deletions src/cli/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ impl Type {
//

pub enum Flag {
Like,
// Does not get toggled
// * User chooses like -> Flag::Like(true)
// * User chooses dislike -> Flag::Like(false)
Like(bool),
Shuffle,
Repeat,
}
Expand All @@ -87,9 +90,14 @@ impl Flag {
pub fn from_matches(m: &ArgMatches<'_>) -> Vec<Self> {
// Multiple flags are possible
let mut flags = Vec::new();

// Only one of these two
if m.is_present("like") {
flags.push(Self::Like);
flags.push(Self::Like(true));
} else if m.is_present("dislike") {
flags.push(Self::Like(false));
}

if m.is_present("shuffle") {
flags.push(Self::Shuffle);
}
Expand Down