Skip to content

Commit 6de4ce2

Browse files
author
Ross
committed
Allow for favoriting and unfavoriting songs
1 parent 3e44c40 commit 6de4ce2

4 files changed

Lines changed: 47 additions & 8 deletions

File tree

src/app/mpris.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use anyhow::Result;
12
use mpris_server::{PlaybackStatus, Property};
23
use notify_rust::{Hint, Notification};
3-
use anyhow::Result;
44

55
use crate::app::Track;
66

@@ -15,8 +15,8 @@ impl App {
1515
PlaybackStatus::Stopped
1616
};
1717

18-
let can_next = self.queue_tab.index < self.queue_tab.len().saturating_sub(1);
19-
let can_prev = self.queue_tab.index > 0;
18+
let can_next = self.playing_index < self.queue_tab.len().saturating_sub(1);
19+
let can_prev = self.playing_index > 0;
2020
let current_pos = self.player.lock().await.get_position();
2121

2222
if let Ok(mut state) = self.shared_state.write() {

src/app/queue.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
use super::App;
12
use crate::app::{ActiveSection, ActiveTab, Track};
23
use anyhow::Result;
34
use futures::future;
4-
use super::App;
5-
65

76
impl App {
87
pub fn add_search_result_to_queue(&mut self) {
@@ -69,4 +68,28 @@ impl App {
6968
}
7069
Ok(())
7170
}
71+
pub async fn make_favorite(&mut self, remove: bool) -> Result<()> {
72+
match (&self.active_section, &self.active_tab) {
73+
(ActiveSection::Queue, _) => {
74+
if !self.queue_tab.data.is_empty() {
75+
let song = self.queue_tab.get().unwrap();
76+
self.subsonic_client.favorite_a_song(song, remove).await?;
77+
}
78+
}
79+
(ActiveSection::Others, ActiveTab::Songs) => {
80+
if !self.tracks_tab.data.is_empty() {
81+
let song = self.tracks_tab.get().unwrap();
82+
self.subsonic_client.favorite_a_song(song, remove).await?;
83+
}
84+
}
85+
(ActiveSection::Others, ActiveTab::Search) => {
86+
if !self.search_tab.data.is_empty() {
87+
let song = self.search_tab.get().unwrap();
88+
self.subsonic_client.favorite_a_song(song, remove).await?;
89+
}
90+
}
91+
_ => (),
92+
}
93+
Ok(())
94+
}
7295
}

src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod ui;
1010
use anyhow::Result;
1111
use app::App;
1212
use crossterm::{
13-
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
13+
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
1414
execute,
1515
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
1616
};
@@ -95,14 +95,18 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> Result
9595
app.start_inline_search();
9696
}
9797
}
98+
KeyCode::Char('f') if key.modifiers.contains(KeyModifiers::SHIFT) => {
99+
app.make_favorite(true).await?
100+
}
101+
KeyCode::Char('f') => app.make_favorite(false).await?,
98102
KeyCode::Char('n') => app.play_next().await?,
99103
KeyCode::Char('p') => app.play_previous().await?,
100104
_ => {}
101105
}
102106
}
103107
}
104108
app.update().await?;
105-
interval(Duration::from_millis(100)).tick().await;
106-
// tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
109+
// interval(Duration::from_millis(100)).tick().await;
110+
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
107111
}
108112
}

src/subsonic.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::app;
22
use crate::app::{Album, Artist, Playlists, Track};
33
use crate::config::Config;
44
use anyhow::{Context, Ok, Result};
5+
use ratatui::widgets::LegendPosition;
56
use serde::Deserialize;
67
use url::Url;
78

@@ -346,6 +347,17 @@ impl SubsonicClient {
346347
.await?;
347348
Ok(())
348349
}
350+
pub async fn favorite_a_song(&self, track: &Track, remove: bool) -> Result<()> {
351+
#[derive(Deserialize)]
352+
struct Empty {}
353+
354+
let mut endpoint = "star";
355+
if remove {
356+
endpoint = "unstar"
357+
}
358+
let _: Empty = self.get(endpoint, vec![("id", track.id.clone())]).await?;
359+
Ok(())
360+
}
349361
pub async fn get_songs_in_album(&self, album: &Album) -> Result<Vec<Track>> {
350362
let data: GetAlbumResponse = self.get("getAlbum", vec![("id", album.id.clone())]).await?;
351363
self.songs_to_tracks(data.album.song)

0 commit comments

Comments
 (0)