Add rebindable keys, refactor lists

This commit is contained in:
KoffeinFlummi
2019-03-17 03:17:30 +01:00
parent 3d385aff9b
commit 5a85619105
16 changed files with 757 additions and 605 deletions

View File

@@ -1,14 +1,12 @@
use std::slice::Iter;
use std::sync::Arc;
use std::sync::{Arc, RwLock};
use events::{Event, EventManager};
use spotify::Spotify;
use track::Track;
pub struct Queue {
// TODO: put this in an RwLock instead of locking the whole Queue struct
queue: Vec<Track>,
current_track: Option<usize>,
pub queue: Arc<RwLock<Vec<Track>>>,
current_track: RwLock<Option<usize>>,
spotify: Arc<Spotify>,
ev: EventManager,
}
@@ -16,18 +14,18 @@ pub struct Queue {
impl Queue {
pub fn new(ev: EventManager, spotify: Arc<Spotify>) -> Queue {
Queue {
queue: Vec::new(),
current_track: None,
queue: Arc::new(RwLock::new(Vec::new())),
current_track: RwLock::new(None),
spotify: spotify,
ev: ev,
}
}
pub fn next_index(&self) -> Option<usize> {
match self.current_track {
match *self.current_track.read().unwrap() {
Some(index) => {
let next_index = index + 1;
if next_index < self.queue.len() {
if next_index < self.queue.read().unwrap().len() {
Some(next_index)
} else {
None
@@ -38,7 +36,7 @@ impl Queue {
}
pub fn previous_index(&self) -> Option<usize> {
match self.current_track {
match *self.current_track.read().unwrap() {
Some(index) => {
if index > 0 {
Some(index - 1)
@@ -50,33 +48,41 @@ impl Queue {
}
}
pub fn get_current(&self) -> Option<&Track> {
match self.current_track {
Some(index) => Some(&self.queue[index]),
pub fn get_current(&self) -> Option<Track> {
match *self.current_track.read().unwrap() {
Some(index) => Some(self.queue.read().unwrap()[index].clone()),
None => None,
}
}
pub fn append(&mut self, track: &Track) {
self.queue.push(track.clone());
pub fn append(&self, track: &Track) {
let mut q = self.queue.write().unwrap();
q.push(track.clone());
}
pub fn append_next(&mut self, track: &Track) -> usize {
if let Some(next_index) = self.next_index() {
self.queue.insert(next_index, track.clone());
pub fn append_next(&self, track: &Track) -> usize {
let next = self.next_index();
let mut q = self.queue.write().unwrap();
if let Some(next_index) = next {
q.insert(next_index, track.clone());
next_index
} else {
self.queue.push(track.clone());
self.queue.len() - 1
q.push(track.clone());
q.len() - 1
}
}
pub fn remove(&mut self, index: usize) {
self.queue.remove(index);
pub fn remove(&self, index: usize) {
{
let mut q = self.queue.write().unwrap();
q.remove(index);
}
// if the queue is empty or we are at the end of the queue, stop
// playback
if self.queue.len() == 0 || index == self.queue.len() {
let len = self.queue.read().unwrap().len();
if len == 0 || index == len {
self.stop();
return;
}
@@ -84,27 +90,32 @@ impl Queue {
// if we are deleting the currently playing track, play the track with
// the same index again, because the next track is now at the position
// of the one we deleted
if let Some(current_track) = self.current_track {
let current = *self.current_track.read().unwrap();
if let Some(current_track) = current {
if index == current_track {
self.play(index);
} else if index < current_track {
self.current_track = Some(current_track - 1);
let mut current = self.current_track.write().unwrap();
current.replace(current_track - 1);
}
}
}
pub fn clear(&mut self) {
pub fn clear(&self) {
self.stop();
self.queue.clear();
let mut q = self.queue.write().unwrap();
q.clear();
// redraw queue if open
self.ev.send(Event::ScreenChange("queue".to_owned()));
}
pub fn play(&mut self, index: usize) {
let track = &self.queue[index];
pub fn play(&self, index: usize) {
let track = &self.queue.read().unwrap()[index];
self.spotify.load(&track);
self.current_track = Some(index);
let mut current = self.current_track.write().unwrap();
current.replace(index);
self.spotify.play();
self.spotify.update_track();
}
@@ -113,12 +124,13 @@ impl Queue {
self.spotify.toggleplayback();
}
pub fn stop(&mut self) {
self.current_track = None;
pub fn stop(&self) {
let mut current = self.current_track.write().unwrap();
*current = None;
self.spotify.stop();
}
pub fn next(&mut self) {
pub fn next(&self) {
if let Some(index) = self.next_index() {
self.play(index);
} else {
@@ -126,15 +138,11 @@ impl Queue {
}
}
pub fn previous(&mut self) {
pub fn previous(&self) {
if let Some(index) = self.previous_index() {
self.play(index);
} else {
self.spotify.stop();
}
}
pub fn iter(&self) -> Iter<Track> {
self.queue.iter()
}
}