Current song persist queue reset
This commit is contained in:
10
src/app.rs
10
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,
|
||||
|
||||
@@ -19,7 +19,8 @@ use crate::subsonic_helper::make_request_url;
|
||||
pub struct Player {
|
||||
tx: UnboundedSender<Action>,
|
||||
pub songs_list: Arc<Mutex<Vec<Child>>>,
|
||||
pub current_playing: usize,
|
||||
pub current_song: Option<Child>,
|
||||
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
|
||||
|
||||
@@ -28,7 +28,7 @@ pub fn make_request_url<R: SubsonicRequest>(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,
|
||||
};
|
||||
|
||||
@@ -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!(
|
||||
"{} - {}",
|
||||
|
||||
Reference in New Issue
Block a user