summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main.rs11
-rw-r--r--src/playlist.rs58
2 files changed, 12 insertions, 57 deletions
diff --git a/src/main.rs b/src/main.rs
index b93b815..e19a9ae 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -116,17 +116,6 @@ impl Application {
}
pub fn add_audio(&self, url: String) {
- if self
- .playlist
- .lock()
- .expect("Mutex was not poisoned")
- .is_full()
- {
- info!("Audio playlist is full");
- self.send_message("Playlist is full");
- return;
- }
-
match youtube_dl::get_audio_download_url(url) {
Ok((audio_url, audio_title)) => {
info!("Found audio url: {}", audio_url);
diff --git a/src/playlist.rs b/src/playlist.rs
index 689a743..86b9100 100644
--- a/src/playlist.rs
+++ b/src/playlist.rs
@@ -1,71 +1,37 @@
+use std::collections::VecDeque;
+
use log::info;
pub struct Playlist {
- data: Vec<Option<AudioRequest>>,
- read: usize,
- write: usize,
- is_full: bool,
+ data: VecDeque<AudioRequest>,
}
impl Playlist {
pub fn new() -> Self {
Self {
- data: Vec::with_capacity(50),
- read: 0,
- write: 0,
- is_full: false,
+ data: VecDeque::new(),
}
}
- pub fn push(&mut self, req: AudioRequest) -> bool {
- if self.is_full {
- return false;
- }
-
+ pub fn push(&mut self, req: AudioRequest) {
info!("Adding {} to playlist", &req.title);
- if self.data.len() < self.data.capacity() {
- self.data.push(Some(req));
- } else {
- self.data[self.write] = Some(req);
- }
-
- self.write = (self.write + 1) % self.data.capacity();
-
- if self.write == self.read {
- self.is_full = true;
- }
-
- true
- }
-
- pub fn is_empty(&self) -> bool {
- !self.is_full && self.write == self.read
- }
-
- pub fn is_full(&self) -> bool {
- self.is_full
+ self.data.push_front(req)
}
pub fn pop(&mut self) -> Option<AudioRequest> {
- if self.is_empty() {
- None
- } else {
- self.is_full = false;
- let res = self.data[self.read].take();
- self.read = (self.read + 1) % self.data.capacity();
+ let res = self.data.pop_back();
+ info!("Popping {:?} from playlist", res.as_ref().map(|r| &r.title));
- info!("Popping {:?} from playlist", res.as_ref().map(|r| &r.title));
+ res
+ }
- res
- }
+ pub fn is_empty(&self) -> bool {
+ self.data.is_empty()
}
pub fn clear(&mut self) {
self.data.clear();
- self.read = 0;
- self.write = 0;
- self.is_full = false;
info!("Cleared playlist")
}