Show notifications on song download
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use std::{collections::HashMap, str::FromStr, sync::{Arc, Mutex, MutexGuard}, time::Duration};
|
||||
use std::{collections::HashMap, str::FromStr, sync::{mpsc::{self, Receiver, Sender}, Arc, Mutex, MutexGuard}, time::Duration};
|
||||
use downloader::song::SongStatus;
|
||||
use log::warn;
|
||||
use xmpd_manifest::song::Song;
|
||||
|
||||
pub mod downloader;
|
||||
@@ -25,6 +26,11 @@ pub enum DlStatus {
|
||||
Error(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Message {
|
||||
DownloadDone(uuid::Uuid),
|
||||
}
|
||||
|
||||
impl Cache {
|
||||
pub fn get() -> crate::Result<MutexGuard<'static, Self>> {
|
||||
match CACHE.lock() {
|
||||
@@ -33,8 +39,10 @@ impl Cache {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(&mut self) -> Result<()> {
|
||||
start_cache_mv_thread();
|
||||
pub fn init(&mut self) -> Result<Receiver<Message>> {
|
||||
let (internal_tx, cache_rx) = mpsc::channel::<Message>();
|
||||
// let (internal_rx, cache_tx) = mpsc::channel::<Message>();
|
||||
start_cache_mv_thread(internal_tx);
|
||||
self.cache_dir = xmpd_cliargs::CLIARGS.cache_path();
|
||||
|
||||
{ // Get cached songs
|
||||
@@ -62,7 +70,7 @@ impl Cache {
|
||||
}
|
||||
{ // Get Cached meta
|
||||
}
|
||||
Ok(())
|
||||
Ok(cache_rx)
|
||||
}
|
||||
|
||||
pub fn download_to_cache(&mut self, sid: uuid::Uuid, song: Song) {
|
||||
@@ -77,12 +85,13 @@ impl Cache {
|
||||
}
|
||||
|
||||
pub fn get_cached_song_status(&mut self, sid: &uuid::Uuid) -> Option<DlStatus> {
|
||||
Some(self.song_cache.get(sid)?.clone())
|
||||
let original = self.song_cache.get(sid)?.clone();
|
||||
Some(original)
|
||||
}
|
||||
}
|
||||
|
||||
fn start_cache_mv_thread() {
|
||||
std::thread::spawn(|| {
|
||||
fn start_cache_mv_thread(tx: Sender<Message>) {
|
||||
std::thread::spawn(move || {
|
||||
loop {
|
||||
{
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
@@ -98,6 +107,7 @@ fn start_cache_mv_thread() {
|
||||
let song_p = song_p.with_extension(&song_format);
|
||||
log::debug!("Found done: {:?}: {}", song_p, song_p.exists());
|
||||
if song_p.exists() {
|
||||
let _ = tx.send(Message::DownloadDone(sid.clone()));
|
||||
cache.song_cache.insert(sid.clone(), DlStatus::Done(song_p));
|
||||
done_jobs.push(sid.clone());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user