added queue clearing functions

This commit is contained in:
MrDulfin 2024-02-27 23:28:52 -05:00
parent e175fe7337
commit 1c14bc5ebb
2 changed files with 70 additions and 18 deletions

View file

@ -1,7 +1,8 @@
use font::opentype::tables::font_variations::InstanceFlags;
use uuid::Uuid; use uuid::Uuid;
use crate::{music_player::Player, music_storage::library::{Album, Song, URI}}; 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)] #[derive(Debug, PartialEq, Clone, Copy)]
pub enum QueueState { pub enum QueueState {
@ -10,7 +11,7 @@ pub enum QueueState {
AddHere, AddHere,
None, None,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
#[non_exhaustive] #[non_exhaustive]
pub enum QueueItemType<'a> { pub enum QueueItemType<'a> {
Song(Uuid), Song(Uuid),
@ -18,10 +19,11 @@ pub enum QueueItemType<'a> {
Album{ Album{
album: Album<'a>, album: Album<'a>,
shuffled: bool, shuffled: bool,
} },
None
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
#[non_exhaustive] #[non_exhaustive]
pub enum QueueSource { pub enum QueueSource {
Library, Library,
@ -30,12 +32,23 @@ pub enum QueueSource {
Queue, Queue,
File, File,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
#[non_exhaustive] #[non_exhaustive]
pub struct QueueItem<'a> { pub struct QueueItem<'a> {
item: QueueItemType<'a>, item: QueueItemType<'a>,
state: QueueState, 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 { pub fn current_index(&mut self) -> i16 {
let mut i = 1; let mut i = 1;
let mut e = self.items.iter().filter(|song| song.state == QueueState::Played ).collect::<Vec<&QueueItem>>().len(); let mut e = self.items.iter().filter(|song| song.state == QueueState::Played ).collect::<Vec<&QueueItem>>().len();
while e >= 51 { // TODO: make the max number of past songs modular
while e > 50 {
self.items.remove(0); self.items.remove(0);
e = self.items.iter().filter(|song| song.state == QueueState::Played ).collect::<Vec<&QueueItem>>().len(); e = self.items.iter().filter(|song| song.state == QueueState::Played ).collect::<Vec<&QueueItem>>().len();
i+=1; i+=1;
@ -74,7 +88,7 @@ impl<'a> Queue<'a> {
e as i16 - 1 e as i16 - 1
} }
pub fn add_item(&mut self, item: QueueItemType<'a>, source: QueueSource) -> Result<(), Box<dyn Error>> { pub fn add_item(&mut self, item: QueueItemType<'a>, source: QueueSource, by_human: bool) -> Result<(), Box<dyn Error>> {
use QueueState::*; use QueueState::*;
let ind = self.current_index(); let ind = self.current_index();
let mut i: i16 = 1; let mut i: i16 = 1;
@ -100,7 +114,8 @@ impl<'a> Queue<'a> {
}else { }else {
AddHere AddHere
}, },
source source,
by_human
} }
); );
Ok(()) Ok(())
@ -114,15 +129,52 @@ impl<'a> Queue<'a> {
// update the state of the next item to replace the item being removed // update the state of the next item to replace the item being removed
if self.items.get(ind + 1).is_some() { if self.items.get(ind + 1).is_some() {
self.items[ind + 1].state = self.items[ind].state; 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[ind].state = None;
self.items.remove(ind); self.items.remove(ind);
Ok(()) Ok(())
}else { }else {
Err("No Songs to remove!".into()) 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<dyn Error>> {
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::<Vec<_>>().len() == 0
}
} }
@ -130,17 +182,17 @@ impl<'a> Queue<'a> {
fn item_add_test() { fn item_add_test() {
let mut q = Queue::new().unwrap(); let mut q = Queue::new().unwrap();
for _ in 0..5 { 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 { 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:?}")); 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()); dbg!(&q.items, &q.items.len());
} }

View file

@ -515,7 +515,7 @@ pub enum Service {
None, None,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
pub struct Album<'a> { pub struct Album<'a> {
title: &'a String, title: &'a String,
artist: Option<&'a String>, artist: Option<&'a String>,