mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-15 17:21:19 +10:00
feat(download manager): update db state with ui and emit events
This commit is contained in:
@ -8,6 +8,9 @@ use std::{
|
||||
};
|
||||
|
||||
use log::{error, info, warn};
|
||||
use tauri::{AppHandle, Emitter};
|
||||
|
||||
use crate::{db::DatabaseGameStatus, library::GameUpdateEvent, DB};
|
||||
|
||||
use super::{
|
||||
download_agent::{GameDownloadAgent, GameDownloadError},
|
||||
@ -64,13 +67,14 @@ pub struct DownloadManagerBuilder {
|
||||
sender: Sender<DownloadManagerSignal>,
|
||||
progress: Arc<Mutex<Option<ProgressObject>>>,
|
||||
status: Arc<Mutex<DownloadManagerStatus>>,
|
||||
app_handle: AppHandle,
|
||||
|
||||
current_game_interface: Option<Arc<AgentInterfaceData>>, // Should be the only game download agent in the map with the "Go" flag
|
||||
active_control_flag: Option<DownloadThreadControl>,
|
||||
}
|
||||
|
||||
impl DownloadManagerBuilder {
|
||||
pub fn build() -> DownloadManager {
|
||||
pub fn build(app_handle: AppHandle) -> DownloadManager {
|
||||
let queue = Queue::new();
|
||||
let (command_sender, command_receiver) = channel();
|
||||
let active_progress = Arc::new(Mutex::new(None));
|
||||
@ -85,6 +89,7 @@ impl DownloadManagerBuilder {
|
||||
status: status.clone(),
|
||||
sender: command_sender.clone(),
|
||||
progress: active_progress.clone(),
|
||||
app_handle,
|
||||
};
|
||||
|
||||
let terminator = spawn(|| manager.manage_queue());
|
||||
@ -92,6 +97,23 @@ impl DownloadManagerBuilder {
|
||||
DownloadManager::new(terminator, queue, active_progress, command_sender)
|
||||
}
|
||||
|
||||
fn set_game_status(&self, id: String, status: DatabaseGameStatus) {
|
||||
let mut db_handle = DB.borrow_data_mut().unwrap();
|
||||
db_handle
|
||||
.games
|
||||
.games_statuses
|
||||
.insert(id.clone(), status.clone());
|
||||
self.app_handle
|
||||
.emit(
|
||||
&format!("update_game/{}", id),
|
||||
GameUpdateEvent {
|
||||
game_id: id,
|
||||
status: status,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn manage_queue(mut self) -> Result<(), ()> {
|
||||
loop {
|
||||
let signal = match self.command_receiver.recv() {
|
||||
@ -145,6 +167,8 @@ impl DownloadManagerBuilder {
|
||||
self.download_agent_registry.remove(&game_id);
|
||||
self.active_control_flag = None;
|
||||
*self.progress.lock().unwrap() = None;
|
||||
|
||||
self.set_game_status(game_id, DatabaseGameStatus::Installed);
|
||||
}
|
||||
}
|
||||
self.sender.send(DownloadManagerSignal::Go).unwrap();
|
||||
@ -160,54 +184,61 @@ impl DownloadManagerBuilder {
|
||||
));
|
||||
let agent_status = GameDownloadStatus::Uninitialised;
|
||||
let interface_data = AgentInterfaceData {
|
||||
id,
|
||||
id: id.clone(),
|
||||
status: Mutex::new(agent_status),
|
||||
};
|
||||
self.download_agent_registry
|
||||
.insert(interface_data.id.clone(), download_agent);
|
||||
self.download_queue.append(interface_data);
|
||||
|
||||
self.set_game_status(id, DatabaseGameStatus::Queued);
|
||||
}
|
||||
|
||||
fn manage_go_signal(&mut self) {
|
||||
info!("Got signal 'Go'");
|
||||
if !self.download_agent_registry.is_empty() && !self.download_queue.empty() {
|
||||
info!("Starting download agent");
|
||||
let agent_data = self.download_queue.read().front().unwrap().clone();
|
||||
let download_agent = self
|
||||
.download_agent_registry
|
||||
.get(&agent_data.id)
|
||||
.unwrap()
|
||||
.clone();
|
||||
self.current_game_interface = Some(agent_data);
|
||||
|
||||
let progress_object = download_agent.progress.clone();
|
||||
*self.progress.lock().unwrap() = Some(progress_object);
|
||||
|
||||
let active_control_flag = download_agent.control_flag.clone();
|
||||
self.active_control_flag = Some(active_control_flag.clone());
|
||||
|
||||
let sender = self.sender.clone();
|
||||
|
||||
info!("Spawning download");
|
||||
spawn(move || {
|
||||
match download_agent.download() {
|
||||
// Returns once we've exited the download
|
||||
// (not necessarily completed)
|
||||
// The download agent will fire the completed event for us
|
||||
Ok(_) => {}
|
||||
// If an error occurred while *starting* the download
|
||||
Err(err) => {
|
||||
error!("error while managing download: {}", err);
|
||||
sender.send(DownloadManagerSignal::Error(err)).unwrap();
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
active_control_flag.set(DownloadThreadControlFlag::Go);
|
||||
self.set_status(DownloadManagerStatus::Downloading);
|
||||
} else {
|
||||
info!("Nothing was set");
|
||||
if !(!self.download_agent_registry.is_empty() && !self.download_queue.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
info!("Starting download agent");
|
||||
let agent_data = self.download_queue.read().front().unwrap().clone();
|
||||
let download_agent = self
|
||||
.download_agent_registry
|
||||
.get(&agent_data.id)
|
||||
.unwrap()
|
||||
.clone();
|
||||
self.current_game_interface = Some(agent_data);
|
||||
|
||||
let progress_object = download_agent.progress.clone();
|
||||
*self.progress.lock().unwrap() = Some(progress_object);
|
||||
|
||||
let active_control_flag = download_agent.control_flag.clone();
|
||||
self.active_control_flag = Some(active_control_flag.clone());
|
||||
|
||||
let sender = self.sender.clone();
|
||||
|
||||
info!("Spawning download");
|
||||
spawn(move || {
|
||||
match download_agent.download() {
|
||||
// Returns once we've exited the download
|
||||
// (not necessarily completed)
|
||||
// The download agent will fire the completed event for us
|
||||
Ok(_) => {}
|
||||
// If an error occurred while *starting* the download
|
||||
Err(err) => {
|
||||
error!("error while managing download: {}", err);
|
||||
sender.send(DownloadManagerSignal::Error(err)).unwrap();
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
active_control_flag.set(DownloadThreadControlFlag::Go);
|
||||
self.set_status(DownloadManagerStatus::Downloading);
|
||||
self.set_game_status(
|
||||
self.current_game_interface.as_ref().unwrap().id.clone(),
|
||||
DatabaseGameStatus::Downloading,
|
||||
);
|
||||
}
|
||||
fn manage_error_signal(&self, error: GameDownloadError) {
|
||||
let current_status = self.current_game_interface.clone().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user