feat: autoupdate ui

This commit is contained in:
2025-04-12 10:53:06 +00:00
parent 28269c5d75
commit f3016eb029
6 changed files with 92 additions and 24 deletions

View File

@ -17,10 +17,9 @@ use fern::colors::{Color, ColoredLevelConfig};
use model::misc::Game;
use pkg::PkgKey;
use pkg_store::Payload;
use tauri::{AppHandle, Listener, Manager, RunEvent};
use tauri::{AppHandle, Emitter, Listener, Manager, RunEvent};
use tauri_plugin_deep_link::DeepLinkExt;
use tauri_plugin_cli::CliExt;
use tauri_plugin_updater::UpdaterExt;
use tokio::{fs, sync::Mutex, try_join};
static EXIT_REQUESTED: OnceLock<()> = OnceLock::new();
@ -248,7 +247,9 @@ pub async fn run(_args: Vec<String>) {
cmd::list_com_ports,
cmd::list_patches
cmd::list_patches,
cmd::has_updated,
])
.build(tauri::generate_context!())
.expect("error while building tauri application");
@ -309,28 +310,56 @@ fn deep_link(app: AppHandle, args: Vec<String>) {
async fn update(app: tauri::AppHandle) -> tauri_plugin_updater::Result<()> {
let mutex = app.state::<Mutex<AppData>>();
let appd = mutex.lock().await;
if !appd.cfg.enable_autoupdates {
log::info!("skipping autoupdate");
return Ok(());
{
let mut appd = mutex.lock().await;
if !appd.cfg.enable_autoupdates {
log::info!("skipping auto-update");
// The frontend may not be available at this point
// So emit isn't suitable
appd.state.has_updated = true;
return Ok(());
}
}
if let Some(update) = app.updater()?.check().await? {
#[cfg(not(debug_assertions))]
{
use tauri_plugin_updater::UpdaterExt;
if let Some(update) = app.updater()?.check().await? {
let mut downloaded = 0;
update.download_and_install(
|chunk_length, content_length| {
downloaded += chunk_length;
_ = app.emit("update-progress", (chunk_length as f64) / (content_length.unwrap_or(u64::MAX) as f64));
},
|| {
log::info!("download finished");
},
)
.await?;
log::info!("update installed");
app.restart();
}
}
// One day I will write proper tests
#[cfg(debug_assertions)]
{
std::thread::sleep(std::time::Duration::from_millis(2000));
let mut downloaded = 0;
update.download_and_install(
|chunk_length, content_length| {
downloaded += chunk_length;
log::debug!("downloaded {downloaded} from {content_length:?}");
},
|| {
log::info!("download finished");
},
)
.await?;
log::info!("update installed");
app.restart();
while downloaded < 200 {
std::thread::sleep(std::time::Duration::from_millis(10));
downloaded += 1;
app.emit("update-progress", (downloaded as f32) / 200f32)?;
}
}
log::info!("ending auto-update check");
let mut appd = mutex.lock().await;
appd.state.has_updated = true;
Ok(())
}