From 6b89127dd0dd186776930780d0760995d4e9272c Mon Sep 17 00:00:00 2001 From: jika Date: Sun, 13 Apr 2025 16:44:59 +0200 Subject: [PATCH] Current song persist queue reset --- src/app.rs | 10 +++++----- src/player.rs | 18 +++++++++++++----- src/subsonic_helper.rs | 2 +- src/widgets/progress.rs | 8 ++------ 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/app.rs b/src/app.rs index 0417fdd..a949ca2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -198,17 +198,17 @@ impl App { Action::UpdateQueue => self.queue_state.select_first(), Action::Next => self.player.skip_next(), Action::Previous => { - let index = self.player.current_playing; + let index = self.player.current_idx; if index > 0 { self.tx.send(Action::ForcePlaySong(index - 1))?; - self.player.current_playing -= 1; + self.player.current_idx -= 1; } } Action::TrackEnded => { - let index = self.player.current_playing + 1; + let index = self.player.current_idx + 1; if index < self.player.songs_list.lock().unwrap().len() { self.tx.send(Action::TryPlaySong(index))?; - self.player.current_playing += 1; + self.player.current_idx += 1; } } } @@ -240,7 +240,7 @@ impl Widget for &mut App { Mode::Quit => {} Mode::Queue => StatefulWidget::render( - &Queue::new(self.player.songs_list.clone(), self.player.current_playing), + &Queue::new(self.player.songs_list.clone(), self.player.current_idx), layout[0], buf, &mut self.queue_state, diff --git a/src/player.rs b/src/player.rs index 5f45fd8..6cec032 100644 --- a/src/player.rs +++ b/src/player.rs @@ -19,7 +19,8 @@ use crate::subsonic_helper::make_request_url; pub struct Player { tx: UnboundedSender, pub songs_list: Arc>>, - pub current_playing: usize, + pub current_song: Option, + pub current_idx: usize, sink: Sink, _stream: OutputStream, } @@ -42,7 +43,8 @@ impl Player { _stream, sink, songs_list: Default::default(), - current_playing: 0, + current_idx: 0, + current_song: None, }) } @@ -50,7 +52,12 @@ impl Player { /// Download file if it does not exist yet /// In any case try to create a decoder from it and send action to add it to the sink pub fn try_play(&self, index: usize, client: Client) -> Result<()> { - let id = self.songs_list.lock().unwrap()[index].id.clone(); + let id = if let Some(song) = self.songs_list.lock().unwrap().get(index) { + song.id.clone() + } else { + return Ok(()); + }; + let mut path = config::temp_dir(); path.push(format!("{id}.mp3")); @@ -86,7 +93,8 @@ impl Player { pub fn add_to_sink(&mut self, index: usize) -> Result<()> { if self.sink.empty() { // set only if is first to be added to sink - self.current_playing = index; + self.current_idx = index; + self.current_song = self.songs_list.lock().unwrap().get(index).cloned(); } // Retrieve file from index let id = self.songs_list.lock().unwrap()[index].id.clone(); @@ -145,7 +153,7 @@ impl Player { self.sink.get_pos() } pub fn ratio_played(&self) -> f64 { - if let Some(song) = self.songs_list.lock().unwrap().get(self.current_playing) { + if let Some(song) = &self.current_song { self.sink.get_pos().as_secs() as f64 / song .duration diff --git a/src/subsonic_helper.rs b/src/subsonic_helper.rs index 73cc01e..c128c4f 100644 --- a/src/subsonic_helper.rs +++ b/src/subsonic_helper.rs @@ -28,7 +28,7 @@ pub fn make_request_url(request_body: R) -> String { username: config::get().subsonic.username.clone(), authentication: Authentication::Token { token, salt }, version: Version::LATEST, - client: "akula".to_string(), + client: env!("CARGO_PKG_NAME").to_string(), format: None, body: request_body, }; diff --git a/src/widgets/progress.rs b/src/widgets/progress.rs index a641a2f..ebbe56f 100644 --- a/src/widgets/progress.rs +++ b/src/widgets/progress.rs @@ -21,13 +21,9 @@ impl Widget for &Progress<'_> { fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) { let ratio = self.player.ratio_played(); let pos = self.player.current_pos_duration().as_secs(); - if let Some(song) = self + if let Some(song) = &self .player - .songs_list - .lock() - .unwrap() - .get(self.player.current_playing) - { + .current_song { Block::default() .title(format!( "{} - {}",