Skip to content

Commit

Permalink
fix: refine loading indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
talwat committed Oct 16, 2024
1 parent 120ac3f commit ddc572b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,28 @@ impl Player {
}

/// This will play the next track, as well as refilling the buffer in the background.
///
/// This will also set `current` to the newly loaded song.
pub async fn next(&self) -> eyre::Result<tracks::Decoded> {
let track = match self.tracks.write().await.pop_front() {
Some(x) => x,
// If the queue is completely empty, then fallback to simply getting a new track.
// This is relevant particularly at the first song.
None => self.list.random(&self.client).await?,
None => {
// Serves as an indicator that the queue is "loading".
// We're doing it here so that we don't get the "loading" display
// for only a frame in the other case that the buffer is not empty.
self.current.store(None);

self.list.random(&self.client).await?
}
};

let decoded = track.decode()?;

// Set the current track.
self.set_current(decoded.info.clone()).await?;

Ok(decoded)
}

Expand All @@ -243,22 +255,14 @@ impl Player {
itx: Sender<()>,
tx: Sender<Messages>,
) -> eyre::Result<()> {
// Serves as an indicator that the queue is "loading".
player.current.store(None);

// Stop the sink.
player.sink.stop();

let track = player.next().await;

match track {
Ok(track) => {
// Set the current track.
player.set_current(track.info.clone()).await?;

// Actually start playing it, this is done later so that the amount
// or times where "loading" appears briefly even though the track is
// from the buffer is minimized.
// Start playing the new track.
player.sink.append(track.data);

// Notify the background downloader that there's an empty spot
Expand Down
2 changes: 1 addition & 1 deletion src/player/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async fn interface(player: Arc<Player>, minimalist: bool) -> eyre::Result<()> {
vec![action, middle, controls]
};

// Formats the menu properly
// Formats the menu properly.
let menu: Vec<String> = menu
.into_iter()
.map(|x| format!("│ {} │\r\n", x.reset()).to_string())
Expand Down

0 comments on commit ddc572b

Please sign in to comment.