fix(uninstalling): Re-enabled uninstalling apps

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2025-01-04 16:27:30 +11:00
parent dceaa56ade
commit a56ee25581
5 changed files with 71 additions and 92 deletions

View File

@ -169,9 +169,6 @@ impl DownloadManagerBuilder {
DownloadManagerSignal::Cancel(meta) => {
self.manage_cancel_signal(&meta);
}
DownloadManagerSignal::Uninstall(meta) => {
self.uninstall_application(&meta);
}
_ => {}
};
}
@ -321,14 +318,6 @@ impl DownloadManagerBuilder {
}
self.push_ui_queue_update();
}
fn uninstall_application(&mut self, meta: &DownloadableMetadata) {
let download_agent = match self.download_agent_registry.get(meta) {
Some(download_agent) => download_agent.clone(),
None => return,
};
self.manage_cancel_signal(meta);
download_agent.on_uninstall(&self.app_handle);
}
fn push_ui_stats_update(&self, kbs: usize, time: usize) {
let event_data = StatsUpdateEvent { speed: kbs, time };

View File

@ -17,5 +17,4 @@ pub trait Downloadable: Send + Sync {
fn on_complete(&self, app_handle: &AppHandle);
fn on_incomplete(&self, app_handle: &AppHandle);
fn on_cancelled(&self, app_handle: &AppHandle);
fn on_uninstall(&self, app_handle: &AppHandle);
}

View File

@ -248,7 +248,6 @@ impl GameDownloadAgent {
pool.scope(|scope| {
for (index, context) in self.contexts.lock().unwrap().iter().enumerate() {
info!("Running index {}", index);
let completed_indexes = completed_indexes_loop_arc.clone();
let progress = self.progress.get(index); // Clone arcs
@ -356,83 +355,15 @@ impl Downloadable for GameDownloadAgent {
on_game_complete(&self.metadata(), self.stored_manifest.base_path.to_string_lossy().to_string(), app_handle).unwrap();
}
fn on_incomplete(&self, app_handle: &tauri::AppHandle) {
fn on_incomplete(&self, _app_handle: &tauri::AppHandle) {
*self.status.lock().unwrap() = DownloadStatus::Queued;
return;
}
fn on_cancelled(&self, app_handle: &tauri::AppHandle) {
fn on_cancelled(&self, _app_handle: &tauri::AppHandle) {
return;
}
fn on_uninstall(&self, app_handle: &tauri::AppHandle) {
let mut db_handle = DB.borrow_data_mut().unwrap();
let metadata = self.metadata();
db_handle
.applications
.transient_statuses
.entry(metadata.clone())
.and_modify(|v| *v = ApplicationTransientStatus::Uninstalling {});
push_game_update(
app_handle,
&metadata,
(None, Some(ApplicationTransientStatus::Uninstalling {})),
);
let previous_state = db_handle.applications.game_statuses.get(&metadata.id).cloned();
if previous_state.is_none() {
info!("uninstall job doesn't have previous state, failing silently");
return;
}
let previous_state = previous_state.unwrap();
if let Some((version_name, install_dir)) = match previous_state {
GameDownloadStatus::Installed {
version_name,
install_dir,
} => Some((version_name, install_dir)),
GameDownloadStatus::SetupRequired {
version_name,
install_dir,
} => Some((version_name, install_dir)),
_ => None,
} {
db_handle
.applications
.transient_statuses
.entry(metadata.clone())
.and_modify(|v| *v = ApplicationTransientStatus::Uninstalling {});
drop(db_handle);
let sender = self.sender.clone();
let app_handle = app_handle.clone();
spawn(move || match remove_dir_all(install_dir) {
Err(e) => {
sender
.send(DownloadManagerSignal::Error(ApplicationDownloadError::IoError(
e.kind(),
)))
.unwrap();
}
Ok(_) => {
let mut db_handle = DB.borrow_data_mut().unwrap();
db_handle.applications.transient_statuses.remove(&metadata);
db_handle
.applications
.game_statuses
.entry(metadata.id.clone())
.and_modify(|e| *e = GameDownloadStatus::Remote {});
drop(db_handle);
DB.save().unwrap();
info!("uninstalled game id {}", metadata.id);
push_game_update(&app_handle, &metadata, (Some(GameDownloadStatus::Remote {}), None));
}
});
}
}
fn status(&self) -> DownloadStatus {
self.status.lock().unwrap().clone()
}

View File

@ -1,6 +1,8 @@
use std::fs::remove_dir_all;
use std::sync::Mutex;
use std::thread::spawn;
use log::info;
use log::{error, info};
use serde::{Deserialize, Serialize};
use tauri::Emitter;
use tauri::{AppHandle, Manager};
@ -238,16 +240,78 @@ fn fetch_game_verion_options_logic<'a>(
pub fn uninstall_game(
game_id: String,
state: tauri::State<'_, Mutex<AppState>>,
app_handle: AppHandle
) -> Result<(), String> {
let state_lock = state.lock().unwrap();
let meta = get_current_meta(&game_id)?;
state_lock.download_manager.uninstall_application(meta);
drop(state_lock);
println!("{:?}", meta);
uninstall_game_logic(meta, &app_handle);
Ok(())
}
fn uninstall_game_logic(meta: DownloadableMetadata, app_handle: &AppHandle) {
println!("Triggered uninstall for agent");
let mut db_handle = DB.borrow_data_mut().unwrap();
db_handle
.applications
.transient_statuses
.entry(meta.clone())
.and_modify(|v| *v = ApplicationTransientStatus::Uninstalling {});
push_game_update(
app_handle,
&meta,
(None, Some(ApplicationTransientStatus::Uninstalling {})),
);
let previous_state = db_handle.applications.game_statuses.get(&meta.id).cloned();
if previous_state.is_none() {
info!("uninstall job doesn't have previous state, failing silently");
return;
}
let previous_state = previous_state.unwrap();
if let Some((version_name, install_dir)) = match previous_state {
GameDownloadStatus::Installed {
version_name,
install_dir,
} => Some((version_name, install_dir)),
GameDownloadStatus::SetupRequired {
version_name,
install_dir,
} => Some((version_name, install_dir)),
_ => None,
} {
db_handle
.applications
.transient_statuses
.entry(meta.clone())
.and_modify(|v| *v = ApplicationTransientStatus::Uninstalling {});
drop(db_handle);
let app_handle = app_handle.clone();
spawn(move || match remove_dir_all(install_dir) {
Err(e) => {
error!("{}", e);
}
Ok(_) => {
let mut db_handle = DB.borrow_data_mut().unwrap();
db_handle.applications.transient_statuses.remove(&meta);
db_handle
.applications
.game_statuses
.entry(meta.id.clone())
.and_modify(|e| *e = GameDownloadStatus::Remote {});
drop(db_handle);
DB.save().unwrap();
info!("uninstalled game id {}", &meta.id);
push_game_update(&app_handle, &meta, (Some(GameDownloadStatus::Remote {}), None));
}
});
}
}
pub fn get_current_meta(game_id: &String) -> Result<DownloadableMetadata, String> {
match DB.borrow_data().unwrap().applications.installed_game_version.get(game_id) {
Some(meta) => Ok(meta.clone()),

View File

@ -51,8 +51,4 @@ impl Downloadable for ToolDownloadAgent {
fn on_cancelled(&self, app_handle: &tauri::AppHandle) {
todo!()
}
fn on_uninstall(&self, app_handle: &tauri::AppHandle) {
todo!()
}
}