Added configuration option for initial shuffle, repeat, volume

This commit is contained in:
dann-merlin
2020-02-20 09:15:08 +01:00
committed by Henrik Friedrichsen
parent 404ba65faf
commit 56a178dcfd
4 changed files with 47 additions and 7 deletions

View File

@@ -12,6 +12,14 @@ pub struct Config {
pub keybindings: Option<HashMap<String, String>>,
pub theme: Option<ConfigTheme>,
pub use_nerdfont: Option<bool>,
pub saved_state: Option<SavedState>,
}
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct SavedState {
pub volume: Option<u16>,
pub shuffle: Option<bool>,
pub repeat: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Default, Clone)]

View File

@@ -178,7 +178,7 @@ fn main() {
let event_manager = EventManager::new(cursive.cb_sink().clone());
let spotify = Arc::new(spotify::Spotify::new(event_manager.clone(), credentials));
let spotify = Arc::new(spotify::Spotify::new(event_manager.clone(), credentials, &cfg));
let queue = Arc::new(queue::Queue::new(spotify.clone()));

View File

@@ -23,13 +23,16 @@ pub struct Queue {
impl Queue {
pub fn new(spotify: Arc<Spotify>) -> Queue {
Queue {
let q = Queue {
queue: Arc::new(RwLock::new(Vec::new())),
spotify,
current_track: RwLock::new(None),
repeat: RwLock::new(RepeatSetting::None),
random_order: RwLock::new(None),
spotify,
}
};
q.set_repeat(q.spotify.repeat);
q.set_shuffle(q.spotify.shuffle);
q
}
pub fn next_index(&self) -> Option<usize> {

View File

@@ -46,6 +46,7 @@ use artist::Artist;
use config;
use events::{Event, EventManager};
use track::Track;
use queue;
pub const VOLUME_PERCENT: u16 = ((u16::max_value() as f64) * 1.0 / 100.0) as u16;
@@ -75,7 +76,9 @@ pub struct Spotify {
token_issued: RwLock<Option<SystemTime>>,
channel: mpsc::UnboundedSender<WorkerCommand>,
user: String,
volume: AtomicU16,
pub volume: AtomicU16,
pub repeat: queue::RepeatSetting,
pub shuffle: bool,
}
struct Worker {
@@ -211,14 +214,38 @@ impl futures::Future for Worker {
}
impl Spotify {
pub fn new(events: EventManager, credentials: Credentials) -> Spotify {
pub fn new(events: EventManager, credentials: Credentials, cfg: &config::Config) -> Spotify {
let player_config = PlayerConfig {
bitrate: Bitrate::Bitrate320,
normalisation: false,
normalisation_pregain: 0.0,
};
let (user_tx, user_rx) = oneshot::channel();
let volume = 0xFFFF;
let volume = match &cfg.saved_state {
Some(state) => match state.volume {
Some(vol) => vol,
None => 0xFFFF as u16,
},
None => 0xFFFF as u16,
};
let repeat = match &cfg.saved_state {
Some(state) => match &state.repeat {
Some(s) => match s.as_str() {
"track" => queue::RepeatSetting::RepeatTrack,
"playlist" => queue::RepeatSetting::RepeatPlaylist,
_ => queue::RepeatSetting::None,
}
_ => queue::RepeatSetting::None,
},
_ => queue::RepeatSetting::None,
};
let shuffle = match &cfg.saved_state {
Some(state) => match &state.shuffle {
Some(true) => true,
_ => false,
},
None => false,
};
let (tx, rx) = mpsc::unbounded();
{
@@ -236,6 +263,8 @@ impl Spotify {
channel: tx,
user: user_rx.wait().expect("error retrieving userid from worker"),
volume: AtomicU16::new(volume),
repeat: repeat,
shuffle: shuffle,
};
// acquire token for web api usage