From 1c14bc5ebbfa93dc448f63b57ca9e8558cdaaa52 Mon Sep 17 00:00:00 2001 From: MrDulfin Date: Tue, 27 Feb 2024 23:28:52 -0500 Subject: [PATCH] added queue clearing functions --- src/music_controller/queue.rs | 86 ++++++++++++++++++++++++++++------- src/music_storage/library.rs | 2 +- 2 files changed, 70 insertions(+), 18 deletions(-) diff --git a/src/music_controller/queue.rs b/src/music_controller/queue.rs index 05bb35e..8764d8f 100644 --- a/src/music_controller/queue.rs +++ b/src/music_controller/queue.rs @@ -1,7 +1,8 @@ +use font::opentype::tables::font_variations::InstanceFlags; use uuid::Uuid; use crate::{music_player::Player, music_storage::library::{Album, Song, URI}}; -use std::{error::Error, ops::Add, path::Path}; +use std::{error::Error, path::Path}; #[derive(Debug, PartialEq, Clone, Copy)] pub enum QueueState { @@ -10,7 +11,7 @@ pub enum QueueState { AddHere, None, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] #[non_exhaustive] pub enum QueueItemType<'a> { Song(Uuid), @@ -18,10 +19,11 @@ pub enum QueueItemType<'a> { Album{ album: Album<'a>, shuffled: bool, - } + }, + None } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] #[non_exhaustive] pub enum QueueSource { Library, @@ -30,12 +32,23 @@ pub enum QueueSource { Queue, File, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] #[non_exhaustive] pub struct QueueItem<'a> { item: QueueItemType<'a>, state: QueueState, - source: QueueSource + source: QueueSource, + by_human: bool +} +impl QueueItem<'_> { + fn new() -> Self { + QueueItem { + item: QueueItemType::None, + state: QueueState::None, + source: QueueSource::Library, + by_human: false + } + } } @@ -66,7 +79,8 @@ impl<'a> Queue<'a> { pub fn current_index(&mut self) -> i16 { let mut i = 1; let mut e = self.items.iter().filter(|song| song.state == QueueState::Played ).collect::>().len(); - while e >= 51 { + // TODO: make the max number of past songs modular + while e > 50 { self.items.remove(0); e = self.items.iter().filter(|song| song.state == QueueState::Played ).collect::>().len(); i+=1; @@ -74,7 +88,7 @@ impl<'a> Queue<'a> { e as i16 - 1 } - pub fn add_item(&mut self, item: QueueItemType<'a>, source: QueueSource) -> Result<(), Box> { + pub fn add_item(&mut self, item: QueueItemType<'a>, source: QueueSource, by_human: bool) -> Result<(), Box> { use QueueState::*; let ind = self.current_index(); let mut i: i16 = 1; @@ -100,7 +114,8 @@ impl<'a> Queue<'a> { }else { AddHere }, - source + source, + by_human } ); Ok(()) @@ -114,15 +129,52 @@ impl<'a> Queue<'a> { // update the state of the next item to replace the item being removed if self.items.get(ind + 1).is_some() { self.items[ind + 1].state = self.items[ind].state; + }else if self.items[ind].state != Current { + self.items[ind - 1].state = self.items[ind].state; } self.items[ind].state = None; self.items.remove(ind); - Ok(()) }else { Err("No Songs to remove!".into()) } } + + pub fn clear(&mut self) { + self.items.retain(|item| item.state == QueueState::Played ); + } + + pub fn clear_except(&mut self, index: usize) -> Result<(), Box> { + let mut index = index; + let ind = self.current_index(); + + if ind != -1 { + index += ind as usize; + }else { + index -=1 + } + + if !self.is_empty() && index < self.items.len() { + let i = self.items[index].clone(); + self.items.retain(|item| item.state == QueueState::Played || *item == i ); + self.items[(ind+1) as usize].state = QueueState::Current + }else { + return Err("index out of bounds!".into()); + } + Ok(()) + } + + pub fn clear_played(&mut self) { + self.items.retain(|item| item.state != QueueState::Played ); + } + + pub fn clear_all(&mut self) { + self.items.clear() + } + + fn is_empty(&self) -> bool { + self.items.iter().filter(|item| item.state != QueueState::Played).collect::>().len() == 0 + } } @@ -130,17 +182,17 @@ impl<'a> Queue<'a> { fn item_add_test() { let mut q = Queue::new().unwrap(); for _ in 0..5 { - q.items.push(QueueItem { item: QueueItemType::Song(Uuid::new_v4()), state: QueueState::Played, source: QueueSource::Queue }); + q.items.push(QueueItem { item: QueueItemType::Song(Uuid::new_v4()), state: QueueState::Played, source: QueueSource::Queue, by_human: false }); } + q.clear(); + for _ in 0..5 { + q.add_item(QueueItemType::Song(Uuid::new_v4()), QueueSource::Library, true).unwrap(); + } + // q.clear_played(); for _ in 0..3 { - q.add_item(QueueItemType::Song(Uuid::new_v4()), QueueSource::Library).unwrap(); - } - dbg!(&q.items, &q.items.len()); - - for _ in 0..1 { q.remove_item(0).inspect_err(|e| println!("{e:?}")); - dbg!(&q.items.len()); } + q.clear_except(4).inspect_err(|e| println!("{e:?}")); dbg!(&q.items, &q.items.len()); } \ No newline at end of file diff --git a/src/music_storage/library.rs b/src/music_storage/library.rs index 93b9991..9915682 100644 --- a/src/music_storage/library.rs +++ b/src/music_storage/library.rs @@ -515,7 +515,7 @@ pub enum Service { None, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub struct Album<'a> { title: &'a String, artist: Option<&'a String>,