Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Change --like that toggled to manual --like and --dislike.
  • Loading branch information
OrangeFran committed Jan 22, 2021
commit 0f41bd9218ba01b146144115a9107290a5a0c6ac
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
32 changes: 22 additions & 10 deletions src/cli/cli_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ 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: &String) -> bool {
// Update the liked_song_ids_set
self
.net
.handle_network_event(IoEvent::CurrentUserSavedTracksContains(vec![id.clone()]))
.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 +333,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 +344,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 +420,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