diff --git a/src/playback.rs b/src/playback.rs index 38f5c39..50d3707 100644 --- a/src/playback.rs +++ b/src/playback.rs @@ -1,5 +1,6 @@ use std::{fs::File, path::PathBuf}; +use rmp::PlaylistElement; use rodio::{OutputStream, OutputStreamHandle, Sink}; use super::decoder::{Decoder, DecoderImpl, SourceWrapper}; @@ -9,12 +10,15 @@ pub struct Playback { _stream: OutputStream, _stream_handle: OutputStreamHandle, sink: Sink, + pub current: Option, } -pub enum PlaybackState { - Empty, - Playing, - Paused, +#[derive(Clone, Debug, Default)] +pub struct Player { + pub track: PlaylistElement, + pub is_paused: bool, + pub duration: Option, + pub position: Option, } impl Playback { @@ -26,17 +30,7 @@ impl Playback { _stream, _stream_handle, sink, - } - } - - pub fn get_state(&self) -> PlaybackState { - // TODO: We know if it's playing but not what is playing. Figure it out later - if self.sink.empty() { - return PlaybackState::Empty; - } - match self.sink.is_paused() { - true => PlaybackState::Paused, - false => PlaybackState::Playing, + current: None, } } @@ -60,6 +54,12 @@ impl Playback { match source { Some(source) => { + self.current = Some(Player { + track: path, + is_paused: false, + duration: source.total_duration().map(|dur| dur.as_secs_f32()), + position: None, + }); self.sink.clear(); self.sink.append(source); self.sink.play(); @@ -70,10 +70,11 @@ impl Playback { /// Toggle playback pause if possible. pub fn toggle_pause(&mut self) { - match self.get_state() { - PlaybackState::Empty => (), - PlaybackState::Playing => self.sink.pause(), - PlaybackState::Paused => self.sink.play(), + if let Some(current) = &self.current { + match current.is_paused { + true => self.sink.play(), + false => self.sink.pause(), + } } } } diff --git a/src/ui.rs b/src/ui.rs index bd37047..2ac23c8 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -33,12 +33,11 @@ fn draw_playlist(f: &mut Frame, app: &App, area: Rect) { .state .selected() .map_or(false, |selected| index == selected); - // let playing = app - // .player_state - // .currently_playing - // .clone() - // .map_or(false, |currently_playing| currently_playing == *path); - let playing = false; + let playing = app + .playback + .current + .clone() + .map_or(false, |current| current.track == *path); let mut style = Style::default(); match (selected, playing) { (true, false) => {